- Home /
Mobile Character controller Moving in world space
So i have been attempting and looking for answers about this for the last week and i finally gave up. My last resort is this place. Im creating a third person mobile game, The camera is a separate gameobject of the player and follows the player around.
I calculate the movement direction of the player by looking at the Joysticks vertical and horizontal input. I Than use this direction to rotate the player in that direction.
The problem is that if i rotate to the right. and than i take my finger off the screen and rotate up. The character will reset to the state he was in prior to rotating right. What i want is for the the player to be able to rotate into any direction and walk forward into that direction. Its really hard to explain so i made a gif. Giphy link of Movement Notice how when i hold the joystick back, the player goes the other way.
Here is the code for controlling it
// ===== INPUT
private void LateUpdate()
{
MovementDirection = (new Vector3(variableJoystick.Horizontal, 0, variableJoystick.Vertical));
}
//ROTATION ========
else if (MovementDirection != Vector3.zero & Dashing == false) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(MovementDirection.normalized), 0.2f);
StartRotation = transform.rotation;
}
//MOVEMENT ========
if (!_controller.isGrounded)
{
MovementDirection.y -= Gravity * Time.deltaTime;
}
if (!Dashing)
{
_controller.Move(MovementDirection.normalized * Time.deltaTime * speed);
}
I have tried putting the player into another gameobject and rotating that gameobject aswell, but that didnt work so i thought he must be moving in world space so i tried local space and still did not work.
I know what the problem is, its cuz the horizontal and vertical axis always just point toward one direction. and back to zero. So i have to somehow rotate the axis. but no idea how.
Answer by Bieere · Aug 11, 2019 at 04:59 PM
The issue is in your movement & Rotation code. The input that you're getting is correct but you need to separate the rotation and movement solving.
Your X value from Movement direction should rotate your character on its respective Vector3.up axis. Theoretically, the values for MovementDirection.x will fall between -1,1 so you can use that like so:
//Rotation Code. You dont have to use Eulers, just using for simplicity
Vector3 rotation = transform.localEulerAngles;
rotation += MovementDirection.x * rotationSpeed * Time.deltaTime;
transform.localEulerAngles = rotation;
//Movement code, make sure you use your forward vector after rotating your character
Vector3 position = transform.position;
position += MovementDirection.y * transform.forward.normalized * speed * Time.deltaTime;
Your movement will use something similar, where you have your y value that again will fall between -1,1.
The important thing to remember is to set your rotation, then you can make use of the normalized forward vector to send your character in the direction that you've rotated them in.