x


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;
}
more ▼

asked Jul 26 '12 at 06:29 PM

williampigmeu gravatar image

williampigmeu
76 4 13 19

What script do you use for the player's movement?

Jul 26 '12 at 06:38 PM Piflik

FPS Controller

Jul 26 '12 at 06:46 PM williampigmeu

The FPS Controller already has camera control...I'm not sure, but I think above script is meant for third person...

Jul 26 '12 at 06:51 PM Piflik

Camera scripts should not affect the player's movement, maybe you should post your movement script.

Jul 26 '12 at 07:15 PM SomeGuy22
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2999
x1367
x1044
x983
x207

asked: Jul 26 '12 at 06:29 PM

Seen: 657 times

Last Updated: Jul 26 '12 at 07:15 PM