1

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

2 Answers2

0

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);
KiynL
  • 4,097
  • 2
  • 16
  • 34
  • Unfortunately, the initial if statement still doesn't work. The else if works like a charm, but something about having to need both the primary & secondary fire activated makes it stop working. EDIT: Also yes you understood my problem completely, thank you! – Peter Hopkins May 24 '22 at 18:02
  • @PeterHopkins Do you want the ability to go charging when you press two mouse buttons? This code charges the Ability mouse when you press two keys. – KiynL May 24 '22 at 18:05
  • It very much should, but it doesn't. With the code, when I press down both left & right mouse button, nothing happens. And the ability is in the initial if statement because the actual charge happens within the ability scriptable object via a Coroutine. If you want I can edit my question to include the script? – Peter Hopkins May 24 '22 at 18:14
  • @PeterHopkins Please complete your question information. There is nothing else in your question. – KiynL May 24 '22 at 18:16
  • I've added the script it calls, but there's literally nothing else to the question. The if statement should call the ability, but it doesn't, and I don't know why. – Peter Hopkins May 24 '22 at 18:22
  • I took the activate code to first if. Try my Edit, Maybe you mean active when you press the key. – KiynL May 24 '22 at 18:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245006/discussion-between-peter-hopkins-and-kiynl). – Peter Hopkins May 24 '22 at 18:39
0

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.