- Home /
Rotate towards movement direction
I'm basically wanting my character to rotate towards the direction of it's movement so if I press down for my character to move down then my character will rotate towards that direction and the same with any diagonal movement.
Problem is I can't figure out how to do it :(
This is my movement code:
void Movement() {
if (Input.GetKey(KeyCode.W))
{
transform.position = transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position = transform.position += transform.forward * -moveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position = transform.position += transform.right * -moveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position = transform.position += transform.right * moveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space))
{
transform.position = transform.position += transform.up * jumpHeight * Time.deltaTime;
}
}
Can anyone help me?
Comment
Answer by Ksanone · Dec 08, 2015 at 11:55 AM
Maybe use Vector3.RotateTowards where the target vector3 is your transform.position + direction you want to go
http://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
3D Heat Seeking Missile (C#) - Odd problem 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotating Character 1 Answer