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
2
Question by ROM · Jul 11, 2010 at 01:54 PM · iphonetouchangleeuleranglestouchscreen

Detect touch location?

I have a sphere in the centre of my application. When a user touches the screen (iphone) I want to know how many degrees away from the centre sphere it is. So for example if the user touched directly to the right of the sphere it would be "90" or if the user touched directly to the left of the sphere it would be "270". I've searched online but I have no idea how to go about doing this. Any ideas?

Edit: Yey! Problem solved. I used the below code combined with this basic touch code I just wrote:

function Update () {
    if ( iPhoneInput.touchCount > 0 ){
        for(var touch : iPhoneTouch in iPhoneInput.touches) { 
                Debug.Log(touch.position);
        }
    } 
}
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
4
Best Answer

Answer by Noise crime · Jul 11, 2010 at 02:25 PM

There are two ways you could approach this, one is to use Vector3.Angle() to get the angle between one vector and another, or you could use Atan2, which is essentially results in the same thing.

Basically you need to find the vector from the screen origin (center of your sphere in screen co-ordinates) and the current mouse position. With this you can find the angle between it and say a vector pointing straight up (in screen space)

Vector3 ScreenOrigin = new Vector3( Screen.width/2,Screen.height/2,0);
Vector3 MousePos = Input.mousePosition - ScreenOrigin;
Vector3 UpVector = new Vector3(0,1,0);
float angle = Vector3.Angle(UpVector, MousePos);        
print("Mouse:  " + MousePos + "  Screen: " + ScreenOrigin + "  Angle: " + angle);

This will give you a value of 0 straight up, 90 directly left/right and 180 straight down. To get it into 0-360 instead of 0-180 on both sides, you'll have work out which side the mouse is on and adjust accordingly. However i'd just use the second method below.

Vector3 ScreenOrigin = new Vector3( Screen.width/2,Screen.height/2,0);
Vector3 MousePos = Input.mousePosition - ScreenOrigin;
float angle =  Mathf.Atan2(MousePos.x, MousePos.y) * Mathf.Rad2Deg;     
if (angle < 0) angle2 = 360+angle;      
print("Mouse:  " + MousePos + "  Screen: " + ScreenOrigin + "  Angle: " + angle );

This should give you 0(and 360) straight up, 90 to the right, 180 down and 270 left.

Comment
Add comment · Show 3 · 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 ROM · Jul 11, 2010 at 02:48 PM 0
Share

Ah sorry, a lot of the stuff is right in your above post but I forgot to mention it's for an iPhone application. I've edited my first post. I'm not sure what the iPhone equivalent of mousePosition is. Oh and I was looking at a full 360 degree angle rather than just 90 etc, they were just examples. Thanks for your help anyway, I might try and work from there!

avatar image Noise crime · Jul 11, 2010 at 11:03 PM 0
Share

Ah, didn't know things were slightly different for iPhone, glad you found the code for that. As to my code I guess you've discovered that it does do all angles 0 to 360, I was just pointing out the directions of the main 4.

avatar image Noise crime · Jul 11, 2010 at 11:05 PM 0
Share

Although If you can hold the iPhone in any orientation then you may have to adjust the Atan(x,y) values to account for that. Same with the UpVector in the first example, where i'm setting it that x is left/right, y is up/down on the screen and z is ignored.

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

No one has followed this question yet.

Related Questions

How can I tell if the device I am on supports touch? 4 Answers

Project works fine in Editor(Remote) but not on iOS 0 Answers

How can I detect touch on anroid or iphone 2 Answers

Camera orbit with touch 1 Answer

What is the command to check whether player is touching the screen or not?? 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