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 Endiss · Oct 11, 2014 at 03:25 PM · c#inputobjecttouch

Get clicked object cords on object ? Unity2D

Hello folks! I'm doing some tests with clicking object and what do get clicked object cords on object. alt text

I think image show exactly what i want to achieve. Here is my click call block:

 checkPosition (Input.GetTouch (0).position);


And here is function that is called from this block.

 private void checkPosition(Vector2 vec){
                 Vector3 wp = Camera.main.ScreenToWorldPoint (vec);
                 Vector2 touchPos = new Vector2 (wp.x, wp.y);
                 var hit = Physics2D.OverlapPoint (touchPos);
 
                 if (hit) {
                     Debug.Log(hit.transform.gameObject.name);
                     Debug.Log(hit.transform.position.x + " | " + hit.transform.position.y);    
                     hit.transform.gameObject.SendMessage("Touched",0,SendMessageOptions.DontRequireReceiver);
                 }
     }

Any advice will be great how to get thous cords.

touches.png (7.0 kB)
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 robertbu · Oct 11, 2014 at 03:31 PM

The solution will vary depending on the type of object. The easiest would be a Quad.

  • Convert the point to a local coordinate using Transform.InverseTransformPoint()

  • Add (0.5, 0.5, 0.0) to the result. This is because coordinates are relative to the pivot, and the pivot is in the center. By adding this vector, you are placing the origin in the lower left corner.

  • Multiply the result by the Transform.localScale

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 Endiss · Oct 11, 2014 at 03:56 PM 0
Share
     private void checkPosition(Vector2 vec){
                 Vector3 wp = Camera.main.ScreenToWorldPoint (vec);
                 Vector2 touchPos = new Vector2 (wp.x, wp.y);
                 var hit = Physics2D.OverlapPoint (touchPos);
 
                 if (hit) {
                         Vector3 tempVect = transform.InverseTransformPoint(hit.transform.gameObject.transform.position);
                         Vector2 tempv2 = new Vector2(tempVect.x,tempVect.y);
             tempv2 = tempv2 + new Vector2(0.5f,0.5f);
             tempv2 = new Vector2(tempv2.x * hit.transform.gameObject.transform.localScale.x,tempv2.y*hit.transform.gameObject.transform.localScale.y);
                         Debug.Log(tempVect.x+" | "+ tempVect.y);
                         }
                 }

Tryed this but still gets same cords even if i click other side of object .

avatar image robertbu · Oct 12, 2014 at 03:45 AM 0
Share

Here is a rewrite of your code that will work with a Quad.

 using UnityEngine;
 using System.Collections;
 
 public class Example: $$anonymous$$onoBehaviour {
 
     void Update () {
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             CheckPosition(Input.mousePosition);
         }
     }
 
     private void CheckPosition(Vector3 vec){
         vec = Camera.main.ScreenToWorldPoint (vec);
 
         Collider2D col = Physics2D.OverlapPoint (vec);
         if (col != null) {
             Vector3 v = col.transform.InverseTransformPoint(vec);
             v += new Vector3(0.5f,0.5f, 0.0f);
             v = Vector3.Scale (v, col.transform.localScale);
             Debug.Log(v.x+" | "+ v.y);
         }
     }
 }

For code like this to work, it is necessary to know the real world size of the object. A Quad is a know size (1 x 1), which makes the calculations easy.

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

2 People are following this question.

avatar image avatar image

Related Questions

Mouse and Oculus Touch Controller Inputs 0 Answers

Do something only when finger is on object 0 Answers

Player doesn't stop jumping when stop touching the screen[Android] 1 Answer

Help changing mouse input to touch input. 0 Answers

PointerClick multiple overlaying UI elements 0 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