Unity2D-当玩家被击中时如何在摧毁它们后恢复心

Posted

技术标签:

【中文标题】Unity2D-当玩家被击中时如何在摧毁它们后恢复心【英文标题】:Unity2D-How to restore hearts after destroying them when Player gets hit 【发布时间】:2021-11-27 06:26:40 【问题描述】:

我正在使用 Destroy(Corazones[0].gameObject);摧毁一个阵列中的 5 个心,但是当玩家得到另一个心时我需要恢复它们。我已经有了增加生命+1的对撞机并且它可以工作,但我不知道如何重生代表生命的心脏。

当玩家获得 Hit() 时,心会被摧毁;但我也需要用 Vida() 恢复心灵;这增加了现场+1

感谢您的帮助! This is the way the hearts look

我的代码使用西班牙语单词。 PD:我知道我的代码是一团糟,但可以用于其他的东西。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.UI;

public class DixonMovement : MonoBehaviour

    public GameObject PildoraPrefab;
    public float Speed;
    public float JumpForce;
    public AudioClip Golpe;
    public AudioClip Salto;
    public AudioClip Muerte;
    public AudioClip Caida;
    public GameObject[] Corazones;
  



    private Rigidbody2D Rigidbody2D;
    private Animator Animator;
    private float Horizontal;
    private bool Grounded;
    private float LastShoot;
    private int Health = 5;
    public bool YaSono = false;

    [Header("IFrame Stuff")]
    public Color flashColor;
    public Color regularColor;
    public float flashDuration;
    public int numberOfFlashes;
    public Collider2D triggerCollider;
    public SpriteRenderer mySprite;
    float timer;
    bool invencible = false;
    


    private void Start()
    
        Rigidbody2D = GetComponent<Rigidbody2D>();
        Animator = GetComponent<Animator>();
        
    

    // Update is called once per frame
    private void Update()
    
        
        timer -= Time.deltaTime;

        if (timer <= 0)
        
            invencible = false;
        


        Horizontal = CrossPlatformInputManager.GetAxis("Horizontal");

        if (Horizontal < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
        else if (Horizontal > 0.0f) transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);




        Animator.SetBool("Caminar", Horizontal != 0.0f);

        Debug.DrawRay(transform.position, Vector3.down * 0.5f, Color.red);

        if (Physics2D.Raycast(transform.position, Vector3.down, 0.5f))
        
            Grounded = true;

        
        else Grounded = false;




        if (CrossPlatformInputManager.GetButtonDown("Jump") && Grounded)
        
            Jump();
        
        if (Input.GetKeyDown(KeyCode.W) && Grounded)
        
            Jump();
        
        if (Input.GetKey(KeyCode.Space) && Time.time > LastShoot + 0.25f)
        
            Shoot();
            LastShoot = Time.time;
        
        if (CrossPlatformInputManager.GetButtonDown("Shoot") && Time.time > LastShoot + 0.50f)
        
            Shoot();
            LastShoot = Time.time;
        

        /*  if (CrossPlatformInputManager.GetButtonDown("Vertical") && Time.time > LastShoot + 0.50f)
          
              Shoot();
              LastShoot = Time.time;
          */


        if (transform.position.y <= -2)
        
            Fall();

        

        




    


    private void FixedUpdate()
    
        Rigidbody2D.velocity = new Vector2(Horizontal * Speed, Rigidbody2D.velocity.y);
    

    private void Jump()
    
        Rigidbody2D.AddForce(Vector2.up * JumpForce);
        Camera.main.GetComponent<Audiosource>().PlayOneShot(Salto);
    

    private void Shoot()
    
        Vector3 direction;
        if (transform.localScale.x == 1.0f) direction = Vector3.right;
        else direction = Vector3.left;

        GameObject pastilla = Instantiate(PildoraPrefab, transform.position + direction * 0.40f, Quaternion.identity);
        pastilla.GetComponent<Pastilla>().SetDirection(direction);
    



    **public void Hit()
    
        
        Health -= 1;
        if (Health > 0)
        
            StartCoroutine(FlashCo());
        
        if (Health < 1)
        
            Destroy(Corazones[0].gameObject);
            
        
        else if (Health < 2)
        
            Destroy(Corazones[1].gameObject);
           
        
        else if (Health < 3)
        
            Destroy(Corazones[2].gameObject);
            
        
        else if (Health < 4)
        
            Destroy(Corazones[3].gameObject);
            
        
        else if (Health < 5)
        
            Destroy(Corazones[4].gameObject);
            
        
        if (Health > 0)
        
            Camera.main.GetComponent<AudioSource>().PlayOneShot(Golpe);
        **




        if (Health == 0)
        
            Camera.main.GetComponent<AudioSource>().PlayOneShot(Muerte);
            Animator.SetTrigger("Muerte");


            if (Grounded == false)
            
                Rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
                Rigidbody2D.gravityScale = 2;
            
            else
            
                Rigidbody2D.bodyType = RigidbodyType2D.Static;
            



        


    


    public void Fall()
    
        if (transform.position.y <= -1)
        

            Rigidbody2D.AddForce(Vector2.up * 60);

            if (!YaSono)
            
                Camera.main.GetComponent<AudioSource>().PlayOneShot(Caida);
                YaSono = true;
            


            Animator.SetTrigger("Muerte");

            Health = 0;
            Destroy(Rigidbody2D, 2);

            Destroy(Corazones[0].gameObject);
            Destroy(Corazones[1].gameObject);
            Destroy(Corazones[2].gameObject);
            Destroy(Corazones[3].gameObject);
            Destroy(Corazones[4].gameObject);

            Invoke("RestartLevel", 2);



        
    

   public void Vida()
    

        
        if (Health >=5)   else  Health += 1; 
      
    



    public void Bounce()
    
        Rigidbody2D.AddForce(Vector2.up * 300);
    

    void Invencible()
    
        timer = 1;
        if (timer > 0)
        
            invencible = true;
        
    


    private IEnumerator FlashCo()
    
        int temp = 0;
        triggerCollider.enabled = false;
        while (temp < numberOfFlashes)
        
            mySprite.color = flashColor;
            yield return new WaitForSeconds(flashDuration);
            mySprite.color = regularColor;
            yield return new WaitForSeconds(flashDuration);
            temp++;
        
        triggerCollider.enabled = true;
    

    private void RestartLevel()
    
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    

    


   

    



【问题讨论】:

【参考方案1】:

您可以使用Corazones[0].gameObject.setActive(false) 或简单地使用Corazones[0].setActive(false),而不是使用Destroy(Corazones[0].gameObject),因为数组已经包含GameObjects

这将保留GameObject,而它的所有组件和子项都将被停用,包括它的Renderer,这将使其消失。

稍后当您需要它时,您可以使用Corazones[0].setActive(true) 重新激活它。

【讨论】:

以上是关于Unity2D-当玩家被击中时如何在摧毁它们后恢复心的主要内容,如果未能解决你的问题,请参考以下文章

Unity2D 使用协程来跟踪玩家何时转动?

如何检测新对象何时被生成或从统一中删除以获得评分/分数?

让对象的组件在足够远的地方禁用一次

Unity2d中如何删除Checkpoint信息

android笔记--保存和恢复activity的状态数据

Unity2D 敌人追踪/攻击/移动AI 第二期