为什么只从第二扇门关上门?第一次入住

时间:2019-03-09 06:23:01

标签: c# unity3d

第一个脚本和第二个脚本都附在门上。 在这种情况下,我有12门。 无论播放器控制器或NPC进入第12个门的哪个门,门都将打开但保持打开状态。播放器控制器或NPC将进入的下一扇门将打开,然后也将关闭,然后关闭所有门。

但每次第一扇门永远都不会关闭。每次运行游戏时,它仅在第二个门上起作用。

在此脚本HoriDoorManager上,我使用的是公共静态标志exitedDoor,并将其在OnTriggerExit中设置为true:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HoriDoorManager : MonoBehaviour
{
    public static bool exitedDoor = false;

    private bool doorLockState;
    private List<DoorHori> doors = new List<DoorHori>();

    private void Start()
    {
        if (transform.parent != null)
        {
            Transform parent = transform.parent;
            var children = parent.GetComponentsInChildren<Transform>();

            if(children != null)
            {
                foreach (Transform door in children)
                {
                    if (door.name == "Door_Left" || door.name == "Door_Right")
                        doors.Add(door.GetComponent<DoorHori>());
                }
            }
        }
    }

    void OnTriggerEnter()
    {
        if (doorLockState == false)
        {
            if (doors != null)
            {
               for(int i =0; i < doors.Count; i++)
                {
                    doors[i].OpenDoor();
                }
            }
        }
    }

    private void OnTriggerExit(Collider collide)
    {
        if (doorLockState == false)
        {
            exitedDoor = true;
        }
    }

    public void ChangeLockState(bool lockState)
    {
        doorLockState = lockState;
    }
}

在第二个脚本中,我正在检查exitedDoor标志是否为真,然后开始关闭门:在方法WaitToClose中:

using UnityEngine;
using System.Collections;

public class DoorHori : MonoBehaviour
{

    public float translateValue;
    public float easeTime;
    public OTween.EaseType ease;
    public float waitTime;

    private Vector3 StartlocalPos;
    private Vector3 endlocalPos;

    private void Start()
    {
        StartlocalPos = transform.localPosition;
        gameObject.isStatic = false;
    }

    public void OpenDoor()
    {
        OTween.ValueTo(gameObject, ease, 0.0f, -translateValue, easeTime, 0.0f, "StartOpen", "UpdateOpenDoor", "EndOpen");
        GetComponent<AudioSource>().Play();
    }

    private void UpdateOpenDoor(float f)
    {
        Vector3 pos = transform.TransformDirection(new Vector3(1, 0, 0));
        transform.localPosition = StartlocalPos + pos * f;

    }

    private void UpdateCloseDoor(float f)
    {
        Vector3 pos = transform.TransformDirection(new Vector3(-f, 0, 0));

        transform.localPosition = endlocalPos - pos;

    }

    private void EndOpen()
    {
        endlocalPos = transform.localPosition;
        StartCoroutine(WaitToClose());
    }

    private IEnumerator WaitToClose()
    {
        if (HoriDoorManager.exitedDoor == true)
        {
            yield return new WaitForSeconds(waitTime);
            OTween.ValueTo(gameObject, ease, 0.0f, translateValue, easeTime, 0.0f, "StartClose", "UpdateCloseDoor", "EndClose");
            GetComponent<AudioSource>().Play();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试在第一个脚本中将OnTriggerEnter更改为此。

private void OnTriggerEnter(Collider collide){
    if (doorLockState == false)
    {
        if (doors != null)
        {
           for(int i =0; i < doors.Count; i++)
            {
                doors[i].OpenDoor();
            }
        }
    }
}

删除方法的参数时,您没有使用unity定义的方法,因此不再引用相同的OnTriggerEnter。

然后它还可以用于检查是什么触发了on enter标志,因为我假设您不希望任何冲突触发此逻辑。