- Home /
checking where the ball was hit by an object
I got a bouncing ball, and i want to set a condition that will execute when my ball is being collided with the bottom of his collider. it has a box collider, when the bottom of the collider is hit by other object the condition i want will be executed. how do I do that?
Answer by fafase · Mar 07, 2015 at 07:22 PM
Easiest way is to separate your collider in sub colliders. At the moment, you have one box collider for the whole ball, make it two on two children.
This way you can still get the collision from the top children if you need so and you get from the bottom one to perform the specific action.
I was thinking the same thing, but I wasn't sure about this since I read somewhere it's not recommended to have a few colliders on 1 object, so looked for an alternative, what's your aspect on more than 1 collider on an object?
Answer by robert_southee · Mar 07, 2015 at 07:53 PM
One solution would be to check if the difference between the positions of the colliding objects points 'away' from the object's local down.
Exampe:
void OnCollisionEnter(Collision collision)
{
float dot = Mathf.Cos(90f * Mathf.Deg2Rad);
var direction = collision.transform.position - transform.position;
if (Vector3.Dot(-transform.up, direction) >= dot)
{
Debug.Log("Hit from bottom");
}
}
You can make the angle tighter be reducing the 90f. You could also change the -transform.up in the Vector3.Dot to -Vector3.up if you only care about world space.
$$anonymous$$ath! Always hated it! I knew it would come back as a boomerang :D I'll try this tomorrow and let you know if this is what I was looking for :) thanks for comment
Your answer
Follow this Question
Related Questions
add collision to animated object 1 Answer
Terrain collision problem 1 Answer
Spawning enemys by collision 1 Answer
collision issues 1 Answer