如果按钮被点击,Unity防止跳转

问题描述:

我有一个菜单场景,玩家可以跳转。在真正的比赛开始之前只是为了好玩。游戏首先点击一个UI按钮。但是,如果我点击按钮,播放器将在新场景加载之前跳转。我想阻止这一点。所以我必须在跳转之前检查,如果没有按钮被点击。任何想法如何做到这一点?如果按钮被点击,Unity防止跳转

我试着用一个静态变量。但问题是,调用Jump Function的脚本首先运行。在Buttonscript之前,我可以将Variable设置为false。

void Update() 
{ 
    isGrounded = Physics2D.OverlapCircle(GroundCheck1.position, 0.15f, groundLayer); 

    // Here I want to check if the click doesnt hit a button 
    if (Input.GetMouseButtonDown(0) && isGrounded) 
    { 
     GetComponent<AudioSource>().Play(); 

     // Here I set Jump to true, so my FixedUpdate Method can execute the Jump 
     jump = true; 
    } 
} 

很简单。

检查,如果鼠标在GUI元素:

EventSystem.current.IsPointerOverGameObject()

所以

if (Input.GetMouseButtonDown(0) 
&& isGrounded 
&& !EventSystem.current.IsPointerOverGameObject()) 

此检查,以确保如果他们点击鼠标,他们也没有在GUI元素,那么玩家可以跳跃。另外,当你得到这样的大条件时,把它们分解成辅助方法是一个不错的主意。

+0

谢谢!非常简单 - 但是我刚开始在Unity中开发。你帮了很多忙。我会尽可能快地回答这个问题。 (延迟7分钟) – DannielR