- Home /
Detecting collisions
I've been trying to get my enemies to detect my player immediately using OnTriggerEnter, OnTriggerExit, and OnTriggerStay but I haven't gotten any of them to work. Does anyone know a void/method/function that could hep me here in c#?
edit: I recently edited my code and now there are two codes that coordinate the score together. they go like this:
using UnityEngine; using System.Collections;
public class GameOver : MonoBehaviour {
void OnTriggerEnter(Collider other){
SendMessageUpwards("HasTriggered",gameObject);
}
}
using UnityEngine; using System.Collections;
public class GameManager : MonoBehaviour {
public bool isCounting = false;
public float time = 0;
public bool HT = false;
private void HasTriggered(GameObject tempGO){
HT = true;
}
public void Update()
{
if (HT == false) {
isCounting = true;
Invoke ( "tick", 0f );
}
}
private void tick() {
if(HT == false) {
time += 1;
} else {
isCounting = false;
}
}
void OnGUI(){
GUI.Label (new Rect(10,10,100,50),"score:" + time);
if(HT){
GUI.Label(new Rect(Screen.width / 2 -50, Screen.height / 2 - 75,200,100),"YOUR SCORE WAS " + time);
if(GUI.Button(new Rect(Screen.width / 2 -50, Screen.height / 2 - 25,100,50),"RESTART?")){
Application.LoadLevel(0);
}
}
}
}
again, thank you for helping me.
Answer by BsseeJ · Jul 03, 2014 at 06:18 AM
Hello Greek_Soldier.
In Monobehavior there is a message method called OnTriggerEnter and the rest of the Triggers/colliders.
Here is an example of one: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
I'll try to help and explain it to you because I'm not sure how much you know based off your post.
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
Firstly you have a return type of void, then you must have the "OnTriggerEnter" name to call upon this Monobehavior method. In the parameters of the method you have to have the class Collider as the first arguement, and then name it. This is whatever the game object which you attached this script to collision was, so for example if a bullet hits and enemy and you have this attached to the bullet the enemy's collider component is "other". In their example they name it other, it could be anything but that is what it is, the other collider that hit the game object's collider.
Then if you want to do something to the object that hits the collider you must start with the (in this case) "other" keyword so that the script knows that you want to effect the object colliding with your game object you have this script attached to. Then you use the dot syntax to call it's gameObject component, this is just the component that resembles the entire game object. If you do not do "other.gameObject" the class will think you are only accessing the collision component instead of the entire gameObject.
I hope this helped! There are also videos on the unity Learn section.
PS. Make sure both the colliders being affected are set to triggers in the inspector. PSS. If this is 2D you need to use a 2D collider method not the normal one.
thanks for trying to help, but actually I'm using that method in my current code(I'll edit my post and put it up) and my main problem is that someone could just stand still and completely negate the effects of OnTriggerEnter.
Answer by king_ · Jul 03, 2014 at 04:44 AM
try OnCollisionEnter, OnCollisionExit, OnCollisionStay,
I've tryed all of those all ready and my problem still persists.
Answer by chris3199 · Jul 03, 2014 at 06:16 AM
// Attach this code to your Enemies code.
public void OnTriggerEnter(Collider other)
{
if(other.Name == "Player")
{
//Once the player has triggered the collider
//we can do stuff like,
Destroy(other);
//The line above will destroy the player once it has entered the collider.
}
}
actually my current code is very similar to this but while it eventually does work if the character is moving, if the player stands still he can completely bypass the system.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I ignore trigger objects for collision? 0 Answers
Trigger Collision Moves When Object Enters 1 Answer
Specific GameObject Collision Issue 1 Answer