Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 moviemastersdk · Dec 10, 2014 at 03:13 AM · camera-movementtouch controls

Camera Touch pan movement inconsistent across different screen resolutions.

I currently have this code.

 if (Input.touchCount == 1) {
             if (Input.GetTouch (0).phase.Equals (TouchPhase.Moved)) {
                 NewPos += new Vector3 (
                     -Input.GetTouch(0).deltaPosition.x/Screen.width*panSpeed,
                     0,
                     -Input.GetTouch(0).deltaPosition.y/Screen.height*panSpeed
                     );    
             }
         }

which is my attempt to normalize the Touch deltaPositions. but even after dividing with screen width/height, I still notice a massive difference in speed when running it on my phone, compared to the smaller window in unity.

My phone has a resolution of 1280x720. and android phones range from everything between 480p to 2k screen resolutions.

Is there a way to make the camera movement speed consistent across all devices?

Comment
Add comment · Show 2
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 Owen-Reynolds · Dec 10, 2014 at 05:47 AM 0
Share

Is this in, ummm, Update? I assume, like other Input events, FixedUpdate can miss or double-count, depending on frame rate. Ohterwise it looks fine to me.

avatar image moviemastersdk · Dec 10, 2014 at 03:17 PM 0
Share

Yeah it's in update, and i set the actual position of the camera in fixed Here's the whole code http://pastebin.com/CzNzu3q6

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dheffner2001 · Oct 20, 2020 at 08:09 PM

This post is from 2014 but i think many people might have the same question. This is my solution:

     public float minXPos = -12f; 
     public float maxXPos = 100f;
     public float minYPos = -100f;
     public float maxYPos = 100f;
     void Update()
     {
         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
         {
             float screenSize = Mathf.Max(Screen.width, Screen.height);
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             Vector2 touchDeltaPercentage = touchDeltaPosition / screenSize;
 
             Vector3 screenCenter = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 1f);
             Vector3 screenTouch = screenCenter + new Vector3(touchDeltaPosition.x, touchDeltaPosition.y, 0f);
 
             Vector3 worldCenterPosition = Camera.main.ScreenToWorldPoint(screenCenter);
             Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(screenTouch);
 
             Vector3 worldDeltaPosition = worldTouchPosition - worldCenterPosition;
 
             //touchDeltaPercentage values will now range from 0 to 1
 
             //define camera border in minXPos, maxXPos, minYPos and maxYPos
             transform.Translate(-worldDeltaPosition);
             Vector3 tmpPosX = transform.position;
             tmpPosX.x = Mathf.Clamp(tmpPosX.x, minXPos, maxXPos);
             transform.position = tmpPosX;
 
             Vector3 tmpPosY = transform.position;
             tmpPosY.y = Mathf.Clamp(tmpPosY.y, minYPos, maxYPos);
             transform.position = tmpPosY;
         }
 }
Comment
Add comment · Show 1 · 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 Tyrant7 · Mar 07 at 02:37 AM 0
Share

This worked very nicely for me, thank you! If anybody is looking for a version for mouse this is my version:

             if (Input.GetMouseButtonDown(2))
             {
                 deltaMousePos = Input.mousePosition;
             }
             else if (Input.GetMouseButton(2))
             {
                 Vector2 mouseDeltaPosition = Input.mousePosition - deltaMousePos;
     
                 Vector3 screenCenter = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 1f);
                 Vector3 screenTouch = screenCenter + new Vector3(mouseDeltaPosition.x, mouseDeltaPosition.y, 0f);
     
                 Vector3 worldCenterPosition = Camera.main.ScreenToWorldPoint(screenCenter);
                 Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(screenTouch);
     
                 Vector3 worldDeltaPosition = worldTouchPosition - worldCenterPosition;
     
                 //touchDeltaPercentage values will now range from 0 to 1
     
                 //define camera border in minXPos, maxXPos, minYPos and maxYPos
                 transform.Translate(-worldDeltaPosition * panMultiplier);
                 Vector3 tmpPosX = transform.position;
                 tmpPosX.x = Mathf.Clamp(tmpPosX.x, minXPos, maxXPos);
                 transform.position = tmpPosX;
     
                 Vector3 tmpPosY = transform.position;
                 tmpPosY.y = Mathf.Clamp(tmpPosY.y, minYPos, maxYPos);
                 transform.position = tmpPosY;
     
                 deltaMousePos = Input.mousePosition;
             }
 


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Panning camera based on a fixed point. 0 Answers

Handling Cinemachine Freelook Camera with Touch Input to pan 0 Answers

Multi-touch for camera not working? 0 Answers

How to avoid TouchPhase.Ended effect? 0 Answers

Adapting camera movement to touch input 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