Smooth Character Rotation

Hi all! For my Character Controller script, I’m making it work with both keyboard and a joystick controller. The rotation with the keys are okay, but it kinda SAPS when going the opposite direction. This is highly noticeable when using the joystick. It’s almost as if the player is really sensitive to the stick rotation, and if the stick is pushed just a bit, the player responds to it.

I’d like there to be a little less sensitivity when using the joystick and to have a smoother rotation. This code snippet controls the rotation. Is there something I can do to make the player rotation smooth?

Control.js

var forward : Vector3 = Camera.main.transform.TransformDirection(Vector3.forward);       
    forward.y = 0;                                                                           
    forward = forward.normalized;                                                           
    var right : Vector3 = Vector3(forward.z, 0, -forward.x);                               
    var vertical : float = Input.GetAxis("Vertical");                                       
    var horizontal : float = Input.GetAxis("Horizontal");                               
   
    var targetDirection : Vector3 = horizontal * right + vertical * forward;               
    targetDirection *= moveSpeed;

    moveDirection.x = targetDirection.x;                                                   
    moveDirection.z = targetDirection.z;                                
   
    if(targetDirection != Vector3.zero){
        var rotation0 = transform.rotation;
        rotation0.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * Time.deltaTime / 0.000005);
        transform.localRotation = rotation0;
    }
    if(canMove)                                                                   
        controller.Move(moveDirection * Time.deltaTime);

If you go to Project Settings->Input->Horizontal/Vertical you can change the joystick sensitivity for those and the joystick deadzone too.

The sensitivity does make the sticks response a bit duller, but it makes my character slow. When I increases his speed, it just returns to the same rotation and speeds, just at a different scale. And exactly what does deadzones do?

Okay. I think I’ve figured it out. The problem I was facing was getting almost too immediate control in the rotation parts of my script. After some tough researching, I had to choose between using Lerp or Slerp functions for smoothing. So, I replaced this:

var rotation = transform.rotation;
rotation.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * Time.deltaTime);
transform.localRotation = rotation;

With this.

var newRotation = Quaternion.LookRotation(targetDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotationSmoothing);

The answer is always simpler than what we think! :wink: