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
0
Question by MadMachine · Jun 15, 2013 at 04:24 PM · androidtouchangle

Work out the angle from center of screen to Touch or Mouse Click

Hello my game has a tile based map and the character moves around in 4 directions (Up,Down,Left,Right) what id like to do is click or touch the screen and it will move in one of these directions.

If im trying to imagine it picture the screen as a square and spliting it in the shape of a cross so a touch event happens in each of the four segments it will move in that direction.

So i figured working out an angle in degrees from the center of the screen to the touch position is the way to go but i was getting weird results and i had trouble writing good IF statments for the angles.

Ive tried a few things but i honestly think i was just using them wrong. What i need is what is the best way to work out this angle and how to use the if statment for the UP direction as it will be between 315 and 45 i would learn from this for the others.

Thank you and if any more info is needed just say.

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
Best Answer

Answer by Graham-Dunnett · Jun 15, 2013 at 05:23 PM

So, imagine a square, with diagonal lines from corner to corner. This divides the square into 4 pieces, call them North, East, South and West. They are each triangle shapes, with a long edge along the sides of the square. If you think about only the East part, then the lines it has are at 45 degrees and -45 degrees. I think you want to know if the user has clicked/touched in this area. Note that in this area the x coordinate is positive, and the y coordinate might be positive or negative. (This assumes the centre of the screen is at 0,0. You might need to do some subtraction of half the screen width and height to compute these origin relative values.) Note also, that you've only clicked in this area if the x coordinate is greater than the absolute value of the y coordinate. If the y coordinate is positive, and greater than the x coord, then you've touched north. If the y coordinate is negative, and the absolute value is greater than the x coord, then you've clicked in south. Then repeat these tests for x negative. You'll end up with hits in West, or more hits in North or South.

(What I mean is, you don't need the angle at all.)

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 MadMachine · Jun 15, 2013 at 06:14 PM 0
Share

Thanks yes i understand the way i was thinking looks jury rigged now :) Thank you for the quick solution

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

15 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

Related Questions

how to drop mesh in all of directions? 0 Answers

TouchPhase not firing correctly - Android 3 Answers

Moving an Object to the position of touch 1 Answer

Best Way to make Swipe to Look function via touch. (Android) 0 Answers

Swipe control for circle (Can do swipe left/right) 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