- Home /
Get which way the player is moving from character controller.move
Hey guys,
I was wondering whats the best way to get which way the character is moving for example is it moving back or forward or strafing left or right.
I tried using if(MoveVector.x <1)
But the problem with that is the player rotates and faces where the camera is facing and that will make it not valid.
This is how im moving my player at the moment.
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical")< -deadZone)
{
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
}
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal")< -deadZone)
{
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
Thanks in Advance.
Answer by MikeNewall · Jun 25, 2014 at 03:35 PM
You can determine the direction based on which keys are pressed.
var x = Input.GetAxisRaw("Horizontal");
var z = Input.GetAxisRaw("Vertical");
if(x > deadZone) Debug.Log("Moving right");
if(x < -deadZone) Debug.Log("Moving Left");
if(z > deadZone) Debug.Log("Moving Forward");
if(z < -deadZone) Debug.Log("Moving Backward");
The guy is rotating so I would guess that won't do in this case.
I thought he meant relative to the character not The world.
Answer by fafase · Jun 25, 2014 at 03:38 PM
Use the world forward vector and compare the player forward vector:
float dot = Vector3.Dot(transform.forward, Vector3.forward);
if(dot > 0.9) // going forward direction
else if (dot < - 0.9) // going opposite to forward direction
else{
Vector3 cross = Vector3.Cross(transform.forward, Vector3.forward);
// This could be the other way around...never remember which order
if(cross.y < 0) // going right
else // going left
}
Thanks for the reply, Would that get you going diagonals? I need it to play the right animation.
If you want diagonals you need to add it. if dot is close to 0.7 (45 degrees) or -0.7 then you are in diagonals. The value of dot is the angle in radians. Then you can just look up a radians table to see where the guy is heading. Using the cross product you get the polarity of the angle.
Your answer
Follow this Question
Related Questions
How can I move character controller in one direction 0 Answers
Transform position of an object at particular angle/direction 3 Answers
Rotation and movement? 1 Answer
Character controller, player y changes when pushing on colliders 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers