对玩家的持续敌人伤害 - Unity 2019.1 Beta

基本上我有一个敌人和一个玩家。当敌人或玩家相互碰撞时,会造成伤害并导致玩家受伤导致HP损失。但是,一旦接触,它只会损坏一次。如何确保在整个碰撞过程中不间断地对玩家造成伤害?


我正在设置一个布尔变量 isTouching 并在接触时将其设置为 true。然后设置一个while循环,不断减少玩家的HP。然后使用 OnCollisionExit 将 isTouching 设置为 false。


private bool isTouching; // determines if the player is 

touching

private void OnCollisionEnter2D(Collision2D collision)

{

    isTouching = true;



        if (collision.gameObject.tag == "Enemy01")

        {

        while (isTouching == true)

        {

            GetComponent<AudioSource>().Play();

            HPBAR.PlayHP -= Random.Range(5, 30);

            if (HPBAR.PlayHP < 0)// Insures player HP bar doesn't read less 

            than zero in GUI TXT

            {

                HPBAR.PlayHP = 0;


            }

            // Debug.Log("Ouch! " + HPBAR.PlayHP + " Left");

            if (HPBAR.PlayHP <= 0 && HPBAR.lives > 0)

            {


                HPBAR.lives--;

                HPBAR.LPfloat--;

                p5.transform.position = spawner.transform.position;

                HPBAR.PlayHP = Random.Range(100, 150);

                // Debug.Log("You're Dead!!! Lives left: " + (HPBAR.lives + 1));


            }

            else if (HPBAR.PlayHP <= 0 && HPBAR.lives <= 0)

            {


                SceneManager.LoadScene("DeathScene");


            }

        }//end of while



        } // of player tag check


}// end of collision enter


private void OnCollisionExit2D(Collision2D collision)

{

    isTouching = false;

}

while 循环会冻结我的计算机,因此我不建议您在其中使用 while 循环运行它。无论如何,我需要知道的是如何实现我想要做的事情。如何让敌人不断伤害玩家?例如,如果我跳到敌人的顶部,它会造成一次伤害,然后我可以骑着他。我希望不间断地造成伤害,以迫使玩家最终离开或死亡。


紫衣仙女
浏览 320回答 1
1回答

翻阅古今

OnCollisionEnter2D旨在仅触发发生相同碰撞的第一帧。您想使用OnCollisionStay2D(),它会在每一帧发生碰撞时触发。此外,摆脱while循环。OnCollisionExit2D在方法终止之前,游戏无法更新(包括调用)。private void OnCollisionStay2D(Collision2D collision){&nbsp; &nbsp; if (collision.gameObject.tag == "Enemy01")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; GetComponent<AudioSource>().Play();&nbsp; &nbsp; &nbsp; &nbsp; // To take 5 - 30 damage per second:&nbsp; &nbsp; &nbsp; &nbsp; HPBAR.PlayHP -= Random.Range(5f,30f) * Time.deltaTime;&nbsp; &nbsp; &nbsp; &nbsp; if (HPBAR.PlayHP < 0)// Insures player HP bar doesn't read less than zero in GUI TXT&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HPBAR.PlayHP = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Debug.Log("Ouch! " + HPBAR.PlayHP + " Left");&nbsp; &nbsp; &nbsp; &nbsp; if (HPBAR.PlayHP <= 0 && HPBAR.lives > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HPBAR.lives--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HPBAR.LPfloat--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p5.transform.position = spawner.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HPBAR.PlayHP = Random.Range(100, 150);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Debug.Log("You're Dead!!! Lives left: " + (HPBAR.lives + 1));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (HPBAR.PlayHP <= 0 && HPBAR.lives <= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SceneManager.LoadScene("DeathScene");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } // of player tag check}// end of collision stayprivate void OnCollisionExit2D(Collision2D collision){&nbsp; &nbsp; if (collision.gameObject.tag == "Enemy01")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // put "i quit touching the hurty thing" stuff here.&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP