- Home /
Verify Planes collision inside OnMouseOver of another GameObject
I have a script attached to a plane (the background of my 2D game) that creates a Prefab
and when the user moves the cursor, this Prefab
is shown on the current mouse position. This is working fine but now, I have some other Planes above the background and I want to hide my Prefab
when it is over one of these planes.
All the planes over the background and the Prefab created (that is also a plane) have a BoxCollider
Here's my code so far:
void OnMouseOver() {
ChooseTower inst=ChooseTower.getInstance();
string prefab=inst.prefabToPlace();
if (prefab!=null){
Vector3 mousePos=Input.mousePosition;
Vector3 cameraPos=Camera.main.ScreenToWorldPoint(mousePos);
cameraPos.y=2;
if (current_tower!=null)
current_tower.transform.position=cameraPos;
else {
current_tower=(GameObject)Instantiate(Resources.Load(prefab),cameraPos,Quaternion.identity);
}
current_tower.renderer.enabled = true;
foreach (GameObject rectangle in Levels.path.getRectanglesPath()){
if (current_tower.collider.bounds.Intersects(rectangle.collider.bounds)){
current_tower.renderer.enabled = false;
break;
}
}
} else if (current_tower!=null){
Destroy(current_tower);
current_tower=null;
}
}
This code kind of works with Planes that are not rotated. But my problem is that I have some Planes that are rotated and here, it detects collision where it doesn't exist. So maybe I cannot use collider.bounds.Intersects
but, I don't know what else I can use... I simply want to verify if a plane is colliding with another on mouse over using BoxColliders on both planes. How can I do it?