- Home /
making an enemy bullet ignore other enemies
so i have a 2d game where i have a turret coming out of the wall and shoots non-stop and a robot walking in its range, i'm not very experienced but i looked for an answer of how to make the enemy bullets ignore the robot, i found physics.ignorecollision but i had no luck trying it, what is happening now is that the bullets are throwing the enemy away any help would be appreciated and here are my scripts
this one is for the machine gun(turret)
public GameObject bullet; public Transform obj; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update () {
GameObject KEBUMZ;
KEBUMZ= Instantiate (bullet, obj.position,obj.rotation ) as GameObject;
KEBUMZ.rigidbody.AddForce(obj.forward*2000);
}
}
and this one is for the bullet
public class Bullet : MonoBehaviour { public GameObject Wobot;
void start(){
Physics.IgnoreCollision(Wobot.collider, collider);
}
// Update is called once per frame
void OnTriggerEnter (Collider Missle) {
if(Missle.tag !="enemy" ){
Destroy(gameObject);
}
} }
Thank you.
Answer by aldonaletto · Jun 28, 2012 at 12:29 PM
There are some alternatives:
1- Use layers: create a new layer (Enemies, for instance - button Layers, option Edit Layers) and select this layer in the enemy and bullet prefabs (field Layer, in the Inspector), then uncheck the crossing of Enemies x Enemies layers in the Physics Manager (menu Edit/Project Settings/Physics) - this will make enemy bullets pass through the enemies without any collision, while preserving collisions with anything else that isn't in the Enemies layer.
2- Use mass ratio: set the bullet mass to a small value - like 0.01, for instance - this will reduce the bullet impact (99%, if 0.01 is used). The bullets will still be destroyed when hitting enemies, but the impact will not be noticeable.
No matter which's the preferred alternative, you should use OnCollisionEnter to detect rigidbody collisions - OnTriggerEnter only works for triggers. Making the bullet's collider a trigger could be a 3rd alternative, with effects similar to the second one: the bullets will be destroyed when hitting anything, but there will not exist any impact.
If the bullet has a normal collider (Is Trigger unchecked), your collision code would become something like this:
... void OnCollisionEnter (Collision Missle) { if (Missle.transform.tag !="enemy"){ // apply damage to the object hit } Destroy(gameObject); }The way you apply damage usually is by SendMessage or by direct call to some player's damage function:
// using SendMessage - this code calls the function ApplyDamage(5) in any hit object, // if such function exists: Missle.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver);
// using direct calls - this code calls the function ApplyDamage(5) in the HealthScript // script, if this script is attached to the hit object: HealthScript hScript = Missle.transform.GetComponent< HealthScript>(); if (hScript) hScript.ApplyDamage(5);
Your answer
Follow this Question
Related Questions
Turret bullet rotation problem 1 Answer
No collision on trigger and character controller 1 Answer
Radial/omni directional shooting Problem 1 Answer
On TriggerEnter issues 1 Answer
Turret won't shoot 1 Answer