#CharacterController only moving on the X axis?

1 messages · Page 1 of 1 (latest)

sudden falcon
#

I tried making a CharacterController Fps controller and it behaves like this:

zinc kernel
#

can u send the code

sudden falcon
#
public class PlayerMovement : MonoBehaviour
{
    public GetInput getInput;//another script where i gather all the values for the new input system
    public CharacterController controller;

    public float speed;
    public float acceleration;

    float horizontal;
    float vertical;
    private void Update()
    {
        Vector2 input = getInput.movement;//WASD vector2

        Vector3 velocity =
            transform.forward * input.y * Time.deltaTime +
            transform.right * input.x * Time.deltaTime;

        horizontal = Mathf.Lerp(horizontal, velocity.x, acceleration * Time.deltaTime);
        vertical = Mathf.Lerp(vertical, velocity.y, acceleration * Time.deltaTime);

        Vector3 movement = new Vector3(horizontal, 0, vertical);

        controller.Move(movement * speed);
    }
}
zinc kernel
#

This code only makes the character move on the X-axis because of the code in line:

Vector3 movement = new Vector3(horizontal, 0, vertical);

the Y component of the movement vector is set to 0. Therefore, the character will only move horizontally along the X-axis and vertically along the Z-axis.

If you want to allow movement in the Y direction as well, you can modify the movement vector like this:

Vector3 movement = new Vector3(horizontal, input.y * Time.deltaTime, vertical);

This will add the input in the Y direction to the movement vector.

sudden falcon
#

well, actually i want it to move on the z axis instead of the y

#

in the video i tried moving in all directions but it only moves along the x..

zinc kernel
#

try this

public class PlayerMovement : MonoBehaviour
{
    public GetInput getInput; //another script where i gather all the values for the new input system
    public CharacterController controller;

    public float speed;
    public float acceleration;

    private void Update()
    {
        Vector2 input = getInput.movement; //WASD vector2

        Vector3 velocity = transform.forward * input.y * Time.deltaTime +
                            transform.right * input.x * Time.deltaTime;

        Vector3 moveDirection = new Vector3(velocity.x, 0, velocity.z);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        controller.Move(moveDirection * Time.deltaTime);
    }
}
#

the velocity vector is calculated just as before, but the horizontal and vertical variables are not used.

Instead, we create a new moveDirection vector which takes into account the velocity vector and the player's orientation. We transform this vector into world space using transform.TransformDirection, and then multiply it by the speed variable to get the final movement vector.

Finally, we use controller.Move to move the character using the moveDirection vector. This allows the character to move freely in any direction based on the WASD input.

sudden falcon
#

I tried your code but then the charactercontroller didn't move at all..

but then i tried adding the horizontal/vertical floats again and tweaked it like this:

Vector3 moveDirection = new Vector3(velocity.x, 0, velocity.z);
        horizontal = Mathf.Lerp(horizontal, moveDirection.x, acceleration * Time.deltaTime);
        vertical = Mathf.Lerp(vertical, moveDirection.z, acceleration * Time.deltaTime);
        Vector3 movement = new Vector3(horizontal, 0, vertical);

        controller.Move(movement);
#

now it works perfectly fine,
thank you for your time!