- Home /
[SOLVED]Why is this scripts output so choppy/glitchy?
[UPDATE]So the following script basically is not smooth, and I know there are ways to fix this but I am not entirely sure how...
(end of update)
I am trying to make a camera script that will move the camera horizontally with the mouse while the middle button is being held down, meaning forward left, right, back, and diagonal, but not up and down; What happens is that it works, but is choppy. How can I fix this? Btw, it has to be attached to the camera to work: movecamera.js:
#pragma strict
//NOTE: THIS SCRIPT HAS TO BE ATTACHED TO THE CAMERA TO WORK!
function Update () {
var flySpeed = 1;
if(Input.GetMouseButton(2)){//For middle click
if(Input.GetAxis("Mouse X")<0){//Left
transform.Translate(Vector3.left * flySpeed);
}
if(Input.GetAxis("Mouse X")>0){//Right
transform.Translate(Vector3.right * flySpeed);
}
if(Input.GetAxis("Mouse Y")>0){//Forward
transform.Translate(Vector3.forward * flySpeed);
}
if(Input.GetAxis("Mouse Y")<0){//backward
transform.Translate(Vector3.back * flySpeed);
}
}
}
Answer by yoda12 · Feb 23, 2015 at 12:49 AM
I solved the problem by basically doing what MonkeyHood had said, only by tracking the cameras position and using division. Thanks!
Answer by MonkeyHood · Feb 19, 2015 at 11:47 PM
It's choppy because you always move the same distance every frame.
Vector3.left * flySpeed
I don't really know JS, so you will have to write the code yourself, but here's what you should do.
save the mouse position in a var (lastMousePosition) as soon as the button is pushed
Next frame, if the button is still held down, take the current position and subtract lastMousePosition from it, store it in mouseDistance
If the mouse button is let go, set LastMousePosition to Vector3.zero
Your new Translate() functions should look like this:
transform.Translate(Vector3.left * flySpeed * MouseDistance);
Well, I think I get your point there, but I don't completely understand what you mean, but for instance when you move the camera this way, you keep the middle mouse button held down.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183812im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to smooth my camera (Vector3.Lerp) 0 Answers
Problems with Lerp. 1 Answer
Smooth Camera Follow Script, Weird Movement... Please help! 1 Answer
Smooth Camera Movement 1 Answer
Smooth Camera/Object Movement 1 Answer