简体   繁体   English

在Unity中重生后停止死亡动画

[英]Stop death animation after respawn in Unity

So I have a player that will start a "hurt" animation when he collides with an enemy. 因此,我有一个玩家与敌人碰撞时将开始“伤害”动画的玩家。 After 2 seconds he will respawn back to his starting position, but the "hurt" animation doesn't stop. 2秒后,他将重新生成自己的起始位置,但“伤害”动画不会停止。 It just keeps on playing even when the player is moving around. 即使玩家在四处移动,它也可以继续播放。

This is my whole Player Script : 这是我的整个播放器脚本:

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

public class Player : MonoBehaviour
{
    public float speed = 3.0f;
    public Vector2 jumpHeight;
    [HideInInspector] public bool facingRight = true;
    private float minX = -10.5f;
    private float maxX = 10.5f;
    public int count;
    public Text countText;
    private Animator anim;
    public GameObject NECText;
    public GameObject WinText;
    public GameObject player;

    void Start()
    {
        anim = GetComponent<Animator>();
        transform.position = new Vector3(-10.0f, -2.32f, 0);
        count = 0;
        countText.text = "Coins: " + count.ToString();
        NECText.gameObject.SetActive(false);
        WinText.gameObject.SetActive(false);
    }

    void Update()
    {
        Movement();
        Death();
        float horizontalInput = Input.GetAxis("Horizontal");
        if(horizontalInput > 0 && !facingRight)
        {
            Flip();
        }
        else if(horizontalInput < 0 && facingRight)
        {
            Flip();
        }
    }

    void Movement()
    {
        float horizontalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime);

        if(Input.GetKeyDown(KeyCode.Space)) //fix unlimited jump
        {
            GetComponent<Rigidbody2D>().AddForce(jumpHeight, ForceMode2D.Impulse);
        }

        if(transform.position.x < minX)
        {
            transform.position = new Vector3 (minX, transform.position.y, 0);
        }
        else if(transform.position.x > maxX)
        {
            transform.position = new Vector3(maxX, transform.position.y, 0);
        }
    }

    void Death()
    {
        if (transform.position.y <= -4.25f)
        {
            anim.SetBool("isHurt", true);
            Invoke("Respawn", 2f);
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            anim.SetBool("isHurt", true);
            Invoke("Respawn", 2f);
        }
        else if (collision.gameObject.tag == "Coin")
        {
            collision.gameObject.SetActive(false);
            count++;
            countText.text = "Coins: " + count.ToString();
        }
        else if(collision.gameObject.tag == "Chest" && count < 3)
        {
            NECText.gameObject.SetActive(true);
            Destroy(NECText, 5.0f);
        }
        else if(collision.gameObject.tag == "Chest" && count == 3)
        {
            WinText.gameObject.SetActive(true);
            Destroy(WinText, 5.0f);
            anim.SetBool("isWinning", true);
        }
    }

    void Respawn()
    {
        Destroy(player);
        Instantiate(player, new Vector3(-10.0f, -2.32f, 0), Quaternion.identity);
        anim.SetBool("isHurt", false);
    }
}

I know it's probably very messy but it's my first game without a tutorial so it's tough. 我知道这可能很混乱,但这是我的第一本没有教程的游戏,所以很难。

As per the discussions in comments above OP had trouble related to Repsawn() function. 根据上面评论中的讨论,OP与Repsawn()函数有关。

Another way you can go about is changing your Respawn() function. 您可以执行的另一种方法是更改Respawn()函数。 Instead of instantiating the player prefab you can reuse the same prefab and just keep on adjusting its values. 无需实例化播放器预制件,您可以重复使用相同的预制件,并继续调整其值。 Here in this case you wont need to create separate class to store the above mentioned values. 在这种情况下,您无需创建单独的类来存储上述值。 You just say like this 你只是这样说

void Respawn()
{
    anim.SetBool("isHurt", false);
    transform.position = new Vector3(-10.0f, -2.32f, 0);
    transform.rotation = Quaternion.identity;
    GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    GetComponent<Rigidbody2D>().angularVelocity = 0; 
}

and this should solve your problem if you rework your respawn code 如果您重做重生代码,这应该可以解决您的问题

By changing the respawn code to reuse only single gameobject the issue seems to be resolved. 通过更改重生代码以仅重用单个游戏对象,该问题似乎已解决。

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

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