playMaker

Author Topic: Please help with this vector rotation [SOLVED]  (Read 8282 times)

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Please help with this vector rotation [SOLVED]
« on: September 08, 2013, 09:07:35 AM »
So everything always seems to be moving ahead nicely until I have to play with vectors and other complex math stuff. It reminds me I'm an artist by trade, not a programmer lol.

I have a prototype tank that aims at a wherever the mouse is on my terrain using the Smooth Look At Direction action. This works nicely.

The problem I have is with the rotation of the turret on slopes. I just want the tank to rotate its turret around its local Y axis.

Hopefully these pics will detail it better:


This shows the problem. The turret clips through the tank when trying to rotate on slopes, this happens wether I have the Keep Vertical on or off.

Below is what it should look like, regardless of angle of slope or position of the mouse. The turret only rotates around it's local Y axis:


I know the answer lies in some vector equation that completely eludes me..

How do I use the Smooth Look actions yet keep the rotation only on the local Y axis..?? sorry If I haven't explained well.
« Last Edit: September 09, 2013, 07:24:56 AM by uberwolfe »

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Please help with this vector rotation :(
« Reply #1 on: September 09, 2013, 04:53:37 AM »
nobody has any idea how this could be done?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation :(
« Reply #2 on: September 09, 2013, 06:50:04 AM »
Hi,

 Here we go: I modified the lookAt action, created a new one called, LookAt2 and it has a new check box "Constraint locally" which in effect makes the "keep vertical" option local, instead of global.


I've attached a fully working example. You'll note that you do need to pass the "up" vector, which is in this case always the vertical axis of the turret.


Bye,

 Jean
« Last Edit: February 12, 2020, 01:32:05 AM by jeanfabre »

uberwolfe

  • Junior Playmaker
  • **
  • Posts: 59
Re: Please help with this vector rotation :(
« Reply #3 on: September 09, 2013, 07:24:09 AM »
Jean, you are an absolute champion - this works perfectly!

Where do I send the beer? haha :)

Thank you so much for this, couldn't have asked for more!!
« Last Edit: September 09, 2013, 07:37:45 AM by uberwolfe »

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Please help with this vector rotation :(
« Reply #4 on: February 09, 2020, 10:36:22 PM »
Hi,

 Here we go: I modified the lookAt action, created a new one called, LookAt2 and it has a new check box "Constraint locally" which in effect makes the "keep vertical" option local, instead of global.


I've attached a fully working example. You'll note that you do need to pass the "up" vector, which is in this case always the vertical axis of the turret.


Bye,

 Jean

For me it doesn't work. Downloading last version of script from Ecosystem made it move, but as you can see from the video it works incorrectly.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation [SOLVED]
« Reply #5 on: February 10, 2020, 01:03:21 AM »
Hi,

 I am not sure why you use the direction. It works fine here just as is targeting what ever you want as a gameobject.




Bye,

 Jean

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Please help with this vector rotation [SOLVED]
« Reply #6 on: February 10, 2020, 01:28:15 AM »
Hi,

 I am not sure why you use the direction. It works fine here just as is targeting what ever you want as a gameobject.




Bye,

 Jean

In that video. I've launched a demo scene that you provided here.
https://hutonggames.com/playmakerforum/index.php?topic=4908.msg23062#msg23062

Your setup works only when "tank" object is in 0 0 0 position, as soon as it is moved anywhere else it stops working.


 I'm using Unity 2018.4.5 and Playmaker 1.9.0.p18
« Last Edit: February 10, 2020, 01:51:07 AM by Gua »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation [SOLVED]
« Reply #7 on: February 10, 2020, 01:42:40 AM »
Hi,

 ow, yes... d'oh! let me check what's wrong with this.

Bye,

 Jean

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Please help with this vector rotation [SOLVED]
« Reply #8 on: February 10, 2020, 01:52:52 AM »
Hi,

 ow, yes... d'oh! let me check what's wrong with this.

Bye,

 Jean
Just to add more information. It's not project related. Here's a setup of a new empty project from scratch. Issue is still there.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation [SOLVED]
« Reply #9 on: February 10, 2020, 10:34:17 AM »
Hi,

 I updated the action, can you check it and let me know if it works ok on your end? thanks :)


Bye,

 Jean

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Please help with this vector rotation [SOLVED]
« Reply #10 on: February 10, 2020, 09:22:32 PM »
Hi,

Thanks for the updated action. Unfortunately new version is still coordinates sensitive.

Here's an example:

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation [SOLVED]
« Reply #11 on: February 11, 2020, 01:03:43 AM »
Hi,

grrrr!!! :)

Ok, please update again, that should do it.

Bye,

 Jean

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Please help with this vector rotation [SOLVED]
« Reply #12 on: February 11, 2020, 02:12:09 AM »
Hi,

grrrr!!! :)

Ok, please update again, that should do it.

Bye,

 Jean
Last version seems to be working fine. Thank you for the fix and quick response!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Please help with this vector rotation [SOLVED]
« Reply #13 on: February 12, 2020, 01:32:46 AM »
Hi,

 Cool!, I re uploaded the package in the post above as it was old.

Bye,

 Jean

Creti

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Please help with this vector rotation [SOLVED]
« Reply #14 on: June 29, 2023, 10:26:14 AM »
ChatGPT helped me writing "Smooth Look At Direction" in Local Space. I share it in case, others need that too. It helped me with a tank turret and a helicopter.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Transform)]
    [Tooltip("Smoothly Rotates a Game Object so its forward vector points in the specified Direction. Lets you fire an event when minMagnitude is reached")]
    public class SmoothLookAtDirectionLocal : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The GameObject to rotate.")]
        public FsmOwnerDefault gameObject;

        [RequiredField]
        [Tooltip("The direction to smoothly rotate towards.")]
        public FsmVector3 targetDirection;

        [Tooltip("Only rotate if Target Direction Vector length is greater than this threshold.")]
        public FsmFloat minMagnitude;

        [Tooltip("Keep this vector pointing up as the GameObject rotates.")]
        public FsmVector3 upVector;

        [RequiredField]
        [Tooltip("Eliminate any tilt up/down as the GameObject rotates.")]
        public FsmBool keepVertical;

        [RequiredField]
        [HasFloatSlider(0.5f, 15)]
        [Tooltip("How quickly to rotate.")]
        public FsmFloat speed;

        [Tooltip("Perform in LateUpdate. This can help eliminate jitters in some situations.")]
        public bool lateUpdate;

        [Tooltip("Event to send if the direction difference is less than Min Magnitude.")]
        public FsmEvent finishEvent;

        [Tooltip("Stop running the action if the direction difference is less than Min Magnitude.")]
        public FsmBool finish;

        GameObject previousGo; // track game object so we can re-initialize when it changes.
        Quaternion lastRotation;
        Quaternion desiredRotation;

        public override void Reset()
        {
            gameObject = null;
            targetDirection = new FsmVector3 { UseVariable = true };
            minMagnitude = 0.1f;
            upVector = new FsmVector3 { UseVariable = true };
            keepVertical = true;
            speed = 5;
            lateUpdate = true;
            finishEvent = null;
        }

        public override void OnPreprocess()
        {
            Fsm.HandleLateUpdate = true;
        }

        public override void OnEnter()
        {
            previousGo = null;
        }

        public override void OnUpdate()
        {
            if (!lateUpdate)
            {
                DoSmoothLookAtDirection();
            }
        }

        public override void OnLateUpdate()
        {
            if (lateUpdate)
            {
                DoSmoothLookAtDirection();
            }
        }

        void DoSmoothLookAtDirection()
        {
            if (targetDirection.IsNone)
            {
                return;
            }

            var go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null)
            {
                return;
            }

            // re-initialize if game object has changed

            if (previousGo != go)
            {
                lastRotation = go.transform.localRotation; // Use localRotation instead of rotation
                desiredRotation = lastRotation;
                previousGo = go;
            }

            // desired direction

            var diff = targetDirection.Value;

            if (keepVertical.Value)
            {
                diff.y = 0;
            }

            var reachedTarget = false;
            if (diff.sqrMagnitude > minMagnitude.Value)
            {
                desiredRotation = Quaternion.LookRotation(diff, upVector.IsNone ? Vector3.up : upVector.Value);
            }
            else
            {
                reachedTarget = true;
            }
           
            lastRotation = Quaternion.Slerp(lastRotation, desiredRotation, speed.Value * Time.deltaTime);
            go.transform.localRotation = lastRotation; // Use localRotation instead of rotation
           
            if (reachedTarget)
            {
                Fsm.Event(finishEvent);
                if (finish.Value)
                {
                    Finish();
                }
            }
        }
    }
}
« Last Edit: August 27, 2023, 08:36:11 PM by Creti »