Search Unity

Rotate Sprite Based on Speed & Direction of movement

Discussion in 'Scripting' started by tedthebug, Sep 1, 2015.

  1. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Hi,
    I'm hoping that someone can point me towards a solution/tutorial (not 100% code as I like to have a guide but work it out myself from that) on how to have my character rotate to more accurately reflect the movement. What I have is:
    • 2D side scroller with whales/dolphins in it that dive under the water & jump out of the water.
    What I'm trying to do:
    • have them slowly rotate facing up when jumping up (Gradually steeper as they go higher)
    • have them slowly rotate to level as they reach the top of their arc
    • slowly rotate facing down as they fall back towards the water (Gradually steeper as they fall further)
    • continuation of both if they are jumping out of the water from starting below the surface to build up speed or diving further down after jumping out of the water
    • Sort of like a Sin curve.
    I already have the actually movement working how I want, I just want to get the sprite rotating to help convey the movement. I've tried linking rotate to velocity but it's not quite right & looks really weird with incomplete turns.

    I've attached a (bad) image to try & show what I mean. Also the code I had tried that sort of worked best but still looked wrong.

    Code (CSharp):
    1. // change fish angle dfacing up & down depending on direction of movement
    2. //        Vector2 dir = transform.GetComponent<Rigidbody2D>().velocity;
    3. //        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg *Time.deltaTime * 15;
    4. //        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

    Thanks

    Bad Dolphin Image.png
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Thanks, will check it out