Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

485
Visualizações
(Unity - C#) Problems with buttons triggering other events

I'm developing a 2D mobile game where the player should tap on the screen to go up. I just added a pause button recently and I've noticed that when I click the button it counts as a tap on the screen, so the player goes up. Basically, in the same exact moment that I press the pause button, the player jumps and then the game freezes while he is in mid-air. Is there a way I could make so the button wouldn't count as a tap?

Here's the part of the player script that makes him move:

            if (Input.GetMouseButtonDown (0) && isDead == false && isKnifeDead == false && UiStartScript.uiInstance.firstClickUi == true) {
            rb.velocity = Vector2.up * velocity;
        }

Heres's the part of the UI script for the pause button:

public void PauseGame()
{
    Time.timeScale = 0;
}

My EventSystem: EventSystem

My Button: Button

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

In U. I script add public GameObject PlayerConrtollerScript; In your U.I Script, under the class Pausgame() Add this code. GameObject.PlayerControllerScript.SetActive = false;

When unpaused you need to add to your script that has the Time. timeScale =1; Add this code. GameObject.PlayerControllerScript.SetActive = true;

After you have added the code mentioned above. Don't forget to add the player game object from your hierarchy into the Inspector with the U. I script, the space will be available to add a game object when you scroll down to your U. I script.

That will stop your player from moving around during pause.

over 4 years ago · Santiago Trujillo Relatório

0

An easy with no code required is to remove the code about detecting tap on screen and instead place a button that covers the whole screen (invisible button).

That button event would trigger the screen movement.

You can add extra buttons on top and Unity will automatically discard the tap from any other button below.

This way you can start adding UI all over without checking in code which should be considered.

over 4 years ago · Santiago Trujillo Relatório

0

This is the approach that I been done. I create a cople of tricks using Event System and Canvas for take advantage of the Mouse Input, you don't need the use of Updates functions for do the basic gameplay.

Here's my example project: TapAndJump on Github

Check the GameObjects called "Click Panel" and "Pause Button" and respective linked methods.

JumpWithTap.cs

using UnityEngine;

public class JumpWithTap : MonoBehaviour
{
    [SerializeField] private UIHandler uiHandler;
    [SerializeField] private float jumpPower = 5f;
    
    private Rigidbody2D rb2D;
    private int counter;

    #region Class Logic
    public void Jump()
    {
        rb2D.velocity = Vector2.up * jumpPower;
        uiHandler.SetNumberToCounter(++counter);
    }
    #endregion

    #region MonoBehaviour API
    private void Awake()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }
    #endregion
}

UIHandler.cs

using UnityEngine;
using UnityEngine.UI;

public class UIHandler : MonoBehaviour
{
    [SerializeField] private Text counterText;
    [SerializeField] private Text pauseText;
    [SerializeField] private Image clickPanel;

    private bool isPaused = false;
    public bool IsPaused
    {
        get { return isPaused; }
        set 
        {
            pauseText.gameObject.SetActive(value);
            isPaused = value; 
        }
    }

    #region Class Logic
    public void SetNumberToCounter(int number) => counterText.text = number.ToString();

    public void DisableClickPanel() => clickPanel.raycastTarget = !clickPanel.raycastTarget;


    public void PauseGame()
    {
        IsPaused = !IsPaused;
        DisableClickPanel();
        // Time.timeScale = IsPaused == true ? 0 : 1; // If you want keep the Physics (ball fall) don't stop the time.
    }
    #endregion
}

I guided by your code example and so on; be free that modify and request any issue.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Assistente virtual