How do i make my bullet destroy on trigger?
Hey im making a 2d shooter and im trying to get the bullets to destroy themselves when they hit something. They already collide with things because they can kill a enemy. What i have just wont work. ive tried alot of differnt things.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotCollide : MonoBehaviour {
public GameObject bulletPrefab;
// Use this for initialization
public bool hit;
public float lifetime = .1f;
void Awake()
{
Destroy(gameObject, lifetime);
}
void OnTriggerEnter () {
Destroy(gameObject);
hit = true;
}
void OnTriggerEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Wall" )
{
Destroy(gameObject);
}
}
}
Comment
Answer by bennett_apps · Aug 02, 2018 at 07:48 PM
OnTriggerEnter doesn't work for 2d games...you HAVE to use OnTriggerEnter2D. Also, OnTriggerEnter2D should be written like:
void OnTriggerEnter2D (Collider2D col) {
}
And NOT:
void OnTriggerEnter2D (Collision2D col) {
}
Your answer
Follow this Question
Related Questions
How to move and rotate to other direction when hitting a wall 1 Answer
Trying to get bool statement to work in OnTriggerStay2D 1 Answer
Unity 2D colliders not triggering,Unity 2D collider not triggering 0 Answers
*Temporarily* turn off collision between player and enemy 1 Answer
Infinite Collision? Can't figure out bug 0 Answers