- Home /
Mouselook.js don't move smooth, its jittery
Hi, I rewrite the Mouselook.cs into my Mouselook.js. Also I figured out that everything worked fine with my solution:
public var sensitivityX : float = 15; public var sensitivityY : float = 15;
public var minimumX : float = -360; public var maximumX : float = 360;
public var minimumY : float = -60; public var maximumY : float = 60;
private var rotationX : float = 0; private var rotationY : float = 0;
private var xQuaternion : Quaternion; private var yQuaternion : Quaternion;
private var originalRotation : Quaternion;
function Start (){ if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; }
function Update(){ rotationX += Input.GetAxis("Mouse X") sensitivityX; rotationY += Input.GetAxis("Mouse Y") sensitivityY; if ((rotationX >= -360) && (rotationX <= 360)){ rotationX = Mathf.Clamp (rotationX, minimumX, maximumX); }else if (rotationX < -360){ rotationX = Mathf.Clamp (rotationX+360, minimumX, maximumX); }else{ rotationX = Mathf.Clamp (rotationX-360, minimumX, maximumX); }
if ((rotationY >= -360) && (rotationY <= 360)){
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
}else if (rotationY < -360){
rotationY = Mathf.Clamp (rotationY+360, minimumY, maximumY);
}else{
rotationY = Mathf.Clamp (rotationY-360, minimumY, maximumY);
}
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
The script is connected to the MainCamera (FirsPersonController is parent > Graphics and Main Camera are children). But after a few tests i figured out that the camera don't move smooth. Everything seems to judder when I move the mouse.
-> I also recognized this with the standard MouseLook.cs. Any ideas what could evoke this?
I just started my project all over again and use the "first person controller" prefab - so far everything works fine.
Answer by qJake · Mar 29, 2010 at 06:02 PM
If the original MouseLook script was also "jittery" (the word you were probably looking for), then it's most likely a problem with your specific machine. I've had no problems in the past with using the MouseLook script and having it lag.
You should check to make sure that your computer isn't running anything in the background that may interfere with Unity, and also check to make sure your computer has enough resources to run Unity smoothly.
$$anonymous$$aybe I expressed it wrong. Walking works without any problem. Even looking exactliy along the x-axis works fine. But any chance to the y-axis seems to jitter (thanks ;) Just the y-axis "calculation" seem to went wrong
Answer by Graham 1 · Nov 08, 2010 at 10:25 PM
Hi, I've been playing around trying to write a better mouse look function and came up with this. Delete the standard mouse look script from the first person controller parent object and also from the child camera. Attach the following script to the child camera only. It will control both the camera (for looking up and down) and also the parent object (for rotating left and right). It uses the Quaternion Lerp function to achieve smooth rotation and uses only a few lines of code.
private var xtargetRotation:float=10; private var ytargetRotation:float=10; var xSensitivity:float=10; var ySensitivity:float=10; var smoothing=2; var min=-60; var max=60;
function Update () {
var yAxisMove:float=Input.GetAxis("Mouse Y")*ySensitivity; // how much has the mouse moved? ytargetRotation+=-yAxisMove; // what is the target angle of rotation ytargetRotation=ytargetRotation % 360; ytargetRotation=Mathf.Clamp(ytargetRotation,min,max);
var xAxisMove:float=Input.GetAxis("Mouse X")*xSensitivity; // how much has the mouse moved? xtargetRotation+=xAxisMove; // what is the target angle of rotation xtargetRotation=xtargetRotation % 360;
transform.localRotation=Quaternion.Lerp(transform.localRotation,Quaternion.Euler(ytargetRotation,0,0),Time.deltaTime*10/smoothing); transform.parent.rotation=Quaternion.Lerp(transform.parent.rotation,Quaternion.Euler(0,xtargetRotation,0),Time.deltaTime*10/smoothing); }
great code!
this is an improvement on mouselook, camera moves so smoothly.
This is a superb solution for a smooth FPS camera. I find that if you make the smoothing 0.5 or so, it makes the camera suitably accurate (not so much lag as a result of the smoothing), but it still feels much smoother than the default camera.
Answer by pyro · May 20, 2010 at 11:25 AM
Put all of your actual camera movement code into LateUpdate() so that it's locked at being updated every 1 frame
function LateUpdate()
{
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
Since yours is in the Update() it's being called many times per frame which is causing it to look jittery.
It's still worth doing camera positioning in late update: it'll more or less assure that you're not positioning based on information which is updated later in the frame.
i.e. if camera.Update is called before cameraTarget.update, you're going to be looking at where cameraTarget WAS.
LateUpate assures that all the movement you've done is processed before positioning the camera.
Answer by Ryan-Gatts · Feb 02, 2014 at 08:45 PM
For anyone who wants it, I translated this into C# on my way to modifying it for my own game. I hope this is helpful to somebody :) using UnityEngine; using System.Collections; using System.Collections.Generic;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour
{
public float xSensitivity = 10f;
public float ySensitivity = 10f;
public float smoothing = 1f;
public float min = -60f;
public float max = 60f;
float xTargetRotation = 10f;
float yTargetRotation = 10f;
void Update ()
{
float yAxisMove = Input.GetAxis("Mouse Y")*ySensitivity; //how much has Mouse Y moved?
yTargetRotation += -yAxisMove; //what is the new target rotation?
yTargetRotation = yTargetRotation % 360; //get the remainder of targetRotation from 360
yTargetRotation = Mathf.Clamp (yTargetRotation,min,max);
float xAxisMove = Input.GetAxis ("Mouse X")*xSensitivity; //how much has Mouse X moved?
xTargetRotation += xAxisMove; //what is the new target rotation?
xTargetRotation = xTargetRotation % 360; //get the remainder of targetRotation from 360
transform.localRotation = Quaternion.Lerp (transform.localRotation, Quaternion.Euler(yTargetRotation,0,0), Time.deltaTime*10/smoothing);
transform.parent.rotation = Quaternion.Lerp (transform.parent.rotation, Quaternion.Euler(0,xTargetRotation,0), Time.deltaTime*10/smoothing);
}
}