Search Unity

Help - 2D sprite change from direction

Discussion in 'Scripting' started by nevaran, Oct 23, 2017.

  1. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Hello, im trying to make a controller ,where the sprites are changed based on which direction the player is moving to- but I cant figure out the math and animator settings I need to setup for it to work.

    My current problem is when rotating from one to other direction works one way, but when going the other, the values of the angle variable do not stay the same(would be a negative number for example)

    Any help would be greatly appreciated!

    My current way is this:
    Code (CSharp):
    1. Vector2 v = rigid.velocity;
    2.    angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
    3.  
    4.        legAnim.SetFloat("Rotation", angle);
    And here is the setup in the animator:

     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Atan2 returns a 0 to 360 value. Rather than making it expect a negative, why not change your "threshold" values to match your actual expected output of the function?
     
  3. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Actually its using 180 -180 range: