- Home /
Why is my door opening and closing at the same time?????????
Im not able to open the door and then close it by touch. My script opens the door and closes it immediately. It does not regonize if the boolean is true or not. it plays the animation at the same time. I want to touch the door then it should open. By the second touch it should close.
I tried a lot but no chance to fix it. The script is attachet to a door!!!
heres my script.//////////////////////////////////////////////////
private var focusObj:GameObject=null; // null Bedeutet das es leer ist
var dorop:boolean= false;
function Update ()
{
if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Began) // checkt ab ob der finger ber¸hrt
{
focusObj = null; // null Bedeutet das es leer ist
var ray =Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit,100000) && hit.transform.tag == "door" && dorop == false)
{
animation.Play("DoorOpenVorrat");
dorop=true;
print ("dorop=true");
focusObj = hit.transform.gameObject; // der Var focusObj wird das GameObjekt zugewiesen.
}
if(Physics.Raycast(ray,hit,100000) && hit.transform.tag == "door" && dorop == true)
{
animation.Play("Door CloseVorrat");
dorop=false;
print ("dorop=false");
}
}
if (focusObj&&Input.touchCount<0&&Input.GetTouch(0).phase == TouchPhase.Ended) // checkt ab ob der finger noch da ist
{
focusObj=null;
}
}
So why it does not work????? Please can help me someone?????
Answer by joni.giuro · Nov 06, 2014 at 04:07 PM
I think it's because on the first frame you touch the door and start the animation and set the dorop to true, then immediately comes the second frame and you probably still have the finger on the door (unless you're really, really fast) and this time the dorop variable is true so the door immediately closes. You should set dorop to true only when the animtion is complete.
But how to accomplish??
yield WaitForSeconds(animation.clip.lenght);
does not work......
Try creating a faction that sets the variable "dorop" to true, and then call that function in that last key frame of the animation ins$$anonymous$$d of on the touch.
Your answer
Follow this Question
Related Questions
Simple Script - and no result 2 Answers
How to make 2 doors with 2 diffrent keys 2 Answers
Open door without using tag 1 Answer