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 TommyB · Feb 06, 2014 at 09:49 PM · 2d collision2d physics

2D get touch position relative to object

How can I get the touch position on a specific 2D gameObject/sprite?

Searching these forums I've managed to record when I get a touch on an object using either a rayCast or the OnMouseDown method, but I want to retrieve the position on the object that was touched.

I've got these methods in a script attached to the gameObject in question and there will be not other gameObjects that I'm worried about clicking.

e.g

If I have a circle and I touch the left hand side of it, I want to move it right. If I touched it on the right hand side I want to move it left. I know how to apply force when it is touched, but I can't work out what coordinates I clicked my object

(left and right were simple examples, in reality I would move it in any direction (e.g diagonals as well)

Thanks

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

2 Replies

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

Answer by TommyB · Feb 06, 2014 at 11:56 PM

I actually managed to do this using the code below. To be clear the issue I Was having was getting the clickPos in relation to the gameObject. I'd worked out how to add the force. Unfortunately I couldn't answer my own question until it was approved!

In case anyone else needs it, this is the code i used to get the click position relative to the gameObject

   if (Input.GetMouseButton (0)) {
                     
                     Vector3 clickedPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                     RaycastHit2D hit = Physics2D.Raycast (clickedPosition, -Vector2.up);
                     if (hit) {
                             Vector3 hitPoint = this.transform.InverseTransformPoint (hit.point);                                                
                     }
             }
Comment
Add comment · 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
0

Answer by Maui-M · Feb 06, 2014 at 10:12 PM

Here is the logic, you'll need to post code to get more detailed information for your situation.

Get position of the game object, Vector2 objPos Get position of your click, Vector2 clickPos

Get the force to apply to the game object

Vector2 force = new Vector2(objPos.x - clickPos.x, objPos.y - clickPos.y);

Comment
Add comment · 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

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

19 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

Related Questions

Lag Caused by Overlapping 2D Colliders 1 Answer

Model stuck on collider 1 Answer

2d Object Increases in Speed with Each Collision 0 Answers

Why Does Object Keep Getting Faster? 1 Answer

2D pixel collision & destruction 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