Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by JoyJazper · Feb 08, 2017 at 04:11 PM · scripting beginnercamera-movementzoomsmoothlypanning

Facing problem in the code for panning and zooming camera smoothly through touch inputs [3D]?

I am trying to move my camera smoothly over the terrain with touch inputs but not getting pure smoothness like any other high quality games And after zooming motion starts becoming slower and slower. Please help me out.

Here is my code:

 public float speed = 0.1f;
 public float MIN_X = 7f;
 public float MAX_X = 12f;
 public float MIN_Y = 8f;
 public float MAX_Y = 13f;
 public float MIN_Z = 10f;
 public float MAX_Z = 10f;
 public Vector3 nextPosition;
 public float moveSpeed;
 public int camAdjust = 10;
 
 void Update () {
 
     if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved) {
         Vector3 touchdeltaPosition = Input.GetTouch (0).deltaPosition;
         transform.Translate (-touchdeltaPosition.x * speed*Time.deltaTime, -touchdeltaPosition.y * speed*Time.deltaTime, 0);
         transform.position = new Vector3(
             Mathf.Clamp(transform.position.x, MIN_X, MAX_X),
             Mathf.Clamp(transform.position.y, MIN_Y, MAX_Y),
             Mathf.Clamp(transform.position.z, MIN_Z, MAX_Z));
     }
 }

and now this script is for zooming.

 public float perspectiveZoomSpeed = 0.5f;        
 public float orthoZoomSpeed = 0.5f;  



 void Update()
 {
     
     if (Input.touchCount == 2)
     {

         Touch touchZero = Input.GetTouch(0);
         Touch touchOne = Input.GetTouch(1);


         Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
         Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;


         float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
         float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;


         float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;


         if (GetComponent<Camera>().orthographic)
         {
             GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
             GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
         }
         else
         {
             GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
             GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 20f, 80f);
         }
     }


 }
Comment
Add comment · Show 8
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 JoyJazper · Feb 12, 2017 at 10:04 AM 0
Share

someone Please reply. I am totally stuck.

avatar image JoyJazper JoyJazper · Feb 14, 2017 at 03:07 PM 0
Share

Please guys help me out.

avatar image ASPePeX · Feb 14, 2017 at 03:25 PM 1
Share

I can't really wrap my head around your code, but here is how I do it (line 50 -56): https://github.com/ProjectS$$anonymous$$rs/TestPrototype2/blob/master/Assets/Scripts/Camera$$anonymous$$ovement.cs

avatar image JoyJazper · Feb 17, 2017 at 05:49 PM 0
Share

Thankyou for the reply, @ASPePeX . I am facing a few errors like the Config.$$anonymous$$axCameraOffset in lines 19,20,54... etc. Apparently Config is missing. and also Helper.CalcTouchAngle from line 42. I really don't know what shall i do now.

avatar image ASPePeX JoyJazper · Feb 18, 2017 at 12:21 PM 0
Share

Those classes are in the same folder, Config.cs and Helper.cs: https://github.com/ProjectS$$anonymous$$rs/TestPrototype2/tree/master/Assets/Scripts

avatar image JoyJazper ASPePeX · Feb 20, 2017 at 05:31 AM 0
Share

Thankyou very much!, i will check and let you know soon.

avatar image RobAnthem · Feb 17, 2017 at 05:54 PM 0
Share

Switch to FixedUpdate and Time.fixedDeltaTime ins$$anonymous$$d of Update, and it will be a lot smoother. The camera is your main visual output, so you are going to not a lot more jittery movement from it than you would from something else.

avatar image JoyJazper RobAnthem · Feb 19, 2017 at 04:17 AM 0
Share

Thank you! Sir, It was very helpful.

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Trackpad "hand" stopped grabbing or moving scene on Mac Tackpad 0 Answers

How to move and rotate a camera smoothly and simultaneously? 0 Answers

Panning camera based on a fixed point. 0 Answers

Camera that follows rotating object with mouse input 0 Answers

How to examine an object? 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