Issue with method

Hello all,

I’m having some issues getting my method for double tapping either horizontal direction buttons to increase the speed of the character. It worked before when I just had it under Update but after moving it to it’s own method it stopped working. I’m not sure why. Relevant code snippets below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class CharacterController2D : MonoBehaviour
{
    private float horizontalInput = 0f;
    private float Initialspeed = 5.0f;
   [SerializeField] private float runMultiplier = 1.5f; // Multiplier for run speed

    private bool isRunning = false;
    private float lastTapTime = 0f;
    private const float doubleTapTimeThreshold = 0.3f;

    void FixedUpdate()
    {
        DoubleTapToRun();

        // Calculate current movement speed
        float currentSpeed = Initialspeed * (isRunning ? runMultiplier : 1.0f);

        // Apply horizontal movement
        rb.velocity = new Vector2(horizontalInput * currentSpeed, rb.velocity.y);

    }

    private void DoubleTapToRun()
    {
        if (Input.GetButtonDown("Horizontal"))
        {
            if (Time.time - lastTapTime < doubleTapTimeThreshold)
            {
                isRunning = true;
                Debug.Log("Run activated! 1.5 times");
            }
            else
            {
                isRunning = false; // Deactivate run when no double tap
            }
            lastTapTime = Time.time;
        }
        else
        {
            isRunning = false; // Deactivate run when direction not held
        }
    }
}

I’ve tried moving the DoubleTapToRun() reference into Update to see if that would help with no change. Not sure what else to try. I’ve been staring at this for like an hour and I think my brain is starting to get fried lol

The rest of the code isn’t relevant in my opinion but if someone wants to see it then I’d be happy to share the full code. It’s only 139 lines so it’s not that long right now.

The issue is not your separate method but that you call it from FixedUpdate. Never do that. FixedUpdate is only for continous physics force stuff. One-time events like button down or button up events are only true for one frame. However FixedUpdate doesn’t run every frame so you can easily miss clicks or button presses. You can only check for such events inside Update.

Though apart from that you have another issue. Inside your else statement at the end you set your isRunning bool to false. So any frame where you didn’t just pressed down your button would set it to false. Keep in mind that GetButtonDown is only true for one frame. That means the next frame the variable would be set to false again.

The exact desired functionality is not clear. However the comment at your else statement suggests that you want to be running until you release the button again after the second tap? If that’s the case you have to check for the button state separately.

    void Update()
    {
        DoubleTapToRun();
        // Calculate current movement speed
        float currentSpeed = Initialspeed * (isRunning ? runMultiplier : 1.0f);
        // Apply horizontal movement
        rb.velocity = new Vector2(horizontalInput * currentSpeed, rb.velocity.y);
    }
    private void DoubleTapToRun()
    {
        if (Input.GetButtonDown("Horizontal"))
        {
            if (Time.time - lastTapTime < doubleTapTimeThreshold)
            {
                isRunning = true;
                Debug.Log("Run activated! 1.5 times");
            }
            lastTapTime = Time.time;
        }
        if (!Input.GetButton("Horizontal"))
        {
            isRunning = false; // Deactivate run when direction not held
        }
    }

Gotcha, that makes sense. I appreciate the changes posted. It works without fail now. Thank you!