添加障碍
1.添加spikes,并设置好位置
2.添加Box Collider 2D并调整碰撞体大小
3.设置is Trigger为true
4.新建脚本PlayerHeathController.cs
添加角色生命值参数
//角色生命值
public int currentHealth, maxHealth; //当前生命,最大生命
添加到角色身上
设置角色Tag,并overrides,以改变所有预制体
5.新建脚本DamagePlayer.cs
添加到spikes上
添加碰撞检测函数,并打印信息,检测是否碰撞
//碰撞
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
//角色碰撞到
Debug.Log("Hit the fox!");
}
}
6.PlayerHealthController.cs添加单例
//单例
public static PlayerHealthController sInstance = null;
private void Awake()
{
sInstance = this;
}
7.DamagePlayer.cs中伤害角色
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
//方式一
//FindObjectOfType<PlayerHealthController>().DealDamage();
//方式二 调用单例
PlayerHealthController.sInstance.DealDamage();
}
}