简体   繁体   中英

(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

Hey man hope this helps.

In U. I script add public GameObject PlayerConrtollerScript; In your UI 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.

Hope it works for you and if you struggle with it let me know. I will try help you as much as I can.

That will stop your player from moving around during pause.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM