using Mathf.SmoothDamp with mouselook

In my project, the player character aims a flashlight based on mouse input (like the camera in a first-person shooter.) I’m trying to use Mathf.SmoothDamp to get a nice smooth feel to the flashlight’s movement, and right now the script i have works beautifully when the flashlight is moving up or to the right but its movement is even more stiff and jerky than usual when moving down or left because it stops instantly when there is no active mouse input. I assume that has something to do with using 0.0f as my starting point, but that’s the only thing I’ve found that approximates the results i want under any situation at all; using a buffered mouse input doesn’t make the rotation noticeably smoother.

Needless to say, I’d like it to be smooth from all directions. I don’t know if a solution doesn’t exist or if it’s head-smackingly obvious, but I’ve spent long enough on it and I thought maybe someone who isn’t completely incompetent at programming/math as I might be willing to take a look.

here’s the most relevant section of the script:

float xSmoothie = Mathf.SmoothDamp(0, xInput, ref xVelocity, smoothTime);
float ySmoothie = Mathf.SmoothDamp(0, yInput, ref yVelocity, smoothTime);


rotationX += xSmoothie * 30;
rotationY += ySmoothie * 30;

and here’s the whole script:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour {
public float sensitivityX = 2F;
public float sensitivityY = 1.8F;

public float minimumX = -41F;
public float maximumX = 43F;

public float minimumY = -30F;
public float maximumY = 30F;

public float smoothTime = 0.01f;

private float xVelocity = 0f;
private float yVelocity = 0f;

float xInput = 0F;
float yInput = 0F;

float averageXInput=0F;
float averageYInput=0F;

float rotationX = 0F;
float rotationY = 0F;

Quaternion originalRotation;

void FixedUpdate ()
{
	// Read the mouse input axis
xInput = Input.GetAxis("Mouse X") * sensitivityX;
yInput = Input.GetAxis("Mouse Y") * sensitivityY;

//Applying the smoothdampangle smoothing

float xSmoothie = Mathf.SmoothDamp(0, xInput, ref xVelocity, smoothTime);
float ySmoothie = Mathf.SmoothDamp(0, yInput, ref yVelocity, smoothTime);


rotationX += xSmoothie * 30;
rotationY += ySmoothie * 30;

rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);

Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}

void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)

rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;

}

public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}

I think Input.GetAxis is already smoothed. You can use Input.GetAxisRaw the same way for a unsmoothed version.

But I think your problem is because there are positive “mouse x” movements and negative, depending on if your moving the mouse left or right on the x axis.

Might need some if “mouse x” > 0 and some if “mouse x” < 0 checks to smooth the different directions.