- Home /
Handling mouse events in objects inside a trigger?
I'm trying to build a turret prefab that detects when an enemy is nearby using a sphere collider, I also want that this turret detects when it's clicked to display information about itself and a menu to upgrade or sell, so here is the problem: the sphere collider that handles enemy detection contains completely the box collider that handles click events how is this usually solved? Is there a better more elegant way to handle mouse events in a game where most units (turrets and enemies) can be clicked as in a RPG?
Here is the code that handles enemy detection:
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Enemy" && myTarget == null) {
myTarget = other.gameObject;
}
}
void OnTriggerExit(Collider other){
if (other.gameObject == myTarget) {
myTarget = null;
}
}
void OnTriggerStay(Collider other){
if (myTarget == null) {
if(other.gameObject.tag == "Enemy"){
myTarget = other.gameObject;
}
}
}
and here is the code that handles mouse events:
public void OnMouseDown() {
print ("mouse down");
gui = true;
}
public void OnMouseUp() {
gui = false;
}
void OnGUI(){
if(gui){
//display menu and handle mouse events
....
}
}
PD: I already tried to separate the colliders in diferent children game objects in the parent turret class without success.
I just solved this adding Raycast hit trigger false on the physics settings, but my question about a better more 'global' way to handle the mouse remains (or maybe it's better if each objects handles the mouse itself?)
Your answer
Follow this Question
Related Questions
GameObject going into terrain. 0 Answers
Check if trigger collider is touching other trigger collider 2 Answers
Get All Renderers inside of GameObejct 0 Answers
C# Make two objects collide to activate a third object 3 Answers
Trigger can't see the enemy 2 Answers