Home > Enterprise >  CharacterController.Move Jumping Issue
CharacterController.Move Jumping Issue

Time:05-27

I have been using CharacterController.Move to control my fps character. I have ran into a problem where my character does small jumps when I press w or s and look down or up respectively. My code:

using UnityEngine;
using Unity.Netcode;
public class PlayerMovement : NetworkBehaviour
{    
    public CharacterController controller;
     
    public float speed = 12f;

    // Update is called once per frame
    void Update ()
    {
      if(IsOwner)
      {

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
      
        Vector3 move = transform.right * x   transform.forward * z;
        controller.Move(move.normalized * speed * Time.deltaTime);
      }
    }
}

I cannot find anything on the matter online, so I came here. I hope you can help me :)

CodePudding user response:

The following line of code is using the transform's rotation to determine direction of movement. If you look up or down, then your transform.forward is also likely directed up or down.

   Vector3 move = transform.right * x   transform.forward * z;

So when you look up and apply a positive z value, your movement is going to move you forward (in the direction you are looking, which is actually up). Due to gravity, you are likely making tiny "jumps" as your character moves up and is pulled down by gravity.

There are a few ways to correct for this, but one potential fix would be to remove the y component of your move vector:

    move.y = 0f;
  • Related