Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detecting Change In Steering Direction

Discussion in 'Scripting' started by Pix77, Oct 24, 2018.

  1. Pix77

    Pix77

    Joined:
    Mar 18, 2018
    Posts:
    21
    Using the below block of code, I have a moving object, which constantly moves forward, have the ability to smoothly rotate left and right. Going from a neutral joystick position to left or right, or the opposite of that works just fine, but when moving the joystick from left to right or vice versa, the object turns really slowly compared to how it should turn sharper and faster like when going from neutral to a direction. Is there a way to detect this kind of change and correct in joystick position and correct the rotation speed?

    Code (CSharp):
    1. //Turning
    2.         if (Input.GetAxis ("Horizontal") > 0 || Input.GetAxis ("Horizontal") < 0) {
    3.             m = 0;
    4.             r += (Input.GetAxis ("Horizontal") * handling) / 100;
    5.         } else if (Input.GetAxis ("Horizontal") == 0) {
    6.             r = Mathf.Lerp(r, 0.5f, m);
    7.             m += 0.1f;
    8.         }
    9.  
    10. rotateConstant = Mathf.Lerp (-1, 1, r);
    11. rotatePower = (rotateConstant * handling) * 2;
    12. transform.Rotate(0, rotatePower, 0);
     
  2. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    You can check your delta on Input.GetAxis ("Horizontal") by using another variable called prevHor and then if it's larger than a threshold you can speed up your lerping or snap your r value back to 0 to get the same effect.

    Code (csharp):
    1.  
    2. float prevHor = 0f;
    3. float threshold = 0.5f;
    4.  
    5. void Update () {
    6.   float h = Input.GetAxis("Horizontal");
    7.  
    8.   if (Mathf.Abs(prevHor - h) > threshold) {
    9.    r = 0f;
    10.   }
    11.  
    12.   prevHor = h;
    13. }
    14.  
    15.  
    Not tested so it may not give exactly the feeling you want but this is a good way to make your axis input more snappy.
     
    Pix77 likes this.
  3. Pix77

    Pix77

    Joined:
    Mar 18, 2018
    Posts:
    21
    Thanks, that works, but it produces a strange side effect. Turning left works just fine, but turning right immediately decreases r to 0, making the character look sharply left before continuing to smoothly and correctly turn right.

    EDIT: I realize my mistake, r needed to be set to 0.5. Thank you for your help!
     
  4. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    Something you can do instead of setting r to 0 is change your handling till you're past 0.

    Code (csharp):
    1.  
    2. bool fastMove = false;
    3. bool isNegative = false;
    4.  
    5. if (Mathf.Abs(prevHor - h) > threshold) {
    6.   if (r < 0) {
    7.     isNegative = true;
    8.   }
    9.   fastMove = true;
    10. }
    11.  
    12. if (fastMove == false) {
    13.  r += (Input.GetAxis ("Horizontal") * handling) / 100;
    14. } else {
    15.  r +=(Input.GetAxis ("Horizontal") * handling) / 20;
    16.  if ((isNegative && r >= 0f) || (isNegative == false && r <= 0f)) {
    17.     fastMove = false;
    18.  }
    19. }
    20.  
    21.  
    Again not tested but has the general idea.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can also play with the axis settings in the input manager. I can't remember what the default is for Horizontal. But its certainly possible to make it more snappy.