猿问

Unity 为什么这个触发器不能正常工作

我一直在尝试在 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);

                        }

                    }

                //}

            }

   

牛魔王的故事
浏览 379回答 1
1回答

临摹微笑

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 hallwaypublic GameObject Entrancedoor;public bool isbossdoor;public bool playerisentering;//Door in the bossroompublic GameObject ExitBossdoor;public GameObject SpawnBossRoom;public void OnTriggerStay2D(Collider2D collision){&nbsp; &nbsp; if (collision.gameObject.tag == "Player")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Playerisatdoor = true;&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Player Is Here");&nbsp; &nbsp; &nbsp; &nbsp; //collision.transform.position = ExitBossdoor.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; if (CanOpen == true && Playerisatdoor == true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var PlayerKey = collision.GetComponent<KeyScript>().KeyNumber;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (NeedsKey == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (Input.GetButton("EnterDoor"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (PlayerKey == NeededKeyNumber)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isbossdoor == false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collision.transform.position = ExitBossdoor.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SpawnBossRoom.SetActive(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isbossdoor == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collision.transform.position = Entrancedoor.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SpawnBossRoom.SetActive(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (NeedsKey == false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( playerisentering == true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("ButtonPressed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isbossdoor == false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collision.transform.position = ExitBossdoor.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SpawnBossRoom.SetActive(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isbossdoor == true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collision.transform.position = Entrancedoor.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SpawnBossRoom.SetActive(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private void OnTriggerExit2D(Collider2D collision){&nbsp; &nbsp; if(collision.gameObject.tag == "Player")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Playerisatdoor = false;&nbsp; &nbsp; }}private void Update(){&nbsp; &nbsp; if(Playerisatdoor == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(Input.GetButton("EnterDoor") || Input.GetKey(KeyCode.Q))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartCoroutine("TeleportPlayer");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerisentering = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public IEnumerator TeleportPlayer (){&nbsp; &nbsp; yield return new WaitForSeconds(TpDelay);&nbsp; &nbsp; playerisentering = true;&nbsp; &nbsp; yield return new WaitForSeconds(1);}}// RoomKey Numbers:/*&nbsp;*/
随时随地看视频慕课网APP
我要回答