I have a FPS shooter the fires bullets (prefabs). I have a cube that I would like to be able to fire bullets at that I can detect it being shot and will then be destroyed.
I added a GameObject (a cube) called ‘Cube’. The OnTrigger is unclicked.
I have the following script attached to the Cube Object:
using UnityEngine;
using System.Collections;
public class shootCube : MonoBehaviour {
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Cube")
{
Debug.Log ("Hit Cube!");
Destroy(col.gameObject);
}
else
{
Debug.Log("Hit something else!");
}
}
}
The bulletPrefab is set-up like this:
Tag=theBullet
Cylinder 001 (Mesh Filter)
Meah → Cylinder001
Animator (checked)
Culling Mode= Always Animate
RigidBody:
Collision Detection = Discrete
Bullet Hit (script)
Script bulletHit
Here is the bulletHit Script:
using UnityEngine;
using System.Collections;
public class bulletHit : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col)
{
Debug.Log ("Bullet hit something!");
}
}