How to make object face the direction it's moving?
Ok, so I have a little character dude and I've set him up to move with WSAD keys. It's a top-down RPG, for the visual.
if(Input.GetKey(KeyCode.W)){
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime, Space.World);
transform.LookAt (Vector3.forward);
}
if(Input.GetKey(KeyCode.S)){
transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime, Space.World);
transform.LookAt (-Vector3.forward);
}
if(Input.GetKey(KeyCode.A)){
transform.Translate (Vector3.left * moveSpeed * Time.deltaTime, Space.World);
transform.LookAt (Vector3.left);
}
if(Input.GetKey(KeyCode.D)){
transform.Translate (Vector3.right * moveSpeed * Time.deltaTime, Space.World);
transform.LookAt (Vector3.right);
}
So obviously I'm using transform.LookAt wrong, because I realize that just points to the point in world space which just kind of makes him twitch and look to the one area. I was previously using a click-move system but that was really getting on my nerves so I changed it up to this, but now I'm not quite sure how to go about making the character face the correct direction he's moving. I mean there are only 8 directions it's possible for him to move, so I could hard code all eight possibilities with exact angles... But that's a bit constrictive and I'm trying to be more thoughtful about my code so I figure instead of having 8 separate checks going on every Update(), I could just point him in his current direction once?
Answer by Arocide · Sep 21, 2017 at 05:52 PM
If you were to make your little character dude look at the new position before translating them they should look to where they will move so for example:
private void Update()
{
float moveVertical = Input.GetAxis("Vertical");
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 newPosition = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.LookAt(newPosition + transform.position);
transform.Translate(newPosition * moveSpeed * Time.deltaTime, Space.World);
}
Would achieve the desired effect, by using Axis instead of Keys directly you can also shorten the code to what is above as you can use the values that the Axis give directly (They are Analogue and such use a float). While this will just drag and drop since by default WASD are already mapped to the virtual axis created by unity you can also change them in the input manager (If for example you'd like for the game to support a controller natively) or through scripts making them very handy. I'd suggest using the virtual axis when possible and they make sense to.
Absolutely awesome answer! Exceeded what I was looking for, thank you!
still, work in 3/14/2019 I would like to thank you so much you save my day
A correct and concise answer hidden deep inside a web search of wrongness!
Your answer
Follow this Question
Related Questions
Sprite rotation clip & ending position 0 Answers
Smooth rotation on animation 1 Answer
RotateAround wont rotate to starting position 0 Answers
Visible Rotation from controller 1 Answer