Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Compguru910 · May 26, 2013 at 06:15 PM · vector3

Help with touch camera movement

Well, im new to Unity and touch based events in general. Im trying to make the camera move on the X/Y coordinate plane with touch. I have been able to get it to move, and its moving in the right direction (so I think) but its moving in extreme steps. Im using the cameras beginning position and take the new delta position from it, and updating the camera postion that way. From what I can tell it is moving in the right direction, but its jumping by the thousands per short movement. Ive even tried to multiply the deltaposition by .001 and still no luck. Im pretty sure this is an easy fix. Here is the code

 void Update () {
     
         foreach(Touch touch in Input.touches) {
             if (touch.phase == TouchPhase.Began) {
                 if (touch.fingerId == 0) {
                     //Get starting position of touch
                     touchBound = touch.fingerId;
                     ScrollTouchOrigin = touch.position;
                 }
             }
             //Unbind the touch if the touch phase ends
             if (touch.phase == TouchPhase.Ended) {
                 touchBound = -1;
                 ScrollTouchOrigin = Vector2.zero;
             }
             //Do movement on touchphase move
             if (touch.phase == TouchPhase.Moved) {
                 //Only move if the touch that is bounds is the one that moved
                 if (touch.fingerId == touchBound) {
                     //Get current camera postion
                     Vector3 oldPos = Camera.main.transform.position;
                     
                     Camera.main.transform.Translate(new Vector3(oldPos.x - (touch.deltaPosition.x * 0.001f), oldPos.y - (touch.deltaPosition.y * 0.001f), 5.0f));
                     mGuiTextX.text = Camera.main.transform.position.x.ToString();
                     mGuiTextY.text = Camera.main.transform.position.y.ToString();
                 }
             }
         }
     }
Comment
Add comment
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
1
Best Answer

Answer by robertbu · May 26, 2013 at 07:48 PM

Note that touch.deltaPositon is in Screen coordinates and your camera lives in world coordinates. You can just define a factor between the two, or you could derive a factor based on screen resolution so that the movement is consistent across devices. But in terms of your code you can change it in one of two ways. Since Translate is a relative movement, you can do:

 Camera.main.transform.Translate(touch.deltaPosition * 0.001);  

Where .001 is a factor that you either hard code or derive from the screen resolution.

Or you can do an absolute movement which is what you code was trying to do:

 Camera.main.transform.position(new Vector3(transform.position.x - (touch.deltaPosition.x * 0.001f), transform.position.y - (touch.deltaPosition.y * 0.001f), 5.0f));

Note this assumes that you want the camera's 'z' positon to be 5.0.

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 Compguru910 · May 26, 2013 at 08:49 PM 0
Share

Thanks, the first option worked perfect. The only thing I had to do differently was multiply the delta position by a negative 0.1f and it worked.

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

13 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

Related Questions

A node in a childnode? 1 Answer

Clamp X position of object 1 Answer

Middle of screen vector 3? 2 Answers

Question on Vector3.Reflect() 2 Answers

How to cut numbers after decimal point in coordinates? 1 Answer


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