I'm having an odd issue in my implementation of distance based object interaction in Unity. Basically it's a simple "Press [key] to [search/pickup/open/etc]" function which requires the player to be within a certain distance of the object before interaction is allowed (via mouse over). In my scene I have 2 test objects which I have set up as similarly as possible, but they are acting differently and I cannot seem to work out why. * http://imgur.com/a/hoAIH * http://imgur.com/a/HbvrO These are the setups, and the script which is on both parent objects is as follows:
public class DisplayUI : MonoBehaviour
{
public string itemName;
bool isSelected;
bool isNear;
void OnTriggerEnter(Collider col)
{
isNear = true;
}
void OnTriggerExit(Collider col)
{
isNear = false;
}
void OnMouseOver()
{
if (isNear)
isSelected = true;
else
isSelected = false;
}
void OnMouseExit()
{
isSelected = false;
}
void OnGUI()
{
if(isSelected == true)
{
GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 200, 20), "Press E to search " + itemName);
}
}
}
For some reason the sphere/cube game object is acting as desired where the mouseover detects the edge of each mesh perfectly, and relies on the distance (within the trigger collider) too, creating the gui label as coded. The trash can however seems to think the main trigger collider is the edge of the mesh so even when the mouse is way outside the physical mesh, the gui label is showing. Where am I going wrong with this?