- Home /
Open door without using tag
Hi, so i'm having a little problem with my open door script.
I'm making a multiplayer game and i use this script to open a door:
var smooth = 2.0;
var DoorOpenAngle = 90.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
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");
}
}
//Activate the Main function when player is near the door
function OnTriggerStay (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;
}
}
Well obviously every player has the tag "player" so any player can open the door , even if their not inside the trigger.
My question is: is there any other way to do this without using tag? (or any other solution?).
Thank you
Answer by Bladesfist · Jul 04, 2013 at 04:41 PM
Another more "natural" solution could be that things open the door and the door has just one function. It could then be called by a script that is attached to the player.
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 5f))
{
if (hit.collider.gameObject.GetComponent<DoorOpenAngle>() != null)
{
DoorOpenAngle.Open(); //you will need to expose this method for it to work
}
}
}
ahhh Thank you. So all i have to do is make the "DoorOpenAngle" function and put it on the door?
Yes, all you have to do is make whatever code you have to open the door public and then change DoorOpenAngle.Open() to whatever the method is called.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scripting issue with opening a door with a key. 2 Answers
Simple Script - and no result 2 Answers
How to make 2 doors with 2 diffrent keys 2 Answers
my script not working..!!! 1 Answer
Script for a door that requires (9 papers) to open 0 Answers