Home > OS >  I'm Having Logic Problems with New Input System
I'm Having Logic Problems with New Input System

Time:05-25

Everything up until this point works, what I want is when left & right button is clicked ability.isCharging turns true. If I get rid of the SecondaryFire.IsPressed() then it starts working, but only with the left mouse button (so I think I narrowed it down to a logic problem, apparently I'm just not logical enough to solve this).

Portion of Code from a MonoBehaviour which contains a Scriptable Object

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) { }
}

CodePudding user response:

As far as I understand, you want, by pressing both mouse clicks, the ability go into the charging phase and activate when releasing the left mouse click. This code may solves your problem.

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);

CodePudding user response:

Problem solved, seems to be a running theme. Else statement nested wrong in logic, nested it correctly now, but its weird that it worked as planned nested in the wrong area until a second requirement was added.

  • Related