总章中间漏了点,最近懒得补。干脆做到哪写到哪吧,有时间再整理。
实现空中多段跳
新建一个空中跳跃状态(PlayerState_AirJump.cs),为他添加资产菜单(CreateAssetMenu);
空中跳跃的逻辑和普通跳跃一样,所以直接复制代码即可。重要的地方在于,我们需要给二段跳进行限制。
在PlayerController.cs中,新建属性CanAirJump,public bool CanAirJump { get; set; } = true;//控制空中跳跃能力的开关
在下落状态脚本PlayerState_Fall.cs中的**LogicUpdate()**函数里实现判断,当玩家可以跳跃时(吃到星星,测试时条件是落地后)切换状态:
public override void LogicUpdate() { if (player.IsGrounded) { stateMachine.SwitchState(typeof(PlayerState_Land)); } if (input.playerjump) { if (player.CanAirJump) { stateMachine.SwitchState(typeof(PlayerState_AirJump)); } } }
|
给跳跃添加粒子特效
分别在两个跳跃状态新建[SerializeField] ParticleSystem jumpVFX;//粒子特效
实现吃星星增加跳跃次数
首先
将玩家控制器脚本PlayerController中,属性 public bool CanAirJump { get; set; }
的ture赋值删除,不进行初始化.默认值为false;
其次
在PlayerState_Land脚本中删除Enter()里,player.CanAirJump
。
打开星星宝石预制体,创建脚本StarGem,内容如下:
public class StarGem : MonoBehaviour { private void OnTriggerEnter(Collider other) { if (other.TryGetComponent<PlayerController>(out PlayerController player)) { player.CanAirJump = true; } } }
|
碰撞体配置,记得打开触发器
接下来实现星星宝石消失后延时恢复
public class StarGem : MonoBehaviour { [SerializeField] float resetTime = 2; MeshRenderer meshRenderer; new Collider collider;
WaitForSeconds waitResetTime; private void Awake() { meshRenderer = GetComponentInChildren<MeshRenderer>(); collider = GetComponent<Collider>(); waitResetTime = new WaitForSeconds(resetTime); } private void OnTriggerEnter(Collider other) { if (other.TryGetComponent<PlayerController>(out PlayerController player)) { player.CanAirJump = true; meshRenderer.enabled = false; collider.enabled = false; StartCoroutine(ResetCoroutine()); } } IEnumerator ResetCoroutine() { yield return waitResetTime; Reset(); } void Reset() { meshRenderer.enabled = true; collider.enabled = true; } }
|
给星星添加音效
组件中添加Audio Source,在代码中添加拾取音效.图中已圈出
旋转星星
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AutoRotate : MonoBehaviour { [SerializeField] Vector3 rotation; private void Update() { transform.Rotate(rotation * Time.deltaTime); } }
|