- Home /
Destroying object on collision
Hello! I just need some help destroying my player once the enemy bullet hits the player. I have a script to do so, but is doesn't work. If you could also tell me how to change the scene once the player is hit, that would be great. Here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DestroyPlayer: MonoBehaviour {
void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "AI Bullet") Destroy(gameObject); } }
I Added AI Bullet Tag to the AI bullet, and I applied this script to the player. I have this same script on the enemy, but it is called DestroyEnemy, and is specific to the enemy. The other script works just fine. Thanks!
Answer by Cornelis-de-Jager · Jul 10, 2017 at 02:51 AM
Instead of using:
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "AI Bullet")
Destroy(gameObject);
}
Use:
void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "AI Bullet")
Destroy(gameObject);
}
Otherwise make sure that your players transform/bullet transform is set as a trigger
Answer by bgprocks · Jul 10, 2017 at 06:12 AM
FYI, OnTriggerEnter and OnCollisionEnter will only fire if your player has a ridigbody attached. This use to get me in the early days.
Answer by TimothyAppel · Jul 10, 2017 at 08:20 PM
Maybe put the destroy on the bullet instead, so: void
OnTriggerEnter(Collision other) { if (other.gameObject.tag == "Player") Destroy(other.gameObject);
}
Also I'm not sure if your bullet is being used as trigger, if so use OnTriggerEnter, otherwise use OnCollsionEnter
I don't know if this helps...goodluck