- Home /
Smooth Camera Movement script with problem.
Well, I seen an script to make camera move smoothly, but, it makes the character have problems to move, and I don't know why.
Here is the script:
//Mouse rotation input
private var rotationX : float = 0;
private var rotationY : float = 0;
//Mouse look sensitivity
public var sensitivityX : float = 2;
public var sensitivityY : float = 2;
//Default mouse sensitivity
public var defaultSensX : float = 2;
public var defaultSensY : float = 2;
//Used to calculate the rotation of this object
private var xQuaternion : Quaternion;
private var yQuaternion : Quaternion;
private var originalRotation : Quaternion;
//Minimum angle you can look up
public var minimumY : float = -60;
public var maximumY : float = 60;
//Number of frames to be averaged, used for smoothing mouselook
public var frameCounterX : int = 35;
public var frameCounterY : int = 35;
//Array of rotations to be averaged
private var rotArrayX = new Array ();
private var rotArrayY = new Array ();
///////////////////////////////////////////////////////
function Start()
{
//Lock/Hide cursor
Screen.lockCursor = true;
if (rigidbody) rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
///////////////////////////////////////////////////////
function Update()
{
//Mouse/Camera Movement Smoothing:
//Average rotationX for smooth mouselook
var rotAverageX : float = 0;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
//Add the current rotation to the array, at the last position
rotArrayX[rotArrayX.length] = rotationX;
//Reached max number of steps? Remove the oldest rotation from the array
if (rotArrayX.length >= frameCounterX)
{
rotArrayX.RemoveAt(0);
}
//Add all of these rotations together
for (var i_counterX = 0; i_counterX < rotArrayX.length; i_counterX++)
{ //Loop through the array
rotAverageX += rotArrayX[i_counterX];
}
//Now divide by the number of rotations by the number of elements to get the average
rotAverageX /= rotArrayX.length;
//Average rotationY, same process as above
var rotAverageY : float = 0;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotArrayY[rotArrayY.length] = rotationY;
if (rotArrayY.length >= frameCounterY)
{
rotArrayY.RemoveAt(0);
}
for (var i_counterY = 0; i_counterY < rotArrayY.length; i_counterY++)
{
rotAverageY += rotArrayY[i_counterY];
}
rotAverageY /= rotArrayY.length;
//Set Rotation Limits for vertical
// if ((rotAverageY >= -360) && (rotAverageY <= 360))
// {
// rotAverageY = Mathf.Clamp (rotAverageY, minimumY, maximumY);
// }
// else if (rotAverageY < -360)
// {
// rotAverageY = Mathf.Clamp (rotAverageY+360, minimumY, maximumY);
// }
// else
// {
// rotAverageY = Mathf.Clamp (rotAverageY-360, minimumY, maximumY);
// }
//Apply and rotate this object
xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
Comment
The FPS Controller already has camera control...I'm not sure, but I think above script is meant for third person...
Camera scripts should not affect the player's movement, maybe you should post your movement script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move camera when mouse is on the edge? 1 Answer
c# scripting navigation problems 1 Answer
Camera Movement issue 1 Answer
character movement follows the direction of camera -1 Answers