transform.Translate Causing Jittery Camera Movement
Hello,
I'm working on a small game that has a perspective camera but looks straight down Z for the most part. I've made a small camera panning script that works functionally but I've found a couple of issues I'd like to understand more and address:
At higher pan speeds, the camera starts shaking / juddering more. panSpeed is 40f by default and it works okay but you can definitely notice it. Putting it up to 60 or 70 can cause the camera to fly off.
I find the position of the mouse when the left button is held down and find the difference in distance as it moves. I apply this offset to the camera to move it. If I swap these around, the camera movement is inversed but the juddering at higher speeds seems to be gone. You can see this when you swap the comments on :
//Vector3 newOffset = origPanPos - oldPanPos; Vector3 newOffset = oldPanPos - origPanPos;
Can anyone help my understand the issue?
Thanks
using UnityEngine;
using System.Collections;
public class CameraPanAndZoom : MonoBehaviour {
public GameObject targetDepth;
private float screenDepth;
private Vector3 origPanPos;
private bool isPanning;
private Vector3 oldPanPos;
public float panSpeed = 40f;
void Start(){
screenDepth = Mathf.Abs(transform.position.z - targetDepth.transform.position.z);
}
void Update(){
if(Input.GetButtonDown("Fire1")){;
oldPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
isPanning = true;
}
if (isPanning){
origPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
//Swapping origPanPos and oldPanPos inverses the camera control but gets rid of the judder
//Vector3 newOffset = origPanPos - oldPanPos;
Vector3 newOffset = oldPanPos - origPanPos;
transform.Translate(newOffset * panSpeed * Time.deltaTime, Space.Self);
oldPanPos = origPanPos;
}
if (Input.GetMouseButtonUp(0)){
isPanning = false;
}
}
}
Answer by Asgardr · Nov 08, 2015 at 12:14 PM
You forgot to take into account the transform.Translate when saving the old pan position. This should fix it:
oldPanPos = origPanPos + ( newOffset * panSpeed * Time.deltaTime );
Brackets added for clarity. Also it might be a good idea to save the move vector and apply it in the translate and this code snippet, instead of calculating it twice.
Thanks for the response but I'm still seeing the jerkyness. It does feel slightly better though and I did need to add what you suggested. panSpeed at 40 feels a lot better, but at 60 you can still see the jerkyness quite easily.
Weirdly, when I switch the comments on the lines 28 and 29 after implementing your suggestion, the controls are inversed and the movement is jerky on that now. Without your suggestion, the inversed controls works smoothly so I have a feeling it's something to do with how I get the difference in mouse positions?
$$anonymous$$y bad! I've edited my answer. Could you see if it works correctly now?
Thank you Asgardr, that's made a big difference! It feels good now. $$anonymous$$y final code is below just in case anyone wants to try it out. I appreciate the help.
using UnityEngine;
using System.Collections;
public class CameraPanAndZoom : $$anonymous$$onoBehaviour {
public GameObject targetDepth;
private float screenDepth;
private Vector3 origPanPos;
private bool isPanning;
private Vector3 oldPanPos;
private Vector3 newOffset;
public float panSpeed = 1f;
public float panRotationSpeed = 2f;
void Start(){
screenDepth = $$anonymous$$athf.Abs(transform.position.z - targetDepth.transform.position.z);
}
void Update(){
if(Input.GetButtonDown("Fire1")){;
oldPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
isPanning = true;
}
if (isPanning){
origPanPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenDepth));
//newOffset = origPanPos - oldPanPos;
newOffset = oldPanPos - origPanPos;
transform.Translate(newOffset * panSpeed * Time.deltaTime, Space.Self);
oldPanPos = origPanPos + (newOffset * panSpeed * Time.deltaTime);
}
if (Input.Get$$anonymous$$ouseButtonUp(0)){
oldPanPos = origPanPos + (newOffset * panSpeed * Time.deltaTime);
isPanning = false;
}
}
}
Your answer
Follow this Question
Related Questions
Making a Smooth camera zoom with transform.Translate 1 Answer
Mouvement issue with mouse input. 0 Answers
Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers
having CharacterController movement in fixedupdate causes jerky movement on camera 3 Answers
Camera Modification Help 0 Answers