Search Unity

How to obtain transform forward velocity locally

Discussion in 'Physics' started by Homicide, Apr 15, 2019.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Hi guys, i've reached a bit of a head scratcher, though i'm sure there's a solution i just cant see.

    I have a Hero with a controller set up that uses both right and left stick (gamepad).

    The left stick moves the hero , while the right stick controls the direction the hero is looking.

    I'm now trying to animate the hero, so i thought passing in the z velocity to the animator would help sort out the transforms forward motion, but i realized that Z velocity is not relating to the actual forward facing direction of my hero. The z velocity seems to only be returned as world space. This just doesn't work out for what i need.

    Can anyone point me in direction of a tutorial or anything related to obtaining the forward velocity of an object regardless of which direction it is facing? Thanks in advance.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Use transform.TransformVector or one of its family, it has 3 variants both for local to world and world to local.

    Can also just multiply the rotation with the vector.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Hmm, i think i see what you mean and how to use. Cheers mate.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Use that to transform the velocity from world space to local space and take the z axis of given vector, incase i wasn't being clear.