Openable Door with Question
I'm currently making a game using Unity for an EAST project and my way of making it school ready is by making the game prompt the player with a grammatical question when they try opening a door with a key. I like this idea in theory, but I personally don't know how to accomplish this in Unity. For example, when the player walks up to a door with a key in their inventory and try to open the door, I want it to prompt the player with the text "I heard that there food is great, we should eat that instead." Then, it will ask the question "Correct this sentence." The player will then be prompted with the interface showing " needs to be changed to " The user will then be able to input "there" "their", thus filling the blanks with the answer. Upon doing this, the door should open for the player and allow them to go inside. I'm not fully sure if this function is even possible to do in Unity, but it's hypothetically a key feature for my project and any help with this issue would be greatly appreciated. Also it's any help, this is the code that I currently have in my game for the doors to be opened (the code is written in Java):
var smooth = 2.0;
var DoorOpenAngle = 90.0;
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("q") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'Q' to open the door");
}
}
//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;
}
}
Allowing because you formatted your code and that makes us very happy around here. This kind of question is not generally allowed because the answers you need are only a half-hour of independent research away... But maybe another volunteer will help out. This can definitely be done, just to make that clear.
Answer by RobAnthem · Dec 10, 2016 at 08:22 AM
AlwaysSunny is right, but it is actually pretty easy, and to sum it up, this is what you need: 1. a GUI for the text to exist in, with a script that has usable string variable and bool for the event. 2. The door script that checks if the bool from the GUI is true. 3. the GUI asks question, compares string, then finds the door component, also might want to add a var to the door script to differentiate from different doors, and runs the open event.
Your answer
Follow this Question
Related Questions
text code to unlock door 2D 1 Answer
Question about triggers 2 Answers
Open Door Animation Problem 0 Answers
Open/Close Door only if player is near the Door 1 Answer
Door open/close sound effect?? 0 Answers