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 TheCount · Feb 02, 2014 at 12:57 AM · javascriptinputpositiontouch

Get touch position?

All I need is to get the x coordinate of the touch position.

I want to know if the user has tapped on the right half of the screen.

This may not be the only touch on the screen... so here is what I have so far:

 #pragma strict
 
 function Update (){            
     for (var i = 0; i < Input.touchCount; ++i){
         if (Input.GetTouch(i).phase == TouchPhase.Began) {
               if (Input.GetTouch[i].position > parseFloat(Screen.width/2)){
                 //do something
                   }
           }
         }
 }

Can someone help me?

Comment
Add comment · Show 1
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 drewdrewpt · Sep 01, 2016 at 10:45 PM 0
Share

In order to use it, do you attach it to the camera and does it work for 3 D games

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by KellyThomas · Feb 02, 2014 at 01:19 AM

One error in you code is using "`GetTouch[i]"` on line 6, when it should be "`GetTouch(i)`" as demonstrated on the line before it.

Usually if you are repeatedly calling the same getter it is neater and easier to store that value of interest locally.

 function Update () {            
     for (var i = 0; i < Input.touchCount; ++i) {
         Touch touch = Input.GetTouch(i);
         if (touch.phase == TouchPhase.Began) {
            if (touch.position > (Screen.width/2)) {
                 //do something
            }
         }
     }
 }
Comment
Add comment · Show 4 · 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 Eduardo Cortes · Aug 28, 2014 at 06:58 PM 1
Share

have a small error in your code.

 function Update () {            
     for (var i = 0; i < Input.touchCount; ++i) {
         Touch touch = Input.GetTouch(i);
         if (touch.phase == TouchPhase.Began) {
            // Need to put .x
            if (touch.position.x > (Screen.width/2)) {
                 //do something
            }
         }
     }
 }

avatar image KellyThomas · Sep 14, 2014 at 08:23 PM 0
Share

thanks, good catch.

avatar image L96Unity · Jan 28, 2015 at 11:27 PM 0
Share
         var touch : Touch;
         touch = Input.GetTouch(i);

Ins$$anonymous$$d of:

  Touch touch = Input.GetTouch(i);

Worked for me. Thanks

avatar image Dane411 · Jun 18, 2015 at 10:57 PM 0
Share

foreach (Touch touch in Input.touches)

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

22 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

Related Questions

How to get my character to move left to right? 0 Answers

solution to detect multi- finger touch position 2 Answers

unity 5.2 Input.GetTouch(0).position incorrect? 3 Answers

Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer

Vector2 Error when Declaring new Vector3 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