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. Dismiss Notice

180 Degrees of Rotation With just the Horizontal Input Axis Only (SOLVED)

Discussion in 'Scripting' started by IDreamofIndie, Sep 2, 2016.

  1. IDreamofIndie

    IDreamofIndie

    Joined:
    Dec 24, 2014
    Posts:
    38
    Hello,
    I would like to have the -1 equal to 270 degrees, the 0 equal to 180 degrees, and the 1 equal to 90 degrees within the Horizontal Input Axis.
    I am currently doing this, but it uses both Axis and I only want it to be done with the Horizontal Input Axis.
    (Using InControl Asset)
    Code (CSharp):
    1. // Use last device which provided input.
    2.         var inputDevice = InputManager.ActiveDevice;
    3.  
    4.         var x = inputDevice.LeftStick.X;
    5.         var y = inputDevice.LeftStick.Y;
    6.         y = Mathf.Clamp (y, -1f, 0f);
    7.  
    8.         //Making Players Rotation Lerp to match Current Direction of the Joystick
    9.         if (x != 0.0 || y != 0.0)
    10.         {
    11.             angle = Mathf.Atan2 (y, x) * Mathf.Rad2Deg;
    12.             newangle = Quaternion.AngleAxis (270.0f - angle, Vector3.forward);
    13.             newangle = Quaternion.Inverse (newangle);
    14.             player.transform.rotation = Quaternion.Lerp(player.transform.rotation, newangle,  rotSpeed);
    15.         }
    Does any one know how I can accomplish this?
    Thanks
     
    Last edited: Sep 2, 2016
  2. IDreamofIndie

    IDreamofIndie

    Joined:
    Dec 24, 2014
    Posts:
    38
    Okay, so I can see how what I am looking for is confusing. Basically I want the Horizontal Input Axis to control the player turning from 90° Left When left stick is all the way Left(-1),90° Right When left stick is all the way Right(1), and 0°/Straight when left stick is at center(0). And have all the degrees of rotation between each of these points based on how far left/right the control stick is.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    So a few questions to clarify what you want:

    Is there a static World Forward direction. So If i push all the way left I will be turned left only as long as I keep holding left on the stick.. and as soon as I let go i snap back to this world forward? Or is this a open world. If i push all the way left that should turn me 90 degrees left in X seconds. If i push 1/2 way left.. it would take 2*x seconds to get there (or X seconds to turn 45 degrees). And by holding left i could eventually spin all the way around?

    Also I assume when you use the degrees you mentions in your post 270 was directly left, 180 was directly foward, and 90 was directly right (because those are odd numbers to use for that. Most poeple would assume 180 mean go backwards).
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Wouldn't that be simply:
    Code (CSharp):
    1. float angle = Input.GexAxis("Horizontal") * 90;
    2. transform.eulerAngles = new Vector3(0, 0, angle);
    (By the way, your original code is abusing Lerp. Please go read about using Lerp properly.)
     
  5. IDreamofIndie

    IDreamofIndie

    Joined:
    Dec 24, 2014
    Posts:
    38
    I want the Horizontal Input Axis to control the player turning from 90° Left When left stick is all the way Left(-1),90° Right When left stick is all the way Right(1), and 0°/Straight when left stick is at center(0). And have all the degrees of rotation between each of these points based on how far left/right the control stick is.

    Yes that does make sense and is very simple.

    I recently found this http://forum.unity3d.com/threads/convert-float-numbers-to-an-angle-solved.43855/
    and it seems to work very well.

    But your post makes much more sense and is easier to understand.

    I will also give your link about Lerp a read.

    Thank You for your help.

    EDIT:
    After trying what you posted it worked perfectly for what I needed!
    I seem to always over think the simple things.

    Thanks again for your help!
     
    Last edited: Sep 4, 2016
    JoeStrout likes this.