How to make one object pass through one object but hit another?
I have a game in which there is going to be three different enemies; one that runs away from you, one that chases you and one that doesn't move. I've managed to make a bullet shoot when you click, but i only want it to hit one type object and pass through or destroy itself when it hits the others, but if it could bounce off the terrain that would be wonderful. I have basically no experience with coding, i copy most of them from the web.
-Tom
Answer by 3dmihai · Aug 30, 2016 at 11:56 AM
I'm quotating from Unity docs: "A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts." I guess this is what you need, you just tick the "Is Trigger" option in the inspector for all objects you don't want to be solid.
I'm not sure if you understand what i mean, i want the objects to be solid but i only want one object and its clones to destroy themselves when they hit it.
Yes, you could mark those objects and use OnTriggerEnter to check if they collided with the bullet. For example:
void OnTriggerEnter(Collider someObject)
{
if (someObject.transform.gameObject.name == "FPSController"){
// destroy object
}
}
just replace FPSController with the name of the object it is colliding.
Answer by daichiy89 · May 31, 2020 at 04:19 PM
I think what you are looking for is Layer based collision detection. https://docs.unity3d.com/Manual/LayerBasedCollision.html
Place the objects you want to collide with in one layer and the ones you want to pass through in another layer. Then uncheck collision detection between the player layer and the pass through layer.
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D not getting called 2 Answers
Collision Problems 1 Answer
Lose Health when GameObject enters collider? (OnTriggerEnter) 2 Answers
What is ContactPoint.point? 0 Answers
Collision With Text 0 Answers