- Home /
How to make character face towards movement direction?
For the past couple of days I have been looking through all the unity answers and questions to find a solution to this problem. Now, I have found a sort of solution but it doesn't work perfectly.
Not only does my character not turn go in the direction that it is facing but when I push my "turning buttons" (this causes the player to turn clockwise or counterclockwise) it causes the players to start rolling in a random direction. Lastly, for some odd reason that I can't even fathom why, my up/forward button goes completely to the right. The left goes the direction the forward/up button should go. I have tried starting my player at a different rotation or just moving the camera but it continues to go right.
Any help at this point would be deeply appreciated. Below is my script for my player controller.
{
public GameObject PlayerOne;
public float Movement;
public Rigidbody rb = null;
public float speed = 5.0f;
public float movementSpeed = 5.0f;
public float clockwise = 10.0f;
public float counterClockwise = -10.0f;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey (KeyCode.W)) {
transform.position += transform.forward * Time.deltaTime * movementSpeed;
} else if (Input.GetKey (KeyCode.S)) {
transform.position += Vector3.back * Time.deltaTime * movementSpeed;
} else if (Input.GetKey (KeyCode.A)) {
transform.position += Vector3.left * Time.deltaTime * movementSpeed;
} else if (Input.GetKey (KeyCode.D)) {
transform.position += Vector3.right * Time.deltaTime * movementSpeed;
}
if (Input.GetKey (KeyCode.E)) {
transform.Rotate (0, Time.deltaTime * clockwise, 0);
} else if (Input.GetKey (KeyCode.Q)) {
transform.Rotate (0, Time.deltaTime * counterClockwise, 0);
}
}
}
hey bro, i think you might be a bit mixed up looking at your code. i assume you have a 3D first person point of view?
when you say Vector3.right, that's world direction! it's just like saying "move east reguardless of which way you are facing".
if you want your character to move to move to his right then you say transform.right.
if you put a negative in front of any direction or speed you get the opposite.... so left is simply -transform.right and backwords is simply -transform.forward.
I'm guessing this is what you want for "a" and "d" and "s" keys
(if you are not first person and have a fixed rotation camera, then you would want the global Vector3 directions. )
I tried what you had suggested and changed the Vector3 to transform but now I have compiler errors saying that there is no definition for transform.back or transform.left.
(By the way, if that was literally the only problem with my code and the next answer is super easy and convenient like this answer I might scream because why didn't I think of that.)
i all ready mentioned a soulution to your errors your talking about in my previous instructions. there is no transform.back or transform.left. you use a negative sign with right and forward as i mentioned above!
Answer by CptKen · Oct 18, 2017 at 05:00 AM
Hi,
Instead of directly modifying the position of the transform, try using Transform.Translate with it set to self space.
https://docs.unity3d.com/ScriptReference/Transform.Translate.html
In this case you can use Vector3.forward, Vector3.right etc. instead of transform.forward because the translation will occur in local space and not world space.
If you want the character to always be rotated in the direction of travel you need to use the following
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
Transform.rotation = Quaternion.LookAt(transform.forward, Vector3.up);
If you want it to smoothly rotate towards the direction of travel its a bit more work. You will have to cache the desired rotation and continuously lerp towards instead of setting it directly.
Answer by uk007singh · Oct 18, 2017 at 09:19 AM
Hi Above script mentioned by you is working fine for me.I think you have to set the directions of the character. The character should face the z axis and x axis on the right and y axis to their upward direction.
Answer by Harinezumi · Oct 18, 2017 at 10:24 AM
Hey there!
There are a few things I would change in your code as well as some suggestions for your player object.
First, there is a huge difference between transform.back and Vector3.back/left/right (which you use for everything except W): they are not aligned to your transforms rotation!
You also don't need a Rigidbody unless you want ragdoll effects at one point. The player character should be kinematic anyway.
Finally, a recommendation: put the visual representation of the player into a child game object of your player game object, so that you can rotate it separately from the player in case it is in a different coordinate system as Unity (usually that's the case).
Answer by CreativeNess · Oct 19, 2017 at 03:55 PM
Thank you all for all your answers. I tried each one of them but none of them seemed to work (I tried the Quaternion and got errors, I moved my whole thing to have everything face the z axis but it continued to go right).
Until I decided to do something a little weird/dumb. I changed my W to Vector3.forward instead of transform.forward and it works perfectly fine now. When I hit my turn keys though it still rotates funny but that is something I can probably mess around with and fix another time. Thank you all though for your suggestions, I really appreciate it!
Your answer
Follow this Question
Related Questions
Jaggie, uneven speed with basic movement script. 0 Answers
Move in the direction its facing C# 2 Answers
Make the player face his movement direction 10 Answers
4 way direction movement using a Character Controller. 1 Answer
Character floats on backwards walk 0 Answers