- Home /
How to detect if an object is within another object??? (Hide and seek type game). Thanks!
Summary of whats already working and what is going on:
(IMAGE ONE) 1. A hider first selects a piece of furniture to hide their object in. When they submit, the object translates to the position of the furniture. When the object collides with the chosen furniture, the object gets hidden (disabled) inside the chosen furniture.
(IMAGE TWO) 2. The detectives (3 of them) take turns tapping (highlighting) furniture they think it is under. At the end of the turn, if the object is hidden under any of the highlighted furniture, the object is found (enabled).
if (Input.GetMouseButtonDown(0))
{
//Send raycast to hit a game object
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
text.text = question;
// Casts the ray and get the first game object hit
if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerToHit)) {
lastClicked = hit.collider.gameObject.transform;
clickedGameObject = hit.collider.gameObject;
tapLastClicked = hit.collider.gameObject.transform;
//THIS IS FOR THE HIDER SELECTING AND SENDING OBJECT TO DESIRED FURNITURE
if (hit.collider.tag == "Object" && lastClicked != null && spawned == false && sessionCounter == 0 && taptaptapSelect.enabled == false) {
//Debug.Log ("This hit at " + hit.transform.name);
print (lastClicked.name);
objectSelect.enabled = true;
objectIsHere = lastClicked;
moveOn = false;
//THIS IS FOR THE FIRST DETECTIVE HIIGHLIGHTING A SPECIFIC FURNITURE
} else if (hit.collider.tag == "Object" && lastClicked != null && taptaptapSelect.enabled == true ) {
//Debug.Log ("This hit at " + hit.transform.name);
rend = clickedGameObject.GetComponent<Renderer> ();
tapOneOriginalMaterial = rend.material;
//tapOne = lastClicked.gameObject;
print (clickedGameObject.gameObject.name );
print ("Tap One");
//tapCounter += 1;
tapOneBool = true;
rend.material = diffuseShaderPlayerOne;
//What is the best method to detect when the object is under one of the highlighted furniture? I've tried many different methods and no luck so far. All my code is working, other than this system I'm trying to get going. I'd appreciate all the help
Answer by JigneshKoradiya · Apr 07, 2016 at 06:05 AM
i can give some idea about how you can do it
first you have two object object-A and Object-B
you have to use boxcollider and make check box ON "is trigger" from inspector and than refer to this script
http://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html
here is you can check if Object-A is in Object-B than you can get event in OntriggerStay method
Answer by saschandroid · Apr 07, 2016 at 06:45 AM
You could probably use something like this on your objects:
if( GetComponent<MeshFilter>().mesh.bounds.Contains(/* position of the hidden object*/))
{
Debug.Log("Bounding box contains hidden object!");
}