I’m using WASD to change rotation of a continuously moving object. The rotations change the direction of the object and the object continues to move in that direction. The movement of the object in all directions is working alright but when I press A to make the object go left, the object does go left, but the rotation makes the object face downwards. How can I fix this ?
Note: The level is rotated 90degrees and hence, W doesn’t necessarily correspond to forward, but instead corresponds to Vector3.left, which is up.
transform.position += transform.forward * speed * Time.deltaTime;
if (Input.GetKey (KeyCode.W)) {
gameObject.transform.rotation = Quaternion.FromToRotation (Vector3.forward, Vector3.left);
}
if (Input.GetKey (KeyCode.A)) {
gameObject.transform.rotation = Quaternion.FromToRotation (Vector3.forward, Vector3.back);
}
if (Input.GetKey (KeyCode.S)) {
gameObject.transform.rotation = Quaternion.FromToRotation (Vector3.forward, Vector3.right);
}
if (Input.GetKey (KeyCode.D)) {
gameObject.transform.rotation = Quaternion.FromToRotation (Vector3.forward, Vector3.forward);
}