I have a door and buttons on the both sides of the door, which open it.
When I press Button1, door opens. When I press it again, door closed.
But if I will press Button1 (door opens), then walk into the room and press Button2, the door will opens again, but this time I need to close it.
I open/close using animation. Can’t understand, what is wrong
public Animation button1;
public Animation button2;
public Animation gate;
public AudioSource myAudio;
private bool isDoorClosed;
private bool isAimationReadyToPlay = true;
private Collider thisCollider;
public void Start()
{
thisCollider = GetComponent<Collider>();
}
void Update()
{
if (Input.GetButton("Fire1"))
if (DoPlayerLookAtButton() && isAimationReadyToPlay)
OpenCloseDoor();
}
bool DoPlayerLookAtButton()
{
RaycastHit _hit;
Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
bool isHit = Physics.Raycast(_ray, out _hit, 1.5f);
if (isHit && _hit.collider == thisCollider) return true;
else return false;
}
void OpenCloseDoor()
{
myAudio.Play();
StartCoroutine("DelayBetweenAnimations");
if (!isDoorClosed) // Play animation with normal speed
{
button1["pressDoorButton"].speed = 1.0f;
button1.Play();
button2["pressDoorButton"].speed = 1.0f;
button2.Play();
gate["openDoor"].speed = 1.0f;
gate.Play();
}
if (isDoorClosed) // Play animation with revert speed
{
button1["pressDoorButton"].speed = -1.0f;
button1["pressDoorButton"].time = button1["pressDoorButton"].length;
button1.Play();
button2["pressDoorButton"].speed = -1.0f;
button2["pressDoorButton"].time = button2["pressDoorButton"].length;
button2.Play();
gate["openDoor"].speed = -1.0f;
gate["openDoor"].time = gate["openDoor"].length;
gate.Play();
}
isDoorClosed = !isDoorClosed;
}
IEnumerator DelayBetweenAnimations()
{
isAimationReadyToPlay = false;
yield return new WaitForSeconds(0.5f);
isAimationReadyToPlay = true;
}