Hello everyone. I seem to be having some raycasting issues. With the code I have written I want the user to click on a certain number of objects. After that I want a question to show up that asks them to click on another object. If I have them right click the object the second time through, after the question is posed, it works. But I would rather have them left click. The problem is that if I have it set to have them left click then after they click on the objects the first time, depending on what they click last, the correct or incorrect response shows up without them clicking on the object a second time. My code follows:
void testQuestionThree()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
string finalClick = hit.collider.gameObject.name;
if(popUpDisplayed == false)
{
if ( Input.GetMouseButtonDown(0) )
{
if (Physics.Raycast (ray, out hit, 100.0f))
{
if(hit.collider.gameObject.name == "learningCylinder2")
{
object1Clicked = true;
}
if(hit.collider.gameObject.name == "learningCube2")
{
object2Clicked = true;
}
// below we add a test, to see if we clicked either object,
// but importantly this only matters if we've clicked both already.
// this determines what we clicked after the request
if(object1Clicked && object2Clicked)
{
allObjectsClicked = true;
object1Clicked = false; //resetting variables to false
object2Clicked = false;
}
if((object1Clicked || object2Clicked) && allObjectsClicked)
{
isClicked = true;
finalClick = hit.collider.gameObject.name;
allObjectsClicked = false; //resetting variables to false.
object1Clicked = false;
object2Clicked = false;
}
}//end physics
}//end input.get
if(allObjectsClicked)
{
if(isClicked == false)
{
GUI.Box (new Rect (350,10,350,120), "Please click the learning cube.");
}//end isClicked false
}
if(isClicked)
{
if(finalClick == "learningCube2")
{
GUI.Box(new Rect (350, 10, 350, 120), "That was correct. You may now continue.");
}
else
{
GUI.Box(new Rect (350, 10, 350, 120), "Sorry, the correct answer was the cube. Click to advance.");
}
navButtonEnabled = true;
}//end if isClicked
}//end popUpDisplayed
}
This is being called in onGUI(). As stated. If I have them right click after the box comes up saying "Please click on learning cube" then it works. But if I them left click then it runs through the whole thing, top to bottom, without the user having to click a second time.