赞
踩
实现敌人掉血时出现闪烁,增强打击感以及判断是否对敌人造成伤害
首先点开Enemywenzi,在它的SpriteRenderer的组件中找到color,先调到想要的颜色,然后记录它的RGB和透明度的数值,
然后再Enemy脚本上写上闪烁的方法,并放在TakeDamage中一起触发
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Enemy : MonoBehaviour
{
public int health;
public int damage;
public float changeTime;
private SpriteRenderer sr;
private Color originColor;
public void Start()
{
sr = GetComponent<SpriteRenderer>();
originColor = sr.color;
}
public void Update()
{
if(health <= 0)
{
Destroy(gameObject);
}
}
public void TakeDamage(int damage)
{
health -= damage;
FlashColor(changeTime);
}
void FlashColor(float time)
{
//分别对应着R,G,B,透明度
sr.color = new Color(255, 255, 0, 255);
Invoke("ResetColor", time);
}
void ResetColor()
{
sr.color = originColor;
}
}
更改颜色 sr.color = new Color(255, 255, 0, 255);
延迟调用某个方法: Invoke("ResetColor", time);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。