OnTriggerEnter for moving object

I’ve attached an OnTriggerEnter event to my bullets, trying to get the bullet to destroy itself when it encounters an object. But it doesn’t seem to work on static objects like walls.

I could use OnCollisionEnter but that’s considerably more processor heavy and I don’t need any collision information…

I could add an OnTriggerEnter script to all objects in the scene, but also seems wasteful…

OnTriggerEnter will work fine. Tag whatever you are shooting at something like Enemy or Target and attach the script the bullet itself.

function OnTriggerEnter(collider : Collision)

{
if(collider.gameObject.tag = “Enemy”)
{
Destroy(this.gameObject);
}
}

That should be it.

You should cast a ray when the bullet is shot to find it’s end point. Once reached, it’s destroyed. The speed of a bullet make it inapropriate for colliders, as it travels several times its size in one frame.

this is C# and this works for me

using UnityEngine;
using System.Collections;

public class Mover : MonoBehaviour
{
public float speed;
public int damage = 5;

void Start ()
{
	GetComponent<Rigidbody2D>().velocity = transform.right * speed;
}

void OnCollisionEnter2D(Collision2D collision)
{
	if (collision.collider.tag == "Enemy") 
	{
		collision.collider.gameObject.GetComponent<Enemy>().Damage (damage);
		Destroy (gameObject);		
	}

	if(collision.collider.gameObject)
	{
		Destroy (gameObject);
	}

}

}