简体   繁体   English

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

[英]Constant Enemy Damage to Player - Unity 2019.1 Beta

Basically I have an enemy and a player. 基本上我有一个敌人和一个玩家。 When the enemy or player bump into one one-another, the damage occurs and the player is injured resulting in a loss of HP. 当敌人或玩家碰到一对一时,造成伤害并且玩家受伤导致HP失去。 However, once contact is made, it only damages one time. 但是,一旦联系,它只会损坏一次。 How to I go about insuring that damage is being dealt to the player non-stop over the duration of the entire length of the collision? 如何确保在整个碰撞期间不间断地对玩家造成伤害?

I am setting up a bool variable isTouching and setting it to true on contact. 我正在设置一个bool变量isTouching并在接触时将其设置为true。 then set a while loop up that will constantly decrement the player's HP. 然后设置一个while循环,这将不断减少播放器的HP。 Then use OnCollisionExit to set the isTouching to false. 然后使用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;
}

The while loop freezes my computer so I Don't recommend you running it with the while loop on there. while循环冻结了我的计算机,所以我不建议你在那里运行while循环。 Anyway, what I need to know is how to achieve what I'm trying to do. 无论如何,我需要知道的是如何实现我想要做的事情。 How can I make the enemy constantly damage the player? 如何让敌人不断伤害玩家? For example, if I jump on top of the enemy, it'll damage one time then I can ride him around. 例如,如果我跳到敌人的顶部,它会损坏一次然后我可以骑他。 Which I want damage non-stop to force the player to move off or die eventually. 我想要不停的伤害迫使玩家离开或最终死亡。

OnCollisionEnter2D is meant to trigger only the first frame the same collision occurs. OnCollisionEnter2D仅用于触发相同碰撞发生的第一帧。

You want to use OnCollisionStay2D() , which triggers on every frame with a collision. 你想使用OnCollisionStay2D() ,它会在碰撞的每一帧上触发。

Also, get rid of the while loop. 另外,摆脱while循环。 The game can't update (including calling OnCollisionExit2D ) until the method terminates. 在方法终止之前,游戏无法更新(包括调用OnCollisionExit2D )。

private void OnCollisionStay2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Enemy01")
    {
        GetComponent<AudioSource>().Play();

        // To take 5 - 30 damage per second:
        HPBAR.PlayHP -= Random.Range(5f,30f) * Time.deltaTime;

        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");
        }

    } // of player tag check

}// end of collision stay

private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Enemy01")
    {
        // put "i quit touching the hurty thing" stuff here.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM