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

Is it possible to Lerp into the negative field?

Discussion in 'Scripting' started by kaysteinhoff2003, May 31, 2020.

  1. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    I'm working on a ship controller(or a flight controller don't know if I can cj´hange it that way) and I ran into some trouble here. I try to rotate my ship if I turn it so that it seems as if it would lean into the curve because of it's own weight. BTW I rotate it around the z axis. Everything works fine but when I try to rotate to the right(or into the negative when I turn left) I make a full turn around the positive field instead of the shortest way - into the negative field. I also tried to make the 'leaningSpeed' variable negative but then it stops turning at all!

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Rigidbody))]
    4. public class ShipMovement : MonoBehaviour
    5. {
    6.     public float finalVelocity = 1000;
    7.     public float turnSpeed = 20;
    8.     public float accelerationRate = 100;
    9.     public float decelerationRate = 250;
    10.    
    11.     public float leaningSpeed = 5f;
    12.     float leftLeaningSpeed;
    13.     public float dist;
    14.     public Vector3 LeaningLimit;
    15.    
    16.     Vector3 nullPiece;
    17.     public bool left = false;
    18.     public bool right = false;
    19.     float initialVelocity = 0;
    20.     float currentVelocity = 0;
    21.    
    22.     void Start()
    23.     {
    24.         leftLeaningSpeed = -leaningSpeed;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.        
    30.         if(Input.GetKey(KeyCode.A))
    31.         {
    32.             left = true;
    33.         }else{
    34.             left = false;
    35.         }
    36.        
    37.         if(Input.GetKey(KeyCode.D))
    38.         {
    39.             right = true;
    40.         }else{
    41.             right = false;
    42.         }
    43.        
    44.         if(left)
    45.         {
    46.             if (Vector3.Distance(transform.eulerAngles, LeaningLimit) > -LeaningLimit.z)
    47.             {
    48.                 dist = Vector3.Distance(transform.eulerAngles, LeaningLimit);
    49.                 transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, LeaningLimit, leaningSpeed * Time.deltaTime);
    50.             }
    51.             else
    52.             {
    53.                 transform.eulerAngles = LeaningLimit;
    54.                 left = false;
    55.             }
    56.         }
    57.        
    58.         if(right)
    59.         {
    60.             if (Vector3.Distance(transform.eulerAngles, LeaningLimit) > 0.01f)
    61.             {
    62.                 transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, LeaningLimit, leaningSpeed * Time.deltaTime);
    63.             }
    64.             else
    65.             {
    66.                 transform.eulerAngles = LeaningLimit;
    67.                 right = false;
    68.             }
    69.         }
    That's not the full code but if you want to see it in it's full beauty just tell me.

    Thanks for the help already!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    You're not using Lerp correctly. Your argument list is more suited to Vector3.MoveTowards(). Try dropping that function in instead of Lerp with the same arguments.

    Lerp requires the first two arguments to be constant across the animation, and the third should move uniformly from 0 to 1.
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    That's an odd, but standard style of Lerp. The form is A=Lerp(A,B, smallConstant);. Notice how the start and the output are the same. It moves A by a small bit towards B each time, slower and slower as it gets near. It's tricky to adjust to different frame rates, but gives a cheap smoothing effect. I assume transform.eulerAngles and then transform.rotation.eulerAngles is only by mistake.

    The 0-1 Lerps look like: A=Lerp(unchangingStart, unchangingEnd, varFrom0to1);
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I actually use Lerp far more often for this purpose than the legit purpose... it's like "poor man's low-pass filter."
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    I think it's fine but it usually isn't doing what inexperienced people think it's doing.
     
  6. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    Well I am using unchaning starts and ends my problem is that my ship does a 360 around the z axis without stoping at the angle I want to reach.