Hello Guest

Author Topic: How to detect which side of a sprite was hit? (Using Raycasting)  (Read 4554 times)

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
I know this question is not really related to 2D Toolkit, but is there a way to "know" which side of a box collider from a 2D sprite that was hit (top, bottom, right, left)?  Since this is really basic stuff for everything, I would suspect it would be much easier to figure out.  :o Maybe there's a function in 2DToolkit that gives you the information about the sprite?

Been looking everywhere to find an answer but it can't seem to find it. Would it be better to just go with raycasting?



« Last Edit: January 18, 2013, 07:24:18 pm by regnared »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to detect which side of a sprite was hit? (Top, Bottom, Right, Left)
« Reply #1 on: January 10, 2013, 01:05:26 am »
In OnCollisionEnter, get the contacts, and look at the normal. You can work out the direction relative to your object that way. Having said that you might have more than one contact in which case you'd have to work out which one affected the impact the most...

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
Re: How to detect which side of a sprite was hit? (Top, Bottom, Right, Left)
« Reply #2 on: January 10, 2013, 01:33:11 am »
That is what I was thinking. Big thank you. :)

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
Re: How to detect which side of a sprite was hit? (Top, Bottom, Right, Left)
« Reply #3 on: January 17, 2013, 02:31:35 am »
If anyone finds this question, I was able to use raycasting for my problem. I posted a script and video here if you are curious.

Youtube Link - [ link ]

Script Link - [ link ]
« Last Edit: January 18, 2013, 01:14:50 am by regnared »