Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Input.GetButtonDown("Fire1") Fires immediately after I reset scene on phones.

Discussion in 'Scripting' started by MCX_LEAGUE, Jul 13, 2023.

  1. MCX_LEAGUE

    MCX_LEAGUE

    Joined:
    Jul 12, 2023
    Posts:
    1
    Im using Input.GetButtonDown("Fire1") to see when a user clicks, Im making a stacking blocks game, when the user misses the scene is reset, but on phones instead of going to the start scene it instantly starts the game.
    Its almost like its taking the click from before the scene reset.

    My start game code:


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Threading;

    public class GameManager : MonoBehaviour
    {
    public static event Action OnCubeSpawned = delegate { };
    private float moveSpeed = 0.1f;

    private Vector3 targetPosition;

    private CubeSpawner[] spawners;
    private int spawnerIndex;
    private CubeSpawner currentSpawner;
    private void Awake()
    {
    spawners = FindObjectsOfType<CubeSpawner>();
    }

    private void Update()
    {
    if (Input.GetButtonDown("Fire1")) -- This starts the game
    {
    if (MovingCube.CurrentCube != null)
    MovingCube.CurrentCube.Stop();

    spawnerIndex = spawnerIndex == 0 ? 1 : 0;
    currentSpawner = spawners[spawnerIndex];

    currentSpawner.SpawnCube();
    OnCubeSpawned();
    moveSpeed = 0.1f;
    targetPosition = Camera.main.transform.position + new Vector3(0f, moveSpeed, 0f);
    StartCoroutine(MoveCamera());
    }
    }

    private System.Collections.IEnumerator MoveCamera()
    {
    while (Camera.main.transform.position != targetPosition)
    {
    // Move the camera towards the target position using MoveTowards
    Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, targetPosition, moveSpeed * Time.deltaTime);

    yield return null;
    }
    }

    }

     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    While i have very little experience with mobile development, i imagine it actually is their last input. We do have some functions to reset parts of the input system, such as ResetInputAxes which actually serve the purpose of getting rid of lingering inputs. The documentation names respawning as a potential usecase. Im not sure, it may apply to swapping scenes too in some cases. There would probably be something similar for touch based inputs then.

    Sorry if this wasnt too useful.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I like to handle this the oldskool way and debounce each post-gameplay screen.

    I just have my own
    float age;
    timer that I count up with
    Time.deltaTime
    ... If it isn't at least 0.8 seconds, touches are ignored, or buttons are non-interactable.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Just curious, but any reason you specifically came up with that number?
     
    Last edited: Jul 14, 2023
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    That is a fascinating question... thinking back to it (and I might totally just be retro-imagining this connection entirely), I believe it was from he MS-DOS days. Here's how:

    - we did everything by frames, 18.2 frames per second (the easiest most reliable simple system hardware timer to use)

    - I chose 15 frames to wait for debounce

    That comes out to 0.82 and I just rounded to 0.8

    But again, that's just a guess... been using that for so long now...

    Interestingly all my old 18.2fps games are now being run at 30fps in my KurtMaster2D game... mostly. Some have been slowed because they were just too hard. Some of my original 8085 TRS-80 games only ran at 10-15fps so those are generally also replayed at lower fps, but you can click to crank it up to 30 if you want!

    PS - wasn't no Time.deltaTime going on back then either! :)
     
    Yoreki likes this.