Character Movement as Camera Movement

Hi, i need to make player movement script where player is rotating on mouse movement and moving like camera direction. I have problem when player is going backward he is like jumping up… Forward, right and left is ok. Only the this one direction is a problem.

void Start () 
	{
		controller = gameObject.GetComponent<CharacterController>();
		moveDirection = Vector3.zero;
		right = Vector3.zero;
		forward = Vector3.zero;

	}
	

	void Update () {
		forward = Camera.main.transform.forward;
		right = Camera.main.transform.right;
		float horizontalInput = Input.GetAxisRaw("Horizontal");
		float verticalInput = Input.GetAxisRaw("Vertical");
		
		Vector3 targetDirection = horizontalInput * right + verticalInput * forward;
		moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 * Mathf.Deg2Rad * Time.deltaTime, 1000);
		
		Vector3 movement = moveDirection * Time.deltaTime * speed ;
		controller.Move(movement);
		Mathf.Clamp(0,0,0);
	}

You could try making the Camera a child of the Player.

You should try removing the movement script from the camera, let your player handle its movement, and have the camera be a child of the player.

Technically what should happen is that your camera will always be looking where you set it at relative to your player, so if your player turns, the camera will follow.

Another way to see this :

Your camera doesn’t move on its own, It’s being dragged around by the player, its parent.


Aaaah! Now I get it!

It’s because you’re using the camera forward vector. I’m assuming your camera is set a little above ground (above player as well), and that it’s pointing down a little. Which will result in a forward vector pointing down to the ground

What happens is that forward movement will work only because the player can’t go underground cause of physics. But nothing prevents him from going upward when you take the opposite vector, as if he was “sucked in” by the camera.

What you wanna do is nullify movement on an axis - in this case, probably the Y-axis.

So, before calling controller.Move(movement), do this:

movement.y = 0;

So i think none understand my problem ^^ Script is attached to the player. Camera has no script

  1. Camera is static now
  2. I want my player move relative to the camera - he can be rotated (face of the player on the left) then when i push the “w” button he goes forward as camera is set and it is working. Only problem is when he move backward then he goes up up to the camera.