I changed some of my code to make it where the character looks at and moves based on mouse movement, but now my gravity doesn’t, and jumping doesn’t work anymore. I think it has something to do with the velocities being equal to the camera, but I don’t know how to fix it without making the character rotation not work anymore.
CharacterController controller;
Vector3 movementDirection;
bool isGrounded;
public float speed = 5f;
public float gravity = 9.8f;
public float jump = 5f;
public float turnSmoothTime = .5f;
public float turnSmoothVelocity;
public Camera camera;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
Movement();
Gravity();
controller.Move(movementDirection * speed * Time.deltaTime);
}
public void Movement()
{
float horizontalMovement;
float verticalMovement;
horizontalMovement = Input.GetAxisRaw("Horizontal");
verticalMovement = Input.GetAxisRaw("Vertical");
controller.transform.Rotate(0, Input.GetAxis("Mouse X"), 0);
Vector3 movementInput = Quaternion.Euler(0, camera.transform.eulerAngles.y, 0) * new Vector3(horizontalMovement, controller.velocity.y, verticalMovement);
movementDirection = movementInput.normalized;
if(Input.GetButtonDown("Jump"))
{
movementDirection.y = jump;
}
Debug.Log(movementDirection);
}
public void Gravity()
{
if (controller.isGrounded)
{
movementDirection.y -= -.5f * Time.deltaTime;
}
else movementDirection.y -= gravity * Time.deltaTime;
}