Home > front end >  Unity C# Not taking in certain Input
Unity C# Not taking in certain Input

Time:04-22

I am creating a FPS game, my player has abilities and her ultimate is Jett's ultimate from Valorant (Her weapon switches to 5 knives that she can throw at enemies) I've managed to make the weapon switch to the knives but when I press the Mouse Left-Click nothing happens. I disabled the script that controls the gun so that the player does shoot bullets, have muzzle flash etc but whether its enabled or disabled the knives don't work. I added debug logs and they arent being called, I use the same button to shoot the gun as throwing the knife. I have also tested it without the 'readyToThrow' variable and 'totalThrows' variable.

Update Method:

void Update()
    {
        if (Time.time >= QabilityTimer && Input.GetKeyDown(KeyCode.Q))
        {
            Dash();
            QabilityTimer = Time.time   Qcooldown;
        }

        if (Time.time >= EabilityTimer && Input.GetKeyDown(KeyCode.E))
        {
            SpeedBoost();
            EabilityTimer = Time.time   Ecooldown;
        }

        if (meterButton.currentProgress == meterButton.maxProgress)
        {
            ultReady = true;

           // Debug.Log("ult ready");

            if (Input.GetKeyDown(KeyCode.X))
            {

                //disable the gun script whilst ult is active.
                GameObject gun = GameObject.Find("Guns");
                gun.GetComponent<Guns>().enabled = false;

                weaponSelection.EunhaUltKnife();
                EunhaUltimate();
            }

        }
        if (ultActive)
        {
            ultTimer -= Time.deltaTime;
        }

        if (ultTimer <= 0)
        {
            ResetUltimate();
        }
    }

Ultimate Method:

public void EunhaUltimate()
    {
        ultActive = true;

        if (Input.GetKeyDown(KeyCode.Mouse0) && readyToThrow && totalThrows > 0)
        {
            Debug.Log("working");
            readyToThrow = false;

            GameObject projectile = Instantiate(objectToThrow, attackPoint.position, cam.rotation);
            Rigidbody projectileRB = projectile.GetComponent<Rigidbody>();

            Vector3 forceToAdd = cam.transform.forward * throwForce   transform.up * throwUpwardForce;
            projectileRB.AddForce(forceToAdd, ForceMode.Impulse);

            totalThrows--;

            Invoke(nameof(ReserThrow), throwCooldown);
        }

    }

CodePudding user response:

Try using Input.GetKey(KeyCode.Mouse0) or Input.GetKeyUp(KeyCode.Mouse0) instead Input.GetKeyDown(KeyCode.Mouse0).

CodePudding user response:

Looks like you can only get into the EunhaUltimate() if you're holding down X:

    if (meterButton.currentProgress == meterButton.maxProgress)
    {
        ultReady = true;

       // Debug.Log("ult ready");

        if (Input.GetKeyDown(KeyCode.X))
        {

            //disable the gun script whilst ult is active.
            GameObject gun = GameObject.Find("Guns");
            gun.GetComponent<Guns>().enabled = false;

            weaponSelection.EunhaUltKnife();
            EunhaUltimate();
        }
    }

Try moving the call to EunhaUltimate() outside of that IF statement. You'll need to have a trigger to show when you've pushed X to trigger the ultimate, but it looks like you're currently doing that inside EunhaUltimate(), with the ultActive = true; line.

Other than setting ultActive = true;, all your EunhaUltimate() seems to do is to check the keyboard press, so I'd recommend rewriting your first snippet as follows:

void Update()
{
    if (Time.time >= QabilityTimer && Input.GetKeyDown(KeyCode.Q))
    {
        Dash();
        QabilityTimer = Time.time   Qcooldown;
    }

    if (Time.time >= EabilityTimer && Input.GetKeyDown(KeyCode.E))
    {
        SpeedBoost();
        EabilityTimer = Time.time   Ecooldown;
    }

    if (meterButton.currentProgress == meterButton.maxProgress)
    {
        ultReady = true;

       // Debug.Log("ult ready");

        if (Input.GetKeyDown(KeyCode.X))
        {

            //disable the gun script whilst ult is active.
            GameObject gun = GameObject.Find("Guns");
            gun.GetComponent<Guns>().enabled = false;

            weaponSelection.EunhaUltKnife();
            ultActive = true; // <--- This is a change
        }
        
    }
    if (ultActive)
    {
        EunhaUltimate(); // <-- this is a change
        ultTimer -= Time.deltaTime;
    }

    if (ultTimer <= 0)
    {
        ResetUltimate();
    }
}
  • Related