I have a simple Openable door script I got here, and I would like to know how can I add a simple password to open the door. Basically, when the player can open the door using the “F” key, it must provide a box where he must type a certain word (the player must find in the game) for this example we can use the word “Book”. The word doesn’t have to be case sensitive.
Here are the script I am currently using:
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
//Main function
function Update (){
if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}
if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}
if(enter == true){
if(Input.GetKeyDown("f")){
open = !open;
}
}
}
//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;
}
}
Ok so i gave it a shot and re wrote a whole new simple door function, i havent tested it yet so you’l have to get back to me on any errors but im sure its a great starting point, basicly i went a bit more advanced and added little things like sound and distance checking so you wont need a silly trigger collider.
Hopefully you have some understanding with javascript and can see what i did.
var doorUser : GameObject;
var openDoorDistnace : float = 20;
var closeDoorDistance : float = 35;
var openSound : AudioClip;
var closeSound : AudioClip;
var AnimOpen : String;
var AnimClose : String;
var needPassword : boolean = true;
var displayPasswordScreen : boolean = false;
var password : String = "Book";
private var open : boolean = false;
function Start(){
if(doorUser == null)
doorUser = GameObject.FindWithTag("Player");
}
function Update () {
var Distance = (doorUser.transform.position - transform.position).sqrMagnitude;
if(Distance < openDoorDistnace){
if(Input.GetKey("e") !open){
if(needPassword){
displayPasswordScreen = true;
}else{
DoorOpen();
}
}
}
if(Distance > closeDoorDistance){
if(open){
DoorClose();
}
}
}
function DoorClose() {
animation.Play(AnimClose);
audio.PlayOneShot(closeSound);
open = false;
}
function DoorOpen() {
displayPasswordScreen = false;
animation.Play(AnimOpen);
audio.PlayOneShot(openSound);
yield WaitForSeconds(1);
open = true;
}
function OnGUI(){
if(displayPasswordScreen){
passwordToEdit = GUI.PasswordField (Rect (10, 10, 200, 20), password, "*"[0], 25);
if(Input.GetKeyDown("return")){ //or("enter")
if(passwordToEdit == password){ //Maybe try passwordToEdit.Contains(password)?
DoorOpen();
}
}
}
}
Hopefully this doesn’t sound rude but i cant ‘babysit’ somebody through the essentials of unity and basic scripting knowledge.
Basically, attach the script to your door, (assuming it has an open and close animation) if it doesnt then re write it to your own preference, maybe transform rotation or position. Once attached, fill in the variable details, the required password, distance from the door etc.
The preset variable information should be fine for testing, making sure your player is tagged “Player” or the variable doorUser is your player gameobject.
once you start your scene, get in close distance with your object that has the door script attached and press “e” or change the input section of your code to whatever you like. If still nothing is happening throw in some debugs when the door open function is called and see if its being sent through.
Ok,I have substuitute the animation with a open and close code, and when I am in range, the password box appears, but does not open. It might have something to do with the animation code?
var doorUser : GameObject;
var openDoorDistnace : float = 20;
var closeDoorDistance : float = 35;
var openSound : AudioClip;
var closeSound : AudioClip;
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
//var AnimOpen : String;
//var AnimClose : String;
var needPassword : boolean = true;
var displayPasswordScreen : boolean = false;
var password : String = "Book";
private var open : boolean = false;
function Start(){
if(doorUser == null)
doorUser = GameObject.FindWithTag("Player");
}
function Update () {
var Distance = (doorUser.transform.position - transform.position).sqrMagnitude;
if(Distance < openDoorDistnace){
if(Input.GetKey("e") !open){
if(needPassword){
displayPasswordScreen = true;
}else{
DoorOpen();
}
}
}
if(Distance > closeDoorDistance){
if(open){
DoorClose();
}
}
}
function DoorClose() {
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
audio.PlayOneShot(closeSound);
open = false;
}
function DoorOpen() {
displayPasswordScreen = false;
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
audio.PlayOneShot(openSound);
yield WaitForSeconds(1);
open = true;
}
function OnGUI(){
if(displayPasswordScreen){
passwordToEdit = GUI.PasswordField (Rect (10, 10, 200, 20), password, "*"[0], 25);
if(Input.GetKeyDown("enter")){ //or("enter")
if(passwordToEdit == password){ //Maybe try passwordToEdit.Contains(password)?
DoorOpen();
}
}
}
}
Also, how do add some text to the password box, for a password hint?
Thanks for the help, appreciate it, as I am building this project as a educational game for my kid, to help him with concentration problems.
Press enter when you enter the password, that’s how I wrote the code, if it still doesn’t work, as mentioned before ad a debug.log statement in certain sections to see if its getting passed through
Please read the script and understand it I’ve added notes, for the input of return you might have to change it to enter for a pc as I’m on a Mac
Good day Sir,
I have tried in many ways,but your code is not working. I have recorded a simple door animation using the animation tool in Unity, naming it AnimOpen and AnimClose.
When not using a password ( the box unticked) I get the following debug error on pressing ‘e’
Are you still using the code from the latest post, or did you go back to a version that uses animations? Since the error mentions line 95, what’s on that line?
Please read the errors before relying on fully detailed translation. Obviously the error says the animation you created can not be found, please take a look at scripting referances before updating ever problem you encounter. As this thread (scripting) is not provided for users to create code for them, just provide a basic understanding of wht they are trying to achieve. My script provided is a perfect expample and starting point for creating your own unique code made especially for your build. I cannot see any errors in the code i provided, only on your part (inside of unity).
Completely replace the open and close functions to this for development purposes and you should see a console message debugging if the door has opened or closed, then go and enter whatever you want to happen when the door ahs opened or closed such as an animation or audio.
function DoorClose() {
Debug.Log(“Door Has Closed”);
yield WaitForSeconds(1); //wait for the door to close
open = false;
}
function DoorOpen() {
displayPasswordScreen = false;
Debug.Log(“Door Has Opened”);
yield WaitForSeconds(1); //wait for the door to open
open = true;
}
That was quite rude considering i went out of my time to check up on this post and took the time to write a code as an example. I have nothing more to say
Please accept my apology for being rude. It was not my intention. You are right, I appreciate you took the time for trying to help. I will remove my post.
However, I would rather go for a way to the door with a key the user must pick up instead.