How to open the door when I need

I have a door with buttons on both sides of the door. Each button should open the door.

When I press button 1, the door opens, press it again, the door closes. But if I press Button1, then press Button2, the door will open again, and not close.

enter image description here

I open / close using animation. I can’t understand what’s wrong.

+4
source share
1 answer

It is very important that you

do not use static variables

in this case.

You will be pleased to know that the solution is simple. Have a script calledAttachToDoor

public void AttachToDoor()
 {
 private bool isOpen;
 public float doorSpeed;
 etc etc
 public void OpenCloseDoor()
   {
   your code to open/close a door
   }
 }

Drag AttachToDoorto your door. Notification OpenCloseDoor()marked public.

Next, ANOTHER, SEPARATE script, AttachToButton

public void AttachToButton()
{
public AttachToDoor amazaingDoorScript;
etc etc
void Update()
 {
  if (Input.GetButton("Fire1"))
   if (DoPlayerLookAtButton() && isAnimationReadyToPlay)
    amazaingDoorScript.OpenCloseDoor();
 }
}

AttachToButton "".

"amazaingDoorScript".

AttachToDoor amazaingDoorScript.

... , .

AttachToButton .

script, , , :

   public AttachToDoor amazaingDoorScript; ...
   Invoke("test",Random.Range(5f,10f)); ...
   private void test()
     {
     // have a ghost mess with the door occasionally
     amazaingDoorScript.OpenCloseDoor();
     Invoke("test",Random.Range(5f,10f));
     }

Unity, , . !


, , UnityEvent.

Unity, UnityEvent. UnityEvent Unity.

: fooobar.com/questions/1627429/...

public void AttachToButton()
{
public UnityEvent buttonClicked;
 void Update()
 {
  if (Input.GetButton("Fire1"))
   if (DoPlayerLookAtButton() && isAimationReadyToPlay)
    {
    Debug.Log("Button pressed!");
    if (buttonClicked!=null) buttonClicked.Invoke();
    }
 }
}

. , , , Log "!".

, .

, , AttachToButton.

. . buttonClicked. , ... OpenCloseDoor.

, . ! ?

+4

Source: https://habr.com/ru/post/1627428/


All Articles