- Home /
Make the player face his movement direction
I've just made a few changes to my code:
void Update () {
ControllPlayer();
}
void ControllPlayer()
{
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.rotation = Quaternion.LookRotation(movement);
transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
if(Input.GetButtonDown ("Fire1"))
{
animation.Play ("attack-01");
}
}
Now the only thing that's wrong with it is, that when I stop pressing the movement keys on the gamepad, the Player turns automatically in the forward direction. I would like him to stay as he was when moving. Is there a quick fix to this?
This line here:
transform.rotation = Quaternion.LookRotation(_movement.normalized);
Is a pretty standard way of getting an object to face the move direction (don't need '.normalized'). How is it failing? If the rotation of your character is (0,0,0), is the front side facing positive 'z'?
how about
if(moveHorizontal != 0 && moveVertical != 0)
{
transform.rotation = Quaternion.LookRotation(movement);
}
Using this I cannot turn 360 degrees with the player, do you have the same issue?
thanks for the idea - I can't believe how easy this was to fix :) ...althought...you got one thing wrong. the correct way is this:
if(moveHorizontal != 0 || moveVertical != 0)
{
transform.rotation = Quaternion.LookRotation(movement);
}
Answer by PaxForce · Oct 10, 2014 at 04:03 PM
the problem was that I was moving in Space.Self instead of Space.World. fixed and done.
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.rotation = Quaternion.LookRotation(movement);
transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
How did you end up exactly fixing this? I'm still stuck with this problem and the code you posted here is the same as the one in your Question. I don't get how to set my player to move in space,world ins$$anonymous$$d of space.self
whenever i do this my character rotates 90 degrees along the Z axis does anyone have a fix??
THAN$$anonymous$$S A LOT!!! it works like a charm :) however you could use the slerp to smooth it out.
Thanks dude! i had no idea you can use .LookRotation();, but now i do. This really helped me progress as a developer, i'm new.
I have a problem, it tells me that "movementspeed " is not in the actual context. Im new btw
Answer by hellopeople0004 · Sep 17, 2016 at 11:23 PM
Sorry for the necropost, but I am sure a lot of people are probably stumbling across this question. I myself a few hours ago found this question while searching for "How to make the player face movement direction" Now I noticed this question is already solved, and the answer works great. After doing a bit of research on Slerp, I made it so the script (that PaxForce made) will smoothly turn instead of doing a sharp turn when rotating.
The way it was done was by replacing transform.rotation = Quaternion.LookRotation(movement);
with transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
If anyone wanted to know how it works, Quaternion.Slerp takes 3 arguments: 2 Quaternions and 1 Float. It interpolates the rotation between the 2 Quaternions with a speed of the Float value (0.0 is no movement while 1.0 is Instant Movement.) The code I did interpolates the rotation between the Current Rotation (transform.rotation) and the Movement Rotation (Quaternion.LookRotation) with a speed of 0.15F.
Hope this helped someone out there.
I was looking all over the place for this solution. Works like a charm. Not sure why so many turning examples and tutorials didn't mention this.
Thanks my dude! It's much better now. looks pretty smooth, especially when you turn 180 degrees.
Answer by ThreeYams · Sep 19, 2016 at 04:58 PM
@hellopeople0004 Your answer was right on time. Works perfectly.
Here's my end result. Nice and smooth.
if(movement != Vector3.zero) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);
Thanks! I couldn't stop my object from rotating back to the default position.
This helped me not have the object rotate back to the default position as well. Thank you.
My camera was rotated, so in case someone also wants the code to work along a rotated camera, here is my full code:
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 movementRotated = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * movement;
if (movement != Vector3.zero) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementRotated.normalized),
0.2f); transform.Translate(movementRotated movementSpeed Time.deltaTime, Space.World);
You can replace Camera.main.transform.eulerAngles.y with whatever angle you want (e.g. '30') depending on how your camera is rotated.
Answer by daavid1231 · Oct 10, 2017 at 03:27 PM
@PaxForce just put this line transform.rotation =Quaternion.LookRotation(movement); into an if statement
if (movement != Vector3.zero) {
transform.rotation =Quaternion.LookRotation(movement);
}
this works for me.
Answer by NickP_2 · Oct 06, 2014 at 12:56 PM
A way to handle this is to set the transforms forward to your normalized move direction, then when he has to walk, add a Vector3.forward force times the speed. For example:
void Update()
{
Vector3 dir = <your movedirection (_movement)>.normalized;
transform.forward = dir;
transform.Translate(Vector3.forward * <speed (_movementspeed)> * Time.deltaTime)>);
}
GL! :)
dude...did you try this piece code out? because it's not working for me.