p=64 Foundation of Skill System ;类恶魔城Demo学习
创建Skill.cs 以后这个作为所有技能的父类
暂时定义检测是否可用技能和使用技能两个方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Skill : MonoBehaviour
{
[SerializeField] protected float cooldown;
protected float cooldownTimer; //计时器
// Update is called once per frame
void Update()
{
cooldownTimer -= Time.deltaTime;
}
public virtual bool CanUseSkill()
{
if(cooldownTimer < 0)
{
cooldownTimer = cooldown;
return true;
}
return false;
}
public virtual void UseSkill()
{
}
}
新建个DashSkill,
在SkillManager 创个dashSkill对象。然后去unity新建对象放入。
DashSkill:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DashSkill : Skill
{
public override bool CanUseSkill()
{
return base.CanUseSkill();
}
public override void UseSkill()
{
base.UseSkill();
}
}
SkillManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkillManager : MonoBehaviour
{
public static SkillManager instance; //静态方法
public DashSkill dashSkill { get; private set; }
//技能,待续
public void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject); // 销毁重复实例
}
else
{
instance = this; // 设置唯一实例
}
}
public void Start()
{
dashSkill = GetComponent<DashSkill>();
}
}
最后player调整一下,去掉之前在里面设置的冲刺CD相关的两个变量
public void CheckForDashInput()
{
if (IsGroundDetect())
{
return;
}
if (Input.GetKeyDown(KeyCode.Mouse0 ) && SkillManager.instance.dashSkill.CanUseSkill())
{
dashDir = Input.GetAxisRaw("Horizontal");
if(dashDir == 0)
{
dashDir = facingDir;
}
stateMachine.ChangeState(dashState);
}
}
}