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

Need Help with Rotation

Discussion in 'Scripting' started by elopez7, Jul 13, 2014.

  1. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    Hello everyone,
    I am currently working in a top down view game and I am stuck with the following.
    I managed to get the player rotate towards the direction it's going, however when there is no input, the player resets back to the original rotation. I want to be able to make the player to face the last direction it was going. (I don't know if I made myself clear). Say the default rotation is towards the right, so if I go up and then release the input, the player will reset its rotation towards the right instead of staying towards the up.
    Thank you.

    Here is the code I am using

    Code (CSharp):
    1. void movement()
    2.     {
    3.         //This will make the player object rotate towards where it's going
    4.         float vertical = Input.GetAxisRaw("Vertical");
    5.         float horizontal = Input.GetAxisRaw("Horizontal");
    6.         Vector3 transVector = new Vector3(horizontal * speed, vertical * speed, 0.0f);
    7.         myTransform.position += transVector;
    8.        
    9.  
    10.         desPos = transVector;
    11.         desPos.z = 0.0f;
    12.         myPos = myTransform.position;
    13.         //desPos.x = desPos.x - myPos.x;
    14.         //desPos.y = desPos.y - myPos.y;
    15.         angle = Mathf.Atan2(desPos.y, desPos.x) * Mathf.Rad2Deg - 90;
    16.         Vector3 rotVector = new Vector3(0,0,angle);
    17.        
    18.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.Euler(rotVector), 5.0f * Time.fixedDeltaTime);
    19.        
    20.         if(vertical != 0|| horizontal != 0)
    21.         {
    22.             anim.SetFloat("speed", 1);
    23.         }
    24.         else
    25.         {
    26.             anim.SetFloat("speed", 0);
    27.         }
    28.     }
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    The reason why it resets is because you are setting transVector based on speed. Use Vector3.MoveTowards instead.
     
  3. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    Thank you for your help, I do understand what you are saying about transVector being set based on speed, but I don't quite understand how to use Vector3.MoveTowards. I will read on it and see what I can find.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Its because of the way you calculate and rotate towards your angle.

    If you have 0 horizontal and 0 vertical, your target direction is (0,0,0). I forget trig, but it looks like youre doing a calculation been current postion to target position to determine what angle your character should be facing, then you start rotating towards that direction. Obviously if that target is Vector3.zero, its gonna rotate that direction..

    easiest fix would be to not rotate if horizontal and vertical are both 0.

    Code (csharp):
    1.  
    2.  
    3. float vertical = Input.GetAxisRaw("Vertical");
    4. float horizontal = Input.GetAxisRaw("Horizontal");
    5.  
    6. if(Mathf.Abs(vertical) > 0 && Mathf.Abs(horizontal) > 0)
    7. {
    8.    //do all your movement & rotation code
    9. }
    10. else
    11. {
    12.   anim.SetFloat("speed", 0);
    13. }
    14.  
    15.  
     
    elopez7 likes this.
  5. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    Thank you very much! This did the trick, however I think that you meant
    to say
    Code (CSharp):
    1. if(Mathf.Abs(vertical) > 0 || Mathf.Abs(horizontal) > 0)
    2. {
    3.    //do all your movement & rotation code
    4. }
    Because when I put what you said, the movement would be tied to pressing two keys at the same time, then I changed the and for or and then it all worked out. Thank you very much!
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    yeah should have been or rather than and

    you figured it out though.
     
    elopez7 likes this.