Character Controller: Faster Diagonal?

I have a character controller that I’ve written for my game. Here is an abridged version: (I’ve taken out things like sounds, health, and energy)

function LateUpdate () {
	var controller : CharacterController = GetComponent(CharacterController);
	var cameraTransform = Camera.main.transform;
	
	var forward = cameraTransform.TransformDirection(Vector3.forward);
	forward.y = 0;
	forward = forward.normalized;
	
	var right = Vector3(forward.z, 0, -forward.x);
	
	var v = Input.GetAxisRaw("Vertical");
	var h = Input.GetAxisRaw("Horizontal");
	
	if (Mathf.Abs(v) + Mathf.Abs(h) != 0) isMoving = true;
	else isMoving = false;
	
	if (controller.isGrounded) {
		moveDirection = h * right + v * forward;
		var targetSpeed = Mathf.Min(moveDirection.magnitude, 1.0);
		moveDirection *= targetSpeed;
		moveDirection *= speed;
		moveDirection = Vector3.RotateTowards(moveDirection, moveDirection, 500 * Mathf.Deg2Rad * Time.deltaTime, 1000);

		if (moveDirection != Vector3.zero) transform.rotation = Quaternion.LookRotation(moveDirection);
	}
	
	moveDirection.y -= gravity * Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
}

When I move diagonaly relative to the camera, my player goes faster than forward. Any suggestions?

Im thinking you need to normalize your moveDirection after you calculate the forward and right. Normalize it then multiply it by the move amount at the end.

1 Like

Works great. Thanks!