为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
牛魔王的故事 2021-06-03 14:58:50
我一直在尝试在 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 hallwaypublic GameObject Entrancedoor;public bool isbossdoor;public bool playerisentering;//Door in the bossroompublic 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);                        }                    }                //}            }   
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

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:

/* 




*/



查看完整回答
反对 回复 2021-06-05
  • 1 回答
  • 0 关注
  • 343 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信