项目实训2022-3-20

实现僵尸被攻击的死亡效果

前面只是实现了僵尸的移动和豌豆射手的攻击,但是并没有将讲两者联系在一起,本次将实现射手对僵尸的攻击效果

实现大体效果

Zombie类的修改

    private int hp = 270;


    public int Hp { get => hp;
        set
        {
            hp = value;
            if (hp <= 0)
            {
                State = ZombieState.Dead;
            }
        }
    }


public void Hurt(int attackValue)
    {
        Hp -= attackValue;
    }


    private void Dead()
    {
        ZombieManager.Instance.RemoveZombie(this);
        Destroy(gameObject);
    }
}

Bullet类修改


    private int attackValue;


    public void Init( int attackValue)
    {
        rigidbody = GetComponent<Rigidbody2D>();
        rigidbody.AddForce(Vector2.right * 300);
        spriteRenderer = GetComponent<SpriteRenderer>();
        this.attackValue = attackValue;

    }
    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (isHit) return;
        if (coll.tag == "Zombie")
        {
            isHit = true;

            coll.GetComponentInParent<Zombie>().Hurt(attackValue);
            Debug.Log(attackValue);
            //修改击中图片
            spriteRenderer.sprite = GameManager.Instance.GameConf.BulletHit;
            //暂停运动
            rigidbody.velocity = Vector2.zero;
            //下落
            rigidbody.gravityScale = 1;

            Invoke("Destroy", 0.8f);

        }
    }


Peashooter修改:

    private int attackValue = 20;

优化死亡效果

僵尸死亡是一个过程,并不是直接消失。僵尸死亡是先

导入头的素材
在这里插入图片描述
设置预制体和动画


    [Header("僵尸")]
    [Tooltip("僵尸头")]
    public GameObject Zombie_Head;

编写失去头的脚本

 public int Hp
    {
        get => hp;
        set
        {
            hp = value;
            if (hp <= 90 && !isLostHead)
            {
                isLostHead = true;
                walkAnimationStr = "Zombie_LostHead";
                attackAnimationStr = "Zombie_LostHeadAttack";

                //创建头

                GameObject.Instantiate<GameObject>(GameManager.Instance.GameConf.Zombie_Head, animator.transform.position, Quaternion.identity, null);
                CheckState();

            }

            if (hp <= 0)
            {
                State = ZombieState.Dead;
            }
        }
    }

编写头的脚本

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

public class Zombie_Head : MonoBehaviour
{

    private Animator animator;
    private bool isOver = false;
    public void Start()
    {
        animator = GetComponent<Animator>();
    }
    // Start is called before the first frame update


    // Update is called once per frame
    void Update()
    {
        if (!isOver&&animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1)
        {
            //播放结束
            animator.speed = 0;
            isOver =true;
            //2s之后销毁
            Invoke("Destroy",2);
        }
        
    }

    private void Destroy()
    {
        Destroy(gameObject);
        
    }
}

实现僵尸受击的反馈效果



    void Start()
    {
        GetGirdByVerticalNum(Random.Range(0,2));
        spriteRenderer = GetComponentInChildren<SpriteRenderer>();
        animator = GetComponentInChildren<Animator>();
        ZombieManager.Instance.AddZombie(this);

   

    }





    public void Hurt(int attackValue)
    {
        Hp -= attackValue;
        StartCoroutine(ColorEF(0.2f, new Color(0.4f, 0.4f, 0.4f), 0.05f, null));
    }



    protected IEnumerator ColorEF(float wantTime, Color targetColor, float delayTime, UnityAction fun)
    {
        float currTime = 0;
        float lerp;
        while (currTime < wantTime)
        {
            yield return new WaitForSeconds(delayTime);
            lerp = currTime / wantTime;
            currTime += delayTime;
            spriteRenderer.color = Color.Lerp(Color.white, targetColor, lerp);
        }
        spriteRenderer.color = Color.white;
        if (fun != null) fun();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值