Rotation not smooth in third person controller

Hi,

I’m making a third person character controller in C# based on the standard javascript one. So far, my character walks and rotates in the direction I want. However, the rotation is not smooth at all. When I press up my character faces forward, then when I press left, the character immediately faces towards the left. I’d like the character to smoothly rotate. I’ve checked the javascript controller and it’s got nothing that I don’t have that should affect the rotation.

below is my code:

void Update()
	{
		UpdateDirection();

		Gravity();

		// Calculate actual motion
		Vector3 movement= moveDirection * moveSpeed + new Vector3 (0, verticalSpeed, 0);
		movement *= Time.deltaTime;
	
		//move controller and save collisionflags
		cf = controller.Move(movement);
		transform.rotation = Quaternion.LookRotation(moveDirection);
	}



	void UpdateDirection()
	{

		Transform cameraTransform = cam.transform;
		bool grounded = IsGrounded();
		
		
		Vector3 forward= cameraTransform.forward;
		forward.y = 0;
		forward = forward.normalized;
		
		Vector3 right= new Vector3(forward.z, 0, -forward.x);
		float v= Input.GetAxisRaw("Vertical");
		float h= Input.GetAxisRaw("Horizontal");
		
		targetDirection= h * right + v * forward;
		
		if(grounded)
		{
			
			if (targetDirection != Vector3.zero)	
			{
				// If we are really slow, just snap to the target direction
				if (moveSpeed < walkSpeed * 0.8 && grounded)
				{
					moveDirection = targetDirection.normalized;
				}
				else
				{
					moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
					moveDirection = targetDirection.normalized;

				}
			}
			
			float curSmooth= 10.0f * Time.deltaTime;
			float targetSpeed= Mathf.Min(targetDirection.magnitude, 1.0f);
			targetSpeed *= walkSpeed;
			moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
		
		}
	}

Would be great if someone could help :slight_smile:

Thanks, looks pretty good! Convert your comment to an answer so I can accept it :)

–

1 Answer

1

Try replacing transform.rotation = Quaternion.LookRotation(moveDirection); with transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(moveDirection),Time.deltaTime*5);