Author Topic: Get coordinate where touch occurred in box collider  (Read 6218 times)

sunkas

  • Guest
Get coordinate where touch occurred in box collider
« on: October 10, 2012, 06:40:53 AM »
Is it possible to detect where a touch is happening in a box collider?

I need a button and ba able to detect where in it the touch is happening.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get coordinate where touch occurred in box collider
« Reply #1 on: October 10, 2012, 09:43:34 AM »
UICamera.currentTouch.pos tells you the screen position. UICamera.lastHit.point tells you the world position. You can calculate the rest from that.
« Last Edit: October 11, 2012, 08:25:52 AM by ArenMook »

sunkas

  • Guest
Re: Get coordinate where touch occurred in box collider
« Reply #2 on: October 11, 2012, 04:00:57 AM »
Thanks!

But what coordinate system does this position use? Any tip on how to convert this position into world coordinate or local coordinate?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get coordinate where touch occurred in box collider
« Reply #3 on: October 11, 2012, 04:18:41 AM »
That's the world position. Transform class has functions for transforming a world point to local coordinates.

sunkas

  • Guest
Re: Get coordinate where touch occurred in box collider
« Reply #4 on: October 11, 2012, 06:04:30 AM »
Are you sure it's world coordinates?

I tried this code on a onClick method on a button:

  1. Vector3 worldTouchPos = UICamera.currentTouch.pos;
  2. Vector3 localTouchPos = this.gameObject.transform.InverseTransformPoint (worldTouchPos);
  3.                
  4. Debug.Log ("worldTouchPos:"+ worldTouchPos);
  5. Debug.Log ("localTouchPos:" + localTouchPos);
  6. Debug.Log ("ButtonPos:"+this.transform.position);

And got the following result:

worldTouchPos:(299.0, 686.5, 0.0)
localTouchPos:(230072.3, 526616.3, -1206.2)
ButtonPos:(-0.6, 0.8, 1.6)

I assume ButtonPos is in world coordinates where the x and y are 0 to 1. InverseTransformPoint does accoding to docs "Transforms position from world space to local space." so I assume UICamera.currentTouch.pos is not world coordinates?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Get coordinate where touch occurred in box collider
« Reply #5 on: October 11, 2012, 07:27:13 AM »
UIcamera.currentTouch.pos is not world position. It's screen pixels.

I've just messed around with it a bunch, so while it may be a mistake, that's certainly what you get. :)

Do note that this is not affected by UIRoot or anything - it's just pixels on the screen, so iphone 4 is [(0,0), (960,640)]

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Get coordinate where touch occurred in box collider
« Reply #6 on: October 11, 2012, 08:24:50 AM »
Nicki is right. currentTouch.pos is in screen pixels. UICamera.lastHit.point is in world coordinates. My mistake.

sunkas

  • Guest
Re: Get coordinate where touch occurred in box collider
« Reply #7 on: October 11, 2012, 08:58:03 AM »
Thanks! UICamera.lastHit.point saved the day!