How to check if a game object is inside another
By mapping the player game object to the cursor, I want to deactivate a game object when the player game object is above it. How would I do this? I'm working in Unity 2D. Here was my attempt:
void KeyPress(Vector3 mousePos)
{
if (Input.GetKeyDown(KeyCode.X))
{
if (hitObject.transform.position == mousePos)
{
hitObject.SetActive(false);
}
}
}
"mousePos" is the position of the cursor, and "hitObject" is the game object i want to deactivate when the cursor is above it.
When i place the cursor above the hit object and hit "X", nothing happens. I figured this was because the position of the player game object was not exactly equal to the position of the game object I want to deactivate, but after setting the position of both game objects to origin and trying again, it did not work.
I feel as if even if I found the solution, the cursor would still need to be exactly on the game object. How would I make it so that it works when the player game object is just inside/above the game object (which is a circle) I'm trying to deactivate?
Could it have anything to do with how i've referenced the "hitObject" game object?
private GameObject hitObject;
void Start()
{
hitObject = GameObject.Find("Hit Object");
}
I'm not getting any compiler errors either.