- Home /
Face direction to move in
So, in a 3D top-down game, I need the player to face the same direction in which he's moving. I'm moving the player with the arrow keys, and I need it to rotate in the direction that it is going to move in. Up makes it face forward, down backward...etc. Then movement should be in that direction until rotated again. I'm using a CharacterController and a generic non-humanoid rig, so I don't think the standard asset controller will work. Trying : transform.Rotate(new Vector3(0, 180, 0)); Just turns adds a 180 degree rotation onto it's current rotation, so that doesn't work.
Answer by TWicked · Jul 15, 2017 at 03:32 AM
Take a look at this https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character?playlist=17144
Answer by ksfox · Jul 15, 2017 at 04:51 AM
The character in that sample project doesn't always move where he's facing. They use a ray from the camera to get a position on the floor, then use: Quaternion newRotatation = Quaternion.LookRotation (playerToMouse); To get the player model to face it.
Perhaps I could make a ray on/near my character, and move it and then use LookRotation to face it, but I think that there's probably a better way.
you can use this code to face character in direction of rigitbody.velocity vector
transform.rotation = Quaternion.LookRotation(TruckControl.rb.velocity);