- Home /
How to rotate character to movement direction
I am making a top down movement script and I need my character to rotate in the direction I am running. I can't use transform.rotate because I need the character to always rotate to the left when i press "A" and so on. Here is my code so far:
#pragma strict
var movementSpeed = 0.5;
function Start () {
}
function Update () {
//WASD MOVEMENT
if(Input.GetKey(KeyCode.A)) {
transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.D)) {
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.S)) {
transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}
//ARROW KEY TURNING
if(Input.GetKeyDown(KeyCode.A)) {
//transform.Rotate(Vector3.back, Space.World);
transform.rotation.x += 1;
}
if(Input.GetKeyDown(KeyCode.D)) {
//transform.Rotate(Vector3.back, Space.World);
}
if(Input.GetKeyDown(KeyCode.W)) {
//transform.Rotate(Vector3.back, Space.World);
}
if(Input.GetKeyDown(KeyCode.S)) {
//transform.Rotate(Vector3.back, Space.World);
}
}
Answer by robertbu · Sep 20, 2014 at 07:49 AM
One solution is to save the position of the previous frame and use the change between the frames as a direction. Below is an untested rewrite to your rotation code. It assumes 1) 2D (since you are moving on the XY plane, and 2) that your sprite or quad has the front side on the right when the rotation is (0,0,0).
#pragma strict
var movementSpeed = 0.5;
private var prevPos : Vector3;
function Start () {
prevPos = transform.position;
}
function Update () {
//WASD MOVEMENT
if(Input.GetKey(KeyCode.A)) {
transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.D)) {
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}
if(Input.GetKey(KeyCode.S)) {
transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
}
if (prevPos != transform.position) {
dir = transform.position - prevPos;
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
prevPos = transform.position;
}
}
Note that this code causes an immediate rotation change. You could add Slerp()ing code, but then the object would be out of alignment for some period of the move in a new direction.
@robertbu - thanks so much! Out of curiosity, how would I change the code if my sprite were facing down when the rotation is (0, 0, 0)?
The best solution is to rotate the texture in your favorite image editor, but I believe you can subtract 90 from the angle passed to AngleAxis:
transform.rotation = Quaternion.AngleAxis(angle - 90.0, Vector3.forward);
Answer by drudiverse · Sep 20, 2014 at 07:47 AM
Hi there, First of all, you should use EulerAngles to rotate your char NESW north east south west, or never eat shredded wheat whichever you prefer....
A = transorm.eulerAngles = (0,0,0)
or player.transform.rotation = quaternion.eulerAngles (0,90,0)
A = transorm.eulerAngles = (0,90,0)
A = transorm.eulerAngles = (0,180,0)
A = transorm.eulerAngles = (0,360,0)
so you have the quaternion rotations that you want in easy format. then you use Slerp to rotate between current angle and new one, and try different lerp speeds to find the right one.
you can also use transform.lookAt (0,99999,0) if you want, to point your player to different angles on screen.
@drudiverse - a solution along your lines was my first thought, but the way the code is written now, he can press two keys at the same time resulting in diagonal movement.
Your answer
Follow this Question
Related Questions
Change gravity on button 1 Answer
2D: Rotating to face mouse position with two fixed axes 1 Answer
Basic 2D cannon rotation code not working 1 Answer
Uni2D Play() - Misunderstanding 3 Answers