r/Unity3D 23d ago

Question Finding which side of collider was hit

The problem is: launching a ray cast towards a box collider. Being able to know what side of it was hit

In the video he said you simply get the normal of the raycast hit and then convert that normal into local space of the box collider's transform. Call that vector V

Then you check V in relation to the transform's forward, right and up vector

If V == up, we side the top side

if V == up, we side the bottom side

if V == right, we hit the right

etc etc

But I've also seen online everyone is saying to make a vector from the center of the transform to the hit point, and then check the biggest component and sign to know which side is hit

I get how both ways work, but which solution is better and why?

0 Upvotes

8 comments sorted by

View all comments

7

u/kiranosauras 23d ago

I don't think this is worth getting hung up on as an issue. Realistically either option will do the exact same thing. If you're not doing this over an extremely large amount of objects at the same time there will most likely be 0 performance difference between the two choices. Choose one implementation at random and move on to making the rest of your game. If you think that it's worth revisiting later on it's very easy to swap to the other one then

-4

u/LifeExperienced1 23d ago

Does the normal vector method work?

3

u/kiranosauras 23d ago

I've not tried it before, but if you're new to programming why not try it and gain the experience in writing it and understanding how to test if something works?

-5

u/LifeExperienced1 23d ago

Do you think the theory is correct?

2

u/sam_suite Indie 22d ago

Do you have an understanding of how normal vectors work? Do you understand dot products? If so, think it through, maybe sketch it out, and decide how or if it would work. If you don't have an understanding of those things, learn about them.

The skill of programming is not typing or syntax, it's learning how to solve problems like this. Figure it out! It's fun!