0
\$\begingroup\$
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.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Your code is actually fine. I got rid of the 'exited' bool because you don't need it. I also converted your two 'if statements' inside your Update to an 'if/else' statement.

As for the the line

 `if (doors[i].transform.position != doorsOriginPos[i])` 

The reason that isn't returning true is because the door won't always return to its exact original position; it might be one decimal place off. For that reason, I replaced it with Vector3.Distance

Let me know what you think

using UnityEngine;

public class GateControl : MonoBehaviour
{
    public float openSpeed = 1f;

    private GameObject[] doors;
    private bool entered = 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;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Collider")
        {
            entered = false;
        }
    }

    private void Update()
    {
        if (entered)
        {
            doors[0].transform.position += Vector3.up * openSpeed * Time.deltaTime;
            doors[1].transform.position += Vector3.down * openSpeed * Time.deltaTime;
        }
        else
        {
            for (int i = 0; i < doors.Length; i++)
            {
                if (Mathf.Abs(Vector3.Distance(doors[i].transform.position, doorsOriginPos[i])) >= .01f)
                {
                    doors[1].transform.position += Vector3.up * openSpeed * Time.deltaTime;
                    doors[0].transform.position += Vector3.down * openSpeed * Time.deltaTime;
                }
            }
        }
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .