Unity为什么此触发器无法正常工作

时间:2018-05-09 20:43:29

标签: c# unity3d

我一直试图在Unity C#中打开一扇门,而且我在大多数情况下让它发挥作用,看起来似乎不起作用的是当玩家在静止时仍然在触发器内时让玩家通过。当我在触发器中移动时,门可以正常工作,但是当我静止不动时,当按下打开的门按钮时,传送播放器似乎是即时的,我知道如何解决。

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

public class BaseDoorScript : MonoBehaviour {

public float NeededKeyNumber;
public float TpDelay;

public bool CanOpen;
public bool NeedsKey;
public bool Playerisatdoor;

//Door in the hallway
public GameObject Entrancedoor;
public bool isbossdoor;
public bool playerisentering;
//Door in the bossroom
public GameObject ExitBossdoor;


public GameObject SpawnBossRoom;
public void OnTriggerStay2D(Collider2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        Playerisatdoor = true;
        Debug.Log("Player Is Here");
        //collision.transform.position = ExitBossdoor.transform.position;
        if (CanOpen == true && Playerisatdoor == true)
        {
            var PlayerKey = collision.GetComponent<KeyScript>().KeyNumber;
            if (NeedsKey == true)
            {
                //if (Input.GetButton("EnterDoor"))
                //{
                    if (PlayerKey == NeededKeyNumber)
                    {

                        if (isbossdoor == false)
                        {
                            collision.transform.position = ExitBossdoor.transform.position;
                            SpawnBossRoom.SetActive(true);
                        }
                        if (isbossdoor == true)
                        {
                            collision.transform.position = Entrancedoor.transform.position;
                            SpawnBossRoom.SetActive(false);
                        }
                    }
                //}
            }
            if (NeedsKey == false)
            {
                if ( playerisentering == true
                    //Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q)
                    )
                {
                    Debug.Log("ButtonPressed");
                    if (isbossdoor == false)
                    {
                        collision.transform.position = ExitBossdoor.transform.position;
                        SpawnBossRoom.SetActive(true);
                    }
                    if (isbossdoor == true)
                    {
                        collision.transform.position = Entrancedoor.transform.position;
                        SpawnBossRoom.SetActive(false);
                    }
                }
            }
        }
    }
}
private void OnTriggerExit2D(Collider2D collision)
{
    if(collision.gameObject.tag == "Player")
    {
        Playerisatdoor = false;
    }
}
private void Update()
{
    if(Playerisatdoor == true)
    {
        if(Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q))
        {
            StartCoroutine("TeleportPlayer");
        }
        else
        {
            playerisentering = false;
        }
    }
}
public IEnumerator TeleportPlayer ()
{
    yield return new WaitForSeconds(TpDelay);
    playerisentering = true;
    yield return new WaitForSeconds(1);
}
}

1 个答案:

答案 0 :(得分:0)

Woops忘了提到我刚才修复了这种情况,我想我会把脚本用来帮助那些有类似问题的人。

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

public class BaseDoorScript : MonoBehaviour {

public float NeededKeyNumber;
public float TpDelay;

public bool CanOpen;
public bool NeedsKey;
public bool Playerisatdoor;

//Door in the hallway
public GameObject Entrancedoor;
public bool isbossdoor;
public bool playerisentering;
//Door in the bossroom
public GameObject ExitBossdoor;


public GameObject SpawnBossRoom;
public void OnTriggerStay2D(Collider2D collision)
{
    if (collision.gameObject.tag == "Player")
    {
        Playerisatdoor = true;
        Debug.Log("Player Is Here");
        //collision.transform.position = ExitBossdoor.transform.position;
        if (CanOpen == true && Playerisatdoor == true)
        {
            var PlayerKey = collision.GetComponent<KeyScript>().KeyNumber;
            if (NeedsKey == true)
            {
                //if (Input.GetButton("EnterDoor"))
                //{
                    if (PlayerKey == NeededKeyNumber)
                    {

                        if (isbossdoor == false)
                        {
                            collision.transform.position = ExitBossdoor.transform.position;
                            SpawnBossRoom.SetActive(true);
                        }
                        if (isbossdoor == true)
                        {
                            collision.transform.position = Entrancedoor.transform.position;
                            SpawnBossRoom.SetActive(false);
                        }
                    }
                //}
            }
            if (NeedsKey == false)
            {
                if ( playerisentering == true
                    //Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q)
                    )
                {
                    Debug.Log("ButtonPressed");
                    if (isbossdoor == false)
                    {
                        collision.transform.position = ExitBossdoor.transform.position;
                        SpawnBossRoom.SetActive(true);
                    }
                    if (isbossdoor == true)
                    {
                        collision.transform.position = Entrancedoor.transform.position;
                        SpawnBossRoom.SetActive(false);
                    }
                }
            }
        }
    }
}
private void OnTriggerExit2D(Collider2D collision)
{
    if(collision.gameObject.tag == "Player")
    {
        Playerisatdoor = false;
    }
}
private void Update()
{
    if(Playerisatdoor == true)
    {
        if(Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q))
        {
            StartCoroutine("TeleportPlayer");
        }
        else
        {
            playerisentering = false;
        }
    }
}
public IEnumerator TeleportPlayer ()
{
    yield return new WaitForSeconds(TpDelay);
    playerisentering = true;
    yield return new WaitForSeconds(1);
}
}




// RoomKey Numbers:
/* 



*/