实现僵尸被攻击的死亡效果
前面只是实现了僵尸的移动和豌豆射手的攻击,但是并没有将讲两者联系在一起,本次将实现射手对僵尸的攻击效果
实现大体效果
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>();
}
void Update()
{
if (!isOver&&animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1)
{
animator.speed = 0;
isOver =true;
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();
}
}
|