using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
namespace Combat.Test
{
public class ChargeInputTest : MonoBehaviour
{
[Header("动作名称")]
public string start_name;
public string chargeing_name;
public string normalAttack_name;
public string chargeAttck_name;
int Hash_Start;
int Hash_Chargeing;
int Hash_NormalAttack;
int Hash_ChargeAttack;
[Header("蓄力设置")]
public float chargeMaxTime = .5f;//充能最大值达到后释放蓄力攻击,否则转换为普通攻击
public float chargeMinTime = .1f;//充能时间小于最少值则不算蓄力
[SerializeField] float chargeTime;
[SerializeField] bool chargeDone;//蓄力完成
[SerializeField] bool chargeing;//是否在蓄力中
[SerializeField] bool MianAct;
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
Hash_NormalAttack = Animator.StringToHash(normalAttack_name);
Hash_Chargeing = Animator.StringToHash(chargeing_name);
Hash_ChargeAttack = Animator.StringToHash(chargeAttck_name);
Hash_Start = Animator.StringToHash(start_name);
OnChargeStart = ChargeStart;
OnChargeing = Chargeing;
OnChargeDone = ChargeDone;
OnChargeCancelled = ChargeCancelled;
}
public void ChargeStart()
{
anim.CrossFade(Hash_Start, .2f, 1, 0, .1f);
}
public void Chargeing()
{
anim.CrossFade(Hash_Chargeing, .2f, 1, 0, .1f);
}
public void ChargeDone()
{
anim.CrossFade(Hash_ChargeAttack, .2f, 1, 0, .1f);
}
public void ChargeCancelled()
{
anim.CrossFade(Hash_NormalAttack, .2f, 1, 0, .1f);
}
private void Update()
{
MianAct = Input.GetKey(KeyCode.Mouse0) ;
}
private void FixedUpdate()
{
SetCharge(MianAct, Time.fixedDeltaTime);
}
#region 蓄力模块
Action OnChargeStart;//当蓄力开始
Action OnChargeing;//蓄力中
Action OnChargeDone;//蓄力完成
Action OnChargeCancelled;//蓄力取消
bool start_invoked;
bool normal_invoked;
void SetCharge(bool _value, float deltaTime)//这在物理帧执行
{
if (_value)
{
if (chargeDone) return;//蓄力完成 则退出
normal_invoked = false;
chargeTime += deltaTime;
if (!start_invoked)
{
OnChargeStart?.Invoke();
start_invoked = true;
}
if (chargeTime >= chargeMinTime && !chargeing)
{
OnChargeing?.Invoke();
normal_invoked = true;
chargeing = true;//蓄力时间大于最小时间才算作蓄力
}
if (chargeTime >= chargeMaxTime && !chargeDone)
{
OnChargeDone?.Invoke();
chargeTime = 0;
chargeDone = true;
normal_invoked = true;
chargeing = false;
}
}
else
{
if (chargeing) //蓄力中断
{
OnChargeCancelled?.Invoke();
chargeing = false;
normal_invoked = true;
}
if(chargeTime < chargeMinTime && !normal_invoked)//未达到蓄力要去时间 直接取消
{
OnChargeCancelled?.Invoke();
normal_invoked = true;
}
start_invoked = false;
chargeDone = false;
chargeTime = 0;
}
}
#endregion
}
}
面板绑定
animator绑定
?
|