• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by loupz · Nov 08, 2015 at 11:37 AM · cameramovementcamera-movementtransform.translateshaking

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


Comment
Asgardr

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image loupz · Nov 08, 2015 at 12:44 PM 1
Share

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?

avatar image Asgardr loupz · Nov 08, 2015 at 01:02 PM 0
Share

My bad! I've edited my answer. Could you see if it works correctly now?

avatar image loupz Asgardr · Nov 08, 2015 at 07:11 PM 0
Share

Thank you Asgardr, that's made a big difference! It feels good now. My 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 : MonoBehaviour {
     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 = 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));
             
             //newOffset = origPanPos - oldPanPos;
             newOffset = oldPanPos - origPanPos;
             
             
             transform.Translate(newOffset * panSpeed * Time.deltaTime, Space.Self);
             oldPanPos = origPanPos + (newOffset * panSpeed * Time.deltaTime);
         }
 
         if (Input.GetMouseButtonUp(0)){
             oldPanPos = origPanPos + (newOffset * panSpeed * Time.deltaTime);
             isPanning = false;
         }
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i smooth out my Camera / Player Movement 2 Answers

Making a Smooth camera zoom with transform.Translate 1 Answer

Mouvement issue with mouse input. 0 Answers

having CharacterController movement in fixedupdate causes jerky movement on camera 3 Answers

Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges