1

我有一个空对象,其中包含下面的脚本。我要实现的是,如果敌人进入此触发框,则玩家无法做任何事情(他们被惊呆了)。直到他们按下“W”,敌人才会出现。我的setActive() false问题是当我按下“W”时,即使敌人不再活跃在现场,角色也会长时间保持昏迷状态。我希望玩家按下“W”的那一刻,眩晕就完全消失并且不会持续很长时间。我还注意到,如果我按下“W”键,则需要一段时间才能进行确认,OnTriggerExit然后才会注意到。

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

public class NoCasting : MonoBehaviour 
{

    [SerializeField]
    private Image customImage;

    //public Animator anim;

    public Casting stop;
    public AudioSource source;
    public AudioSource negative;
    public AudioSource helps;
    public AudioSource positive;
    public ParticleSystem stun;


    // Use this for initialization
    void Start () 
    {
        //anim = GetComponent<Animator> ();
        source = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update () {

    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Enemy")
        {
            customImage.enabled = true;

            Debug.Log ("is working trigger");
            stop.GetComponent<Casting> ().enabled = false;
            source.Play ();
            negative.Play ();
            stun.Play ();
            //anim.Play ("DaniAttack");
        }

    }

    private void OnTriggerExit(Collider other)
    {
        if (Input.GetKeyDown ("w"))
        {
            Debug.Log ("is ended trigger");
            stun.Stop ();
            negative.Stop ();
            stop.GetComponent<Casting> ().enabled = !stop.enabled;
            customImage.enabled = false;
            helps.Play ();
            positive.Play ();
        }
    }
}
4

2 回答 2

0

由于无论如何您都必须检测输入Update,我真的不明白为什么OnTriggerExit在这种情况下您仍然需要(因为您还希望它是即时的)。

只需执行以下操作:

private bool isStunned = false;     // Add this so code isn't run when not stunned

private void OnTriggerEnter(Collider other)
{
    if(isStunned == false && other.gameObject.tag == "Enemy")
    {
        Stun();
    }
}

private void Update()
{
    if(isStunned == true && Input.GetKeyDown("w"))
    {
        Unstun();
    }
}

public void Stun()
{
    isStunned = true;

    // Do the stuff for stunning
}

public void Unstun()
{
    // Do the stuff that is needed for unstunning

    isStunned = false;
}

另外,另一方面:由于您的stop成员是 type Casting,因此您无需编写stop.GetComponent<Casting>().enabled,stop.enabled就足够了。(我想,可能是GameObject stop在某个时候。)

于 2018-05-03T09:01:25.610 回答
0

您正在检查其中的输入,OnTriggerExit只有当敌人从触发区域出去时才会起作用。(即,如果他仍在比赛附近,则"W"甚至不会检查新闻界)。

您可以尝试以下解决方案:

添加一个布尔成员,表示玩家被击晕(在 if 语句中将其设置为truein并将其设置为 false on )OnTriggerEnterOnTriggerExit

将“W”按键检查块移至Update()方法。

于 2018-05-03T07:21:22.253 回答