- Home /
Players moves with simple script, but dont look where he moves
Hello all.
Sorry for my stupid question; my little script allows player to move in X and Z axis.
But he don't look where he moves...
public class PlayerMovement : MonoBehaviour { Animator anim; Rigidbody playerRigidbody; Vector3 movement;
void Awake()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
void FixedUpdate()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 15.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 15.0f;
transform.Translate(x, 0, z);
Animating(x,z);
}
}
Answer by Mikael-H · Jan 10, 2017 at 09:09 AM
You only tell the transform to translate in the direction you move. You also need to explicitly tell it to turn towards that direction. You can do this with the LookAt-method which will turn a transform to face a specific position:
transform.LookAt(transform.position + new Vector3(x, 0, z));
EDIT:
Now that your character is turning you also need to take into account that the method signature for Translate() is:
public void Translate(Vector3 translation, Space relativeTo = Space.Self);
This means that if you do not att the second parameter it will assume you want to move in the transform's local space, i.e. positive x axis is the right hand side of your character. If this is not the intended behaviour then you should write this instead:
transform.Translate(new Vector3(x, 0, z), Space.World);
Personally, I prefer to always specify which space to use as I tend to forget which one is the default :)
Answer by rabione · Jan 10, 2017 at 10:12 PM
Thank you so much for your quick reply !
I've tried this script, and now thr player moves only in 2 directions :
With Up or Down button : he moves diagonally to down of the screen. with left or right : he moves diagonally to up of the screen.
(if you imagine what i'm sayin')
Thank you and sorry for this question.
It's because the translate method has a parameter, which asks if you want to translate the object in Local Space, or in World Space. By default it's set to Space.Self.
transform.Translate(x, 0, z, Space.World);
this should fix your problem, I think.
@Scoutas is absolutely right, I added some explanation in my answer. If this works for you then please mark the answer as correct. Also, please do not add an answer if you need clarification, add it as a comment or edit the original question. That way it will be easier for someone else who has the same problem in the future to find the correct answer to the question.
Thank you so much!
With these 2 methods, the player keeps moving diagonally:
Not Up/down/left/right, but diagnoally...
Sorry and thank you for your time!
Now it looks like this :
void FixedUpdate() {
var x = Input.GetAxis("Vertical") * Time.deltaTime * 20.0f;
var z = Input.GetAxis("Horizontal") * Time.deltaTime * 20.0f;
transform.Translate(new Vector3(x, 0, z), Space.World);
transform.LookAt(transform.position + new Vector3(x, 0, z));
Animating(x,z);
}
What happens if you comment out Animating(x,z) ? $$anonymous$$aybe there is an issue with root motion being turned on in the animator.
It simply stops player's animation... He walks diagonally without "walk" motion.
As the code should be correct, and it's not the animator settings then I suspect your input settings might be wrong. Can put their values in a Debug.Log statement and see that you actually get the vvalues through controls that you expect?
I don't know how to do this x) (debug log and stuff...)
I will check my default inputs in Unity.
$$anonymous$$ind posting your code here again?
I would love to see the inspector view of the player as well.
I just implemented this code into my own scene and it works perfectly (even the rotations look pretty). Anyway, just elaborate a little bit more, tell me what's attached to what and post the movement script in full.
The script has no dependencies.
It runs on a 3rd person shooter. The player is freezed in Y position and X,Z rotation.
The inputs in Unity are correct.
The player now moves like this :
http://img15.hostingpics.net/pics/899525Sanstitre.jpg
Here's the full script :
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { Animator anim; Rigidbody playerRigidbody; Vector3 movement;
void Awake()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
void FixedUpdate()
{
var x = Input.GetAxis("Vertical") * Time.deltaTime * 20.0f;
var z = Input.GetAxis("Horizontal") * Time.deltaTime * 20.0f;
transform.Translate(new Vector3(x, 0, z), Space.World);
transform.LookAt(transform.position + new Vector3(x, 0, z));
Animating(x,z);
}
}
The reason I ask, if the camera is rotated, is because with translating in the world space, the player moves along the worlds x, y and z axis. If you camera is rotated by 45 degrees, that's the behaviour I would expect to be happening.
I understand.
But the camera is not rotated. It faces the back of the player (when he's not moving) perfectly.
And why the Right and Left button are inverted + diagnoal?
I'm stuck at this point :/
It is extremely weird. What about trying to create a new scene and just applying this script to a simple cube object and see if it works? Because it worked for me, I have no clue why it wouldn't work for you.
Your answer
Follow this Question
Related Questions
3rd person carrying objects,3rd person picking up objects 0 Answers
Create clip button not showing. 0 Answers
Object is not following parent bone. 0 Answers