我在使用新输入系统时遇到逻辑问题
I'm Having Logic Problems with New Input System
到目前为止一切正常,我想要的是单击左右按钮时 ability.isCharging 变为真。如果我摆脱 SecondaryFire.IsPressed() 然后它开始工作,但只能用鼠标左键(所以我想我把它缩小到一个逻辑问题,显然我只是没有足够的逻辑来解决这个问题) .
包含脚本对象的 MonoBehaviour 的部分代码
if (ability.isCharging == false && (input.Player.PrimaryFire.IsPressed() == true && input.Player.SecondaryFire.IsPressed() == true))
{
ability.isCharging = true;
ability.Activate(gameObject);
}
else if (ability.isCharging == true && input.Player.PrimaryFire.IsPressed() == false)
{
ability.isCharging = false;
}
return;
BaseAlternate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class BaseAlternate : Ability
{
private float chargePower;
public float maxChargePower;
private bool isCoroutineStarted = false;
private NewPlayerController movement;
private CharacterController controller;
public override void Activate(GameObject parent)
{
isCoroutineStarted = false;
movement = parent.GetComponent<NewPlayerController>();
controller = parent.GetComponent<CharacterController>();
if (isCoroutineStarted == false)
{
MonoInstance.instance.StartCoroutine(charging());
}
}
IEnumerator charging()
{
isCoroutineStarted = true;
while (isCharging == true)
{
chargePower += Time.deltaTime;
if(chargePower > maxChargePower)
{
chargePower = maxChargePower;
}
yield return new WaitForEndOfFrame();
}
if (isCharging == false)
Release();
yield return null;
}
private void Release()
{
Debug.Log(chargePower);
chargePower = 0;
isCoroutineStarted = false;
}
}
Ability.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ability : ScriptableObject
{
public new string name;
public float cooldownTime;
public float activeTime;
public bool isChargeAttack;
public bool isCharging;
public bool isAlternateFire;
public virtual void Activate(GameObject parent) { }
public virtual void ChargeAbility(bool buttonIsPressed) { }
public virtual void ReleaseAbility(GameObject parent) { }
}
据我了解,您希望通过同时单击鼠标,使技能进入充电阶段并在释放鼠标左键时激活。此代码可能会解决您的问题。
if (!ability.isCharging && input.Player.PrimaryFire.IsPressed() && input.Player.SecondaryFire.IsPressed())
{
ability.isCharging = true; // charge ability when press both keys
ability.Activate(gameObject);
}
else if (ability.isCharging && input.Player.PrimaryFire.WasReleasedThisFrame())
{
ability.isCharging = false;
}
Debug.Log(ability.isCharging);
问题已解决,似乎是 运行 主题。
Else 语句在逻辑上嵌套错误,现在嵌套正确,但奇怪的是它按计划嵌套在错误的区域,直到添加第二个需求。
到目前为止一切正常,我想要的是单击左右按钮时 ability.isCharging 变为真。如果我摆脱 SecondaryFire.IsPressed() 然后它开始工作,但只能用鼠标左键(所以我想我把它缩小到一个逻辑问题,显然我只是没有足够的逻辑来解决这个问题) .
包含脚本对象的 MonoBehaviour 的部分代码
if (ability.isCharging == false && (input.Player.PrimaryFire.IsPressed() == true && input.Player.SecondaryFire.IsPressed() == true))
{
ability.isCharging = true;
ability.Activate(gameObject);
}
else if (ability.isCharging == true && input.Player.PrimaryFire.IsPressed() == false)
{
ability.isCharging = false;
}
return;
BaseAlternate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class BaseAlternate : Ability
{
private float chargePower;
public float maxChargePower;
private bool isCoroutineStarted = false;
private NewPlayerController movement;
private CharacterController controller;
public override void Activate(GameObject parent)
{
isCoroutineStarted = false;
movement = parent.GetComponent<NewPlayerController>();
controller = parent.GetComponent<CharacterController>();
if (isCoroutineStarted == false)
{
MonoInstance.instance.StartCoroutine(charging());
}
}
IEnumerator charging()
{
isCoroutineStarted = true;
while (isCharging == true)
{
chargePower += Time.deltaTime;
if(chargePower > maxChargePower)
{
chargePower = maxChargePower;
}
yield return new WaitForEndOfFrame();
}
if (isCharging == false)
Release();
yield return null;
}
private void Release()
{
Debug.Log(chargePower);
chargePower = 0;
isCoroutineStarted = false;
}
}
Ability.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ability : ScriptableObject
{
public new string name;
public float cooldownTime;
public float activeTime;
public bool isChargeAttack;
public bool isCharging;
public bool isAlternateFire;
public virtual void Activate(GameObject parent) { }
public virtual void ChargeAbility(bool buttonIsPressed) { }
public virtual void ReleaseAbility(GameObject parent) { }
}
据我了解,您希望通过同时单击鼠标,使技能进入充电阶段并在释放鼠标左键时激活。此代码可能会解决您的问题。
if (!ability.isCharging && input.Player.PrimaryFire.IsPressed() && input.Player.SecondaryFire.IsPressed())
{
ability.isCharging = true; // charge ability when press both keys
ability.Activate(gameObject);
}
else if (ability.isCharging && input.Player.PrimaryFire.WasReleasedThisFrame())
{
ability.isCharging = false;
}
Debug.Log(ability.isCharging);
问题已解决,似乎是 运行 主题。 Else 语句在逻辑上嵌套错误,现在嵌套正确,但奇怪的是它按计划嵌套在错误的区域,直到添加第二个需求。