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

Help with player tilt

Discussion in 'Scripting' started by Jake_Martin_, Oct 29, 2015.

  1. Jake_Martin_

    Jake_Martin_

    Joined:
    Aug 11, 2013
    Posts:
    4
    I'm making a sort of infinite runner/flyer sort of thing at the moment, but I'm having a problem with getting my spaceship thing to tilt. It 'flies' (actually, the ground moves, but that's not important) toward/along the x-axis and I just need it to tilt left a bit when the player is going left and vice-versa.
    I found this snippet somewhere, changed it a bit, put it in the update function of 'Player' and it's proved to be half the solution.

    Code (CSharp):
    1. float x = Input.GetAxis("Horizontal") * 15.0f;
    2. Vector3 euler = transform.localEulerAngles;
    3. euler.x = Mathf.Lerp(euler.x, x, 2.0f * Time.deltaTime);
    4. transform.localEulerAngles = euler;
    This seems to be exactly what I need, and it's pretty elegant, but it really is only half the solution - literally. When pressing left, it tilts exactly how you would expect. When I attempt to tilt right, it rapidly wobbles somewhere around a rotation of 274 in the x-axis, and flip flops between 0 and 180 degrees in the other 2 axis (according to the inspector).

    I'd seriously appreciate some help in fixing this; it's 1:38 in the morning and it's keeping me up.

    UPDATE: Here's some screenshots for clarity about what's going on -
    Left:
    left.png
    Right:
    right.png
    And there's a gif of that here: http://gfycat.com/ImpureBothDinosaur
     
    Last edited: Oct 29, 2015
  2. sgemmen9

    sgemmen9

    Joined:
    Jul 24, 2015
    Posts:
    17
    Hey so I'm super green and might not be of much help. But I just had this problem and seemed like everything was good except for bouncing and titling. For me, I just checked Is kenetic and Use gravity on the rigid body collider and that fixed it.
     
  3. Jake_Martin_

    Jake_Martin_

    Joined:
    Aug 11, 2013
    Posts:
    4
    Thanks for trying, but it didn't help. In fact, it didn't even have a rigidbody and adding one and making it kinematic and stuff didn't make any difference.
     
  4. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    It's not exactly perfect, but I hope it helps

    turned off gravity
    didn't enable is kinematic

    Code (CSharp):
    1.     public float tilt;
    2.     Rigidbody rb;
    3.     // Use this for initialization
    4.     void Start () {
    5.         rb = GetComponent<Rigidbody>();
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         if (Input.GetKey(KeyCode.A))
    11.         {
    12.             rb.velocity = Vector3.left * 2f;
    13.             transform.rotation = Quaternion.Euler(0.0f, 0.0f, (transform.position).x * -tilt);
    14.         }
    15.  
    16.         if (Input.GetKey(KeyCode.D))
    17.         {
    18.             rb.velocity = Vector3.right * 2f;
    19.             transform.rotation = Quaternion.Euler(0.0f, 0.0f, (transform.position).x * -tilt);
    20.         }
    21. }
     
  5. Jake_Martin_

    Jake_Martin_

    Joined:
    Aug 11, 2013
    Posts:
    4
    Thanks for the help, but this appears to cause more problems than it fixes, and I don't really know how to fix them. It did however help me anyway, as it gave me an idea of how to fix one of my minor niggles with my old code. But, nonetheless, the problem remains. Here is what it looks like now:


    Code (CSharp):
    1.         if (Input.GetAxis("Horizontal")>0){
    2.             Vector3 euler = transform.localEulerAngles;
    3.             euler.x = Mathf.Lerp(euler.x, maxTilt, 2.0f * Time.deltaTime);
    4.             transform.localEulerAngles = euler;
    5.         }
    6.         else if (Input.GetAxis("Horizontal")<0 && transform.localEulerAngles.x>1){
    7.             Vector3 euler = transform.localEulerAngles;
    8.             euler.x = Mathf.Lerp(euler.x, 0, 4.0f * Time.deltaTime);
    9.             transform.localEulerAngles = euler;
    10.         }
    11.         else if (Input.GetAxis("Horizontal")<0 && transform.localEulerAngles.x <= 1){
    12.             Vector3 euler = transform.localEulerAngles;
    13.             euler.x = Mathf.Lerp(0, -maxTilt, 2.0f * Time.deltaTime);
    14.             transform.localEulerAngles = euler;
    15.         }
    It's still a work in progress though, because I can't get past the way once it gets to zero, rather than carry on lerping to -maxTilt, it randomly jumps to ~274 degrees and gets stuck. So, yeah, this still isn't fixed and I could still really do with some help here.
     
  6. Jake_Martin_

    Jake_Martin_

    Joined:
    Aug 11, 2013
    Posts:
    4
    Well, I got bored and changed the controls to mouse controls (which don't need lerping), so... While this isn't exactly solved, I have a workaround.
     
  7. Moosetaco

    Moosetaco

    Joined:
    Jan 27, 2013
    Posts:
    77