C# - How can i solve this door problem in script? Can someone help me?
My doubt is - i'm doing a script with the objective to open the door if the character that enters in the trigger are holding the "object3", and i do it without problem, but now i see this problem -
If the character exit and enter again in the trigger the "open" animation starts again and it's feels so weird see the door teleports from the ceiling to the floor (i'm using a scifi-door that i found in asset store, link below).
I know if i do the OnTriggerExit() the door can close when the character exits the trigger, but i asked to my friend to test with me something and i got this - if other player with the "object3" enters in the trigger at the same time with another player the door do the same thing for the 2 players, in other words, the door open 2 times, and if i enter in the trigger when the door is closing the door teleports to the ceiling too.
I don't know how to solve this, someone can help me?
I'm using this door for all the tests - https://www.assetstore.unity3d.com/en/#!/content/21813
public class Door2 : MonoBehaviour {
public GameObject object, door;
public Animation anim;
void Start(){
anim = door.GetComponent<Animation>();
}
void OnTriggerEnter ( Collider obj ){
door = GameObject.FindWithTag ("Door");
//if the character enter with the 3rd object and the door are closed, this code runs
if ((object.GetComponent<Renderer> ().sharedMaterial.IsKeywordEnabled ("Object3")) && (!anim.isPlaying) && (door.GetComponent<Animation>().name != "open")) {
door = GameObject.FindWithTag ("Door");
door.GetComponent<Animation> ().Play ("open");
door.GetComponent<Animation> ().wrapMode = WrapMode.Once;
//here i try to do something like - if the character enters again in the trigger and the door are open, the door don't do the "open" animation again
if ((object.GetComponent<Renderer> ().sharedMaterial.IsKeywordEnabled("Object3")) && (anim.isPlaying) && (door.GetComponent<Animation>().name == "open")) {
return;
}
return;
}
}
Answer by IMemeManI · Jan 17, 2018 at 04:17 PM
Use a bool to detect if it's open or closed.
Surround your code in this
if(!isOpen)
{
//code to open your door
isOpen = true;
}
Make sure you declare the "isOpen" bool.
Also, using the Find method is something you should avoid if possible.