I have a player that shoots in the direction that it is facing. However, the shot that is created when I click, also destroys the player (example). How would I make the shot ignore collision with the player? Or better yet, how to make a shot destroy anything it touches and destroy itself without affecting the player?
This is the code that controls collisions:
function OnTriggerEnter (col : Collider) {
Destroy(col.gameObject);
}
The shot is a trigger, but the player isn't. Not sure if this changes anything in this case.
Answer
There are a lot of ways to ensure that.
The best approach is to use the "Layer Collision Matrix". To keep things organized, you could create a Bullets
, Enemies
and a Player
Layer. Then go to Edit > Project Settings > Physics and you'll see the collision matrix.
You can check all the layer-pairs that should report collisions there. In the screenshot you can see that Bullets will only collide with Enemies and Enemies also collide with the Player. Then all that's left to do is assign the matching layers to your GameObjects or Prefabs.
Using the collision matrix is the best approach, because it directly affects the physics simulation (non-colliding layers don't have to be checked for collisions at all) which is the most performant solution to your problem.
Another approach (or you could also combine the two) is to implement OnCollisionEnter
or OnTriggerEnter
on the Enemies and Players, and not the Bullet. Then you're free to implement different behavior for each entity and also report scores or perform other tasks.
Bullets could also use
col.gameObject.SendMessage("HitByBullet", null,
SendMessageOptions.DontRequireReceiver);
instead of your current Destroy(col.gameObject)
. Then you can implement a HitByBullet
method in your Enemy Script and destroy the object there, play a sound effect etc.
No comments:
Post a Comment