- Home /
Multiple Choice question to popup when pressing a key
Hello, this is my first post, and I m new to Unity, and i m doing a game for a class. The idea is when your near a door you would press "f" a question would pop up if you answer it right, the door will open if not a "try again" message would show. i have figured out the door opening part, but i m struggling with the question message when f is pressed. please help
here is the script P.S. the script been taking from different posts and reworked for my project.
pragma strict
//Make an empty game object and call it "Door" //Rename your 3D door model to "Body" //Parent a "Body" object to "Door" //Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be //Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger //Assign this script to a "Door" game object that have box collider with trigger enabled //Press "f" to open the door and "g" to close the door //Make sure the main character is tagged "player"
// Smothly open a door var smooth = 1; var DoorOpenAngle = 90.0; var textToShow1: String; var textToShow2: String; var isQuestionaryOn = false;
private var open : boolean; private var enter : boolean;
private var defaultRot : Vector3; private var openRot : Vector3;
function Start(){ defaultRot = transform.eulerAngles; openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z); }
//Main function function Update (){ if(open){ //Open door transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime smooth); }else{ //Close door transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime smooth); }
if(Input.GetKeyDown("f") && enter){ open = !open; } }
function OnGUI(){ if(enter){ GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door"); } } GUI.Label (Rect (20, 10, 500, 30), "we’re much better at a wide variety of behaviors when…"); GUI.Label (Rect (300, 40, 500, 600), textToShow1); GUI.Label (Rect (300, 70, 500, 600), textToShow2);
if(GUI.Button(Rect(20,40,250,20),"We’re thinking about them.")) { textToShow1="Try Again"; } if(GUI.Button(Rect(20,70,250,20),"We’re not thinking about them.")) { isQuestionaryOn=true; textToShow2="well done!"; }
}
//Activate the Main function when player is near the door function OnTriggerEnter (other : Collider){ if (other.gameObject.tag == "Player") { enter = true; } }
//Deactivate the Main function when player is go away from door function OnTriggerExit (other : Collider){ if (other.gameObject.tag == "Player") { enter = false; } }