Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Resetting X/Z rotation to zero with Quaternion Slerp

Discussion in 'Scripting' started by Monsterwald, Apr 10, 2021.

  1. Monsterwald

    Monsterwald

    Joined:
    Jan 19, 2020
    Posts:
    68
    I try to get my rotation !Except Y axis! smoothly back to zero, tried to use slerp but no matter what I try it resets every axis back to zero.

    Code (CSharp):
    1.     [SerializeField] float flyingSpeed;
    2.     [SerializeField] float maxSpeed;
    3.     [SerializeField] float turningSpeed_H, turningSpeed_V;
    4.     [SerializeField] Transform fish;
    5.     [SerializeField] float targetTilt;
    6.     float minSpeed;
    7.     float normalSpeed;
    8.     float timeCount = 0;
    9.  
    10.     float blaaa;
    11.  
    12.     private void Start()
    13.     {
    14.         normalSpeed = flyingSpeed;
    15.         minSpeed = normalSpeed;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         var h = Input.GetAxis("Horizontal");
    21.         var v = Input.GetAxis("Vertical");
    22.  
    23.         transform.Translate(Vector3.forward * Time.deltaTime * flyingSpeed);
    24.         transform.Rotate(v * turningSpeed_V, (h * turningSpeed_H), 0);
    25.  
    26.         blaaa = transform.rotation.y;
    27.  
    28.         if (v == 0 && h == 0) {
    29.             transform.rotation = Quaternion.Slerp(transform.rotation,
    30.             Quaternion.Euler(0, blaaa, 0), 0.002f);
    31.         }
    32.  
    33.     }
    Edit: got it to work:

    Code (CSharp):
    1.     [SerializeField] float flyingSpeed;
    2.     [SerializeField] float maxSpeed;
    3.     [SerializeField] float turningSpeed_H, turningSpeed_V;
    4.     [SerializeField] Transform fish;
    5.     [SerializeField] float targetTilt;
    6.     float minSpeed;
    7.     float normalSpeed;
    8.     float timeCount = 0;
    9.  
    10.     float blaaa;
    11.  
    12.     private void Start()
    13.     {
    14.         normalSpeed = flyingSpeed;
    15.         minSpeed = normalSpeed;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.  
    21.         var h = Input.GetAxis("Horizontal");
    22.         var v = Input.GetAxis("Vertical");
    23.  
    24.         transform.Translate(Vector3.forward * Time.deltaTime * flyingSpeed);
    25.         transform.Rotate(v * turningSpeed_V, (h * turningSpeed_H), 0);
    26.  
    27.         if (v != 0 || h != 0) blaaa = transform.rotation.eulerAngles.y;
    28.        
    29. if ((v < 0.01f && v > -0.01f) && (h < 0.01f && h > -0.01f))
    30.         {
    31.             //transform.rotation = Quaternion.Euler(0, blaaa, 0);
    32.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, blaaa, 0), 0.005f);
    33.         }
    34.  
    35.     }
     
    Last edited: Apr 11, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Slerp and Lerp take two quantities and give you a fraction in between.

    This means if line 29/30 is executed, it gives you 2/1000ths of the "blaa" and mostly the rotation.

    Also, line 28 is a bad idea: if your joystick is just a smidge off of zero, either axis, line 29/30 will never execute. Generally never test floating point quantities for equality.

    The other issue of the script above is you are mixing relative rotation (line 24 and the Rotate() method), and directly and instantaneously setting rotation with line 29.

    If you're trying to make a basic flying script, start from some examples.
     
    Monsterwald likes this.
  3. Monsterwald

    Monsterwald

    Joined:
    Jan 19, 2020
    Posts:
    68
    I solved it, finally
    Thanks I keep that in mind, even though I don't understand the rotation part (mixing relativ and the one in line 29), I already searched for examples and looked at every one I could find but samples about flying (mine should be a flying fish) are extremely sparse... I already made many games for my college, ...badly scripted to be fair. Programming is one of these things I can't get the hang of it
     
    Last edited: Apr 11, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Explicitly:

    Line 25 says "from whatever the rotation is, Rotate() it this much to a new rotation" (e.g, relatively adjust it)

    Line 29 says "immediately set the rotation to the computed value, fractionally close to what is already is, based on the 2/1000ths of a Slerp() towards the blah rotation" (e.g., absolutely set it)

    As long as you keep trying it will steadily build up!

    Keep things simple and always use lots of Debug.Log() to spit numbers out so you can reason about them.
     
    Monsterwald likes this.