I’m actually quite sure this was asked before, but I wasn’t able to find a solution that fits my needs.
Basically, I need some help with writing a script that makes my player look towards the direction he’s walking to. (Like in Dark Souls, Uncharted or most other 3rd Person Games)
I’m using Input.GetAxis for getting the input from my Keyboard and my Controller. Additionally, I’ve got a Camera i can move around the player. Therefore, i need to consider the current camera rotation before I apply movement to my player, because pushing forward should move my player different if my camera is looking at my player at a different angle.
Some variables like ‘player’ and ‘camera’ assumes you have a reference to them.
Horizontal is a float that is movement along the X Axis
Vertical is a float that is movement along the Y Axis
Rotation
Vector3 direction = new Vector3(horizontal, 0.0F, vertical);
Quaternion rotation = player.rotation;
if(direction.magnitude > 0.01F) {
direction = camera.transform.TransformDirection(direction);
direction.y = 0.0F; // we don't care.
rotation = Quaternion.LookRotation(direction, Vector3.up);
}
So TransformDirection takes local facing Vector and transforms it into a World Vector relative to its Forward Vector. So since you want to move forward relative to the camera thats how you do it. Use the rotation variable to rotate your player. For movement, this is easy too.
In the meantime I managed to somewhat solve this problem as well, but your approach is much nicer.
What i did was creating a new empty GameObject, set it to 0,0,0 and made it a child of my Camera. After that, I added my direction Vector (horizontal, 0, vertical) to the empty GameObject’s position and reset it upon releasing my keys. Then, i could use transform.LookAt(thatGameObject) to make my player look at that gameObject.
Well, this was working. What bugged me was, I wasn’t able to smooth the rotation while using LookAt(). At least not with some extra code.
So, thanks again for your help I use your approach now
Cheers.
EDIT:
For others who are seeing this:
Don’t forget to add “transform.rotation = rotation” after calculating the rotation