- Home /
add momentum to mouse movement
ok i am using the built in FPS "mouse look" script and it works the way i need it to, as in i attached the camera to my "tank" and when i move the mouse it moves the tank as well (what i want) but i want there to be momentum in the movements so when for instances i turn right very fast the tank continue turning right for a little bit when i stop moving my mouse, same with looking up/down. please and ty for any help you can give me
AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour { public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F; public float minimumX = -360F; public float maximumX = 360F; public float minimumY = -20F; public float maximumY = 40F; float rotationY = 0F; void Update () { if (axes == RotationAxes.MouseXAndY) { float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); } else if (axes == RotationAxes.MouseX) { transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0); } else { rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0); } }
Answer by Scribe · Mar 02, 2014 at 11:27 PM
Next time you should add a comment to your question rather than an answer. Basically all you want to do is smooth the value of Input.GetAxis("Mouse X/Y")
rather than using its exact value. To do this you can use Mathf.Lerp (LERP = linearly interpolate).
Your code might end up something like:
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float smooth = 2;
float smoothXAxis;
float smoothYAxis;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -20F;
public float maximumY = 40F;
float rotationY = 0F;
void Update (){
smoothXAxis = Mathf.Lerp(smoothXAxis, Input.GetAxis("Mouse X"), Time.deltaTime*smooth);
smoothYAxis = Mathf.Lerp(smoothYAxis, Input.GetAxis("Mouse Y"), Time.deltaTime*smooth);
if (axes == RotationAxes.MouseXAndY){
float rotationX = transform.localEulerAngles.y + smoothXAxis * sensitivityX;
rotationY += smoothYAxis * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}else if (axes == RotationAxes.MouseX){
transform.Rotate(0, smoothXAxis * sensitivityX, 0);
}else{
rotationY += smoothYAxis * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
You can change the variable 'smooth' to slow down the value faster or slower.
Scribe
wow ok thank you very much =], i keep going to the Unity documentation but the way its organized its not easy to find functions like this, i cant search by typing in things like "momentum" perhaps one day in the future a documentation library will work better.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134254im_/https://answers.unity.com/themes/thub/images/avi.jpg)