监测点系统checkpoint
Graphics/Level Mechanics目录下Checkpoint On, Checkpoint Off
1.添加Checkpoint到场景
2.添加Circle Collider 2D碰撞检测,设置为Trigger
3.添加Checkpoint脚本,并绑定到Checkpoint上
添加检测参数
//监测点设置
public SpriteRenderer theSR; //监测点
public Sprite cpOn, cpOff; //监测点on和off标识
添加碰撞检测
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
theSR.sprite = cpOn;
}
}
public void ResetCheckpoint()
{
theSR.sprite = cpOff;
}
4.制作Checkpoint预制体,并添加多个Checkpoint
5.添加CheckpointController
添加空对象CheckpointController,添加脚本CheckpointController,添加单例控制,并绑定到组件上
添加监测点,生成点,并设置
public static CheckpointController sInstance;
private Checkpoint[] checkpoints; //监测点
public Vector3 spawnPoint; //生成点
private void Awake()
{
sInstance = this;
}
void Start()
{
checkpoints = FindObjectsOfType<Checkpoint>();
//设置初始生成点
spawnPoint = PlayerController.sInstance.transform.position;
}
重置全部Checkpoint
public void DeactivateCheckpoints()
{
for(int i=0; i<checkpoints.Length; i++)
{
checkpoints[i].ResetCheckpoint();
}
}
更新生成点
public void SetSpawnPoint(Vector3 newSpawnPoint)
{
spawnPoint = newSpawnPoint;
}
在Checkpoint中碰撞检测时,先重置所有的Checkpoint,更新生成点
//Checkpoint.cs
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
CheckpointController.sInstance.DeactivateCheckpoints();
theSR.sprite = cpOn;
CheckpointController.sInstance.setSpawnPoint(transform.position);
}
}
6.LevelManager
新建脚本LevelManager,新建空对象LevelManager,添加单例控制,并绑定脚本
添加复活时间点
public static LevelManager sInstance;
public float waitToRespawn;
private void Awake()
{
sInstance = this;
}
添加函数RespawnPlayer,开始调用RespawnCo,隔段时间waitToRespawn执行复活,并设置位置
public void RespawnPlayer()
{
StartCoroutine(RespawnCo());
}
//隔段时间调用
private IEnumerator RespawnCo()
{
PlayerController.sInstance.gameObject.SetActive(false);
yield return new WaitForSeconds(waitToRespawn);
PlayerController.sInstance.gameObject.SetActive(true);
PlayerController.sInstance.transform.position = CheckpointController.sInstance.spawnPoint;
//重置生命值
PlayerHealthController.sInstance.currentHealth = PlayerHealthController.sInstance.maxHealth;
UIController.sInstance.UpdateHealthDisplay();
}
在PlayerController中,DealDamage函数下判断currentHealth<=0后面添加
//重置player
LevelManager.sInstance.RespawnPlayer();
thDisplay();
}
在PlayerController中,DealDamage函数下判断currentHealth<=0后面添加
```C#
//重置player
LevelManager.sInstance.RespawnPlayer();