- Home /
A collision wall with different behavior
Hi,
I'm building an 'invisible wall' which must behave differently based on the objects that collide on it.
If GameObject of type A collides on the wall, it will be destroyed.
If GameObject of type B collides on the wall it will bounced in the opposite direction.
If GameObject of type C collides on the wall, it won't be affected the GameObject in any way, so it continues its original movement and trajectory.
Which is the best way of doing this sort of thing?
Here my solution, but since i'm new on unity i'm not sure if it's the best way:
The wall is an empty GameObject with a boxCollider and the "isTringger = true " option:
myWall = new GameObject ("Wall");
boxCollider = myWall.AddComponent (BoxCollider);
boxCollider.size = Vector3 (...);
boxCollider.center = Vector3 ();
boxCollider.isTrigger = true;
Then I control the behavior with this code:
void OnTriggerEnter(Collider collision)
{
if (collision.collider.gameObject.tag == "Type**A**")
{
Destroy(collision.gameObject);
}
else if(collision.collider.gameObject.tag == "Type**B**")
{
collision.collider.rigidbody.velocity = new Vector3(collision.collider.rigidbody.velocity.x, -collision.collider.rigidbody.velocity.y, collision.collider.rigidbody.velocity.z) ;;
}**else**{
//third case, the wall is totally invisible, any action is performed
}
}
Is it a good solution?
Thanks in advance! Regards!
Answer by Fattie · Oct 28, 2012 at 03:53 PM
catwoman - the answer is you have reached the point where must learn about physics "LAYERS"
http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
you can't do any real development without this so take your time and enjoy