- Home /
How to detect collisions in C#
So before I begin I'm new to coding. As it says in the title I'm trying to detect a collision in C#. What I'm trying to do is when two colliders collide it causes a float variable to go down a certain amount. So for example a sword hits an enemy and causes its life to go down. Would I use OnCollisionEnter, OnTriggerEnter, or something else? And how would I go about using whichever one I need? Thank you in advance for the help!
Answer by clunk47 · Jan 01, 2013 at 07:02 PM
Read the script reference... OnCollisionEnter, or OnTriggerEnter. You can change the example to C# on the script refs.
Answer by animationanalyst · May 29, 2013 at 11:27 AM
have used this in a game where the villain game object will destroy when it gets hit by the bullet gameobject.*
Note: The velocity of the bullet must be less like 200. Unless unity cannot calculate the trigger function.
using UnityEngine; using System.Collections;
public class smallvillianhealth : MonoBehaviour { public int health = 100;
void Start ()
{
}
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag=="bullet")
Destroy(gameObject);
}
}