using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GateControl : MonoBehaviour
{
public float openSpeed = 1f;
private GameObject[] doors;
private bool entered = false;
private bool exited = false;
private Vector3[] doorsOriginPos;
private void Start()
{
doors = GameObject.FindGameObjectsWithTag("Door");
doorsOriginPos = new Vector3[2];
for (int i = 0; i < doors.Length; i++)
{
doorsOriginPos[i] = doors[i].transform.position;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Collider")
{
entered = true;
exited = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Collider")
{
exited = true;
entered = false;
}
}
private void Update()
{
if (entered == true)
{
doors[0].transform.position += Vector3.up * openSpeed * Time.deltaTime;
doors[1].transform.position += Vector3.down * openSpeed * Time.deltaTime;
}
if (exited == true)
{
for (int i = 0; i < doors.Length; i++)
{
if (doors[i].transform.position != doorsOriginPos[i])
{
doors[1].transform.position += Vector3.up * openSpeed * Time.deltaTime;
doors[0].transform.position += Vector3.down * openSpeed * Time.deltaTime;
}
}
}
}
}
Seems too much code or too long and can be simpler.
And the part:
if (doors[i].transform.position != doorsOriginPos[i])
Never true. I want that when the doors are getting close they will stop at the original position and won't continue move up/down nonstop.