十四.监测点系统

监测点系统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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值