角色生命等属性控制
1.添加UI界面
鼠标右键---->UI---->Canvas,重命名为UICanvas
EventSystem放置在UICanvas下
在UICanvas下添加Image,命名为Heart1,设置图片为UI_Heart
拷贝2个Heart1,共3个心形代表角色生命值
设置UI对齐方式为左上角
2.创建脚本UIController
绑定到UICanvas
创建单例instance,在在Awake中指向UIController
//单例
public static UIController sInstance;
private void Awake()
{
sInstance = this;
}
添加引用UI
using UnityEngine.UI;
创建3个Image,分别指向Heart1,Heart2,Heart3
创建3个Sprite,表示满血、半血和空血,指向三个sprite
public Image heart1, heart2, heart3; //三个生命值状态标识
public Sprite heartFull, heartHalf, heartEmpty; //生命值状态
新建函数UpdateHealthDisplay,更新生命值状态
public void UpdateHealthDisplay()
{
switch(PlayerHealthController.sInstance.currentHealth)
{
case 6:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartFull;
break;
case 5:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartHalf;
break;
case 4:
heart1.sprite = heartFull;
heart2.sprite = heartFull;
heart3.sprite = heartEmpty;
break;
case 3:
heart1.sprite = heartFull;
heart2.sprite = heartHalf;
heart3.sprite = heartEmpty;
break;
case 2:
heart1.sprite = heartFull;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
break;
case 1:
heart1.sprite = heartHalf;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
break;
case 0:
heart1.sprite = heartEmpty;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
break;
default:
heart1.sprite = heartEmpty;
heart2.sprite = heartEmpty;
heart3.sprite = heartEmpty;
break;
}
}
在PlayerHealthController中,player绑定中maxHealth改为6,overrides---->Apply All
DealDamage函数内同步生命值状态显示
public void DealDamage()
{
currentHealth --;
if(currentHealth <= 0)
{
currentHealth = 0;
//生命值耗尽,设置角色隐藏
gameObject.SetActive(false);
}
//同步生命值状态显示
UIController.sInstance.UpdateHealthDisplay();
}
3.设置受到伤害后一段时间无敌
添加无敌状态的一些参数
//防止连续受到伤害,设置受到伤害后,一段时间无敌状态
public float invincibleLength; //无敌时间
private float invincibleCounter; //无敌计数器
//设置无敌状态时角色的显示变化
private SpriteRenderer theSR;
//Start()中初始化
theSR = GetComponent<SpriteRenderer>();
设置invincibleLength为1,在Update中更新invincibleCounter
void Update()
{
if (invincibleCounter > 0)
{
invincibleCounter -= Time.deltaTime;
if(invincibleCounter <= 0)
{
theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, 1.0f);
}
}
}
被攻击时,也同步判断和设置
public void DealDamage()
{
if (invincibleCounter <= 0)
{
currentHealth--;
if (currentHealth <= 0)
{
currentHealth = 0;
//生命值耗尽,设置角色隐藏
gameObject.SetActive(false);
}
else
{
invincibleCounter = invincibleLength;
theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, .5f);
}
//同步生命值状态显示
UIController.sInstance.UpdateHealthDisplay();
}
}
4.受伤后状态
在PlayerController中添加参数
//受伤状态
public float knockBackLength; //推动时间长
public float knockBackForce; //推动力量
private float knockBackCounter; //推动计数器
设置初始值
在Update中添加判断
void Update()
{
if (knockBackCounter <= 0)
{
//设置速度 /*GetAxisRaw*/
theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);
//检测是否在地面 参数:圆形中心,圆形半径,筛选器(用于检查仅在指定层上的对象)
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatIsGround);
if (isGrounded)
{
//在地面时,可以连跳为true
canDoubleJump = true;
}
//跳跃
if (Input.GetButtonDown("Jump"))
{
if (isGrounded) //判断是否在地面
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
else
{
if (canDoubleJump)
{
//不在地面,连跳
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
canDoubleJump = false;
}
}
}
//翻转
if (theRB.velocity.x < 0)
{
theSR.flipX = true;
}
else if (theRB.velocity.x > 0)
{
theSR.flipX = false;
}
}
else
{
knockBackCounter -= Time.deltaTime;
if(!theSR.flipX)
{
theRB.velocity = new Vector2(-knockBackForce, theRB.velocity.y);
}
else
{
theRB.velocity = new Vector2(knockBackForce, theRB.velocity.y);
}
}
//设置动画中设置的moveSpeed,isGrounded的值
anim.SetFloat("moveSpeed", Mathf.Abs(theRB.velocity.x));
anim.SetBool("isGrounded", isGrounded);
}
添加KnockBack()函数,设置受伤后推动值
public void KnockBack()
{
knockBackCounter = knockBackLength;
theRB.velocity = new Vector2(0, knockBackForce);
}
在PlayerHealthController中,DealDamage()函数判断当前生命值大于0时,添加推动状态(else下)
//角色受伤后推动状态
PlayerController.sInstance.KnockBack();
5.受伤动画
添加受伤动画
交叉设置动画帧
新建Trigger,命名为"hurt"
设置连接动画
其他状态到受伤状态
受伤状态到正常状态
PlayerController中,KnockBack()函数里面,添加受伤触发动画
//受伤动画
anim.SetTrigger("hurt");