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 Nagh · Mar 03, 2019 at 05:04 PM · androidtouchtouch controls

Move Camera with touch (andoid)

Good day everyone, I've a question about touch input and using it to move my camera. My goal is to move the camera, which shows everything from above, with touch. So if my finger swipes up in a value of 1, the camera swipes down in a value of 1. So you move it like moving the map in google maps.

This code is in the update-section of my camera.

 if (Input.touchCount == 0)
             {
                 hold = false;
             }
             if (!hold)                                  //wenn noch nicht gehalten, dann
             {
                 if (Input.touchCount == 1)              //überprüfe, of ein kontakt
                 {
                     hold = true;
                     startPos = Camera.main.ScreenToViewportPoint(Input.touches[0].position);
                 }
                 else if (Input.touchCount == 2)         //überprüfe, ob zwei kontakte (ZOOM)
                 {
 
                 }
                 else
                 {
 
                 }
             }
             else                                        //wenn schon gehalten wird, dann
             {
                 if (Input.touchCount == 1)
                 {
                     Vector2 MoveVector = new Vector2(startPos.x - Camera.main.ScreenToViewportPoint(Input.touches[0].position).x, startPos.y - Camera.main.ScreenToViewportPoint(Input.touches[0].position).y);
                     transform.Translate(MoveVector * Time.deltaTime * 20);
                 }
             }

I know, this is maybe hard to understand what I want (because I'm too stupid to tell you correctly), but I'm very happy about every answer :)

Have a nice day dudes!

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
0

Answer by xxmariofer · Mar 03, 2019 at 08:27 PM

hello test this code and tell me if it was what you were searching for

  Vector2 MoveVector;
 // Start is called before the first frame update
 void Update()
 {
     if (Input.touchCount == 0)
     {
         if (hold)
         {
             MoveVector = new Vector2(startPos.x - Camera.main.ScreenToViewportPoint(Input.touches[0].position).x, startPos.y - Camera.main.ScreenToViewportPoint(Input.touches[0].position).y);
             hold = false;
         }
     }
     else
     {
         if (Input.touchCount == 1 && !hold)              //überprüfe, of ein kontakt
         {
             hold = true;
             startPos = Camera.main.ScreenToViewportPoint(Input.touches[0].position);
         }
         else if (Input.touchCount == 2)         //überprüfe, ob zwei kontakte (ZOOM)
         {

         }
         else
         {

         }
     }

     transform.Translate(MoveVector * Time.deltaTime * 20);
 }
Comment
Add comment · Show 2 · 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 Nagh · Mar 04, 2019 at 09:06 AM 0
Share

Hey, thank you for your help, but I get errors if I touch the screen. It says:

IndexOutOfRangeException: Index was outside the bounds of the array. CameraBehavior.Update () (at Assets/Scripts/Data/CameraBehavior.cs:25)

In your Code It's line 10.

But thank you anyways :)

avatar image xxmariofer Nagh · Mar 04, 2019 at 09:10 AM 0
Share

oh i see thats because i am accessing to the touch number 1 when there are no touches my fault. try this create a new var called endPos

 void Update()
 {
     if (Input.touchCount == 0)
     {
         if (hold)
         {
             $$anonymous$$oveVector = new Vector2(startPos.x - endPos.x, startPos.y - endPos.y);
             hold = false;
         }
     }
     else
     {
         if (Input.touchCount == 1 && !hold)              //überprüfe, of ein kontakt
         {
             hold = true;
             startPos = Camera.main.ScreenToViewportPoint(Input.touches[0].position);
         }else if(Input.touchCount == 1 && hold)
         {
             endPos = Camera.main.ScreenToViewportPoint(Input.touches[0].position);
         }
         else if (Input.touchCount == 2)         //überprüfe, ob zwei kontakte (ZOO$$anonymous$$)
         {
         }
         else
         {
         }
     }
     transform.Translate($$anonymous$$oveVector * Time.deltaTime * 20);
 }
     

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

247 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 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 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 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 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 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 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

Differentiate swipe from tap android 0 Answers

Why is this raycast still hitting ground below a UI button, even after I pass a check to see if finger is above button (as per Unity tutorial) 1 Answer

Mutli-touch in android? 1 Answer

EventSystem.current.IsPointerOverGameObject(t.fingerId) - Not working on Android builds - Unity 2 Answers

Cross Platform Input Controller Not Working on Mobile Device 2 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