用第一根手指检测按钮是否按下,如果用更多手指检测按钮,则不会检测到 [英] Detect button press with the first finger but not if pressed with more fingers Unity

查看:155
本文介绍了用第一根手指检测按钮是否按下,如果用更多手指检测按钮,则不会检测到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的平台游戏中,我有一个通过按三个按钮移动的球角色:一个 向右移动" 按钮使其向右移动, "MoveLeft" 将其向左移动,以及 "Jump" > 按钮,该按钮会向球施加垂直力,因此它会跳跃.

In my platform game I have a ball character that is moved by pressing three buttons: a "MoveRight" button to make it move to the right, a "MoveLeft" to move it to the left, and a "Jump" button that will give a vertical force to the ball so it will jump.

这是我用于运动控制的代码-一切正常,:

This is the code i have for the movement control - everything works as it should:

脚本 Move2D :

Script Move2D:

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

public class Move2D : MonoBehaviour
{
    public float moveSpeed = 7f;
    public float jumpSpeed = 7F;
    public bool isGrounded = false;

    public Rigidbody2D rigidbody;

    private Vector2 currentMoveDirection;

    private void Awake()
    {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    public void Jump()
    {
        if (isGrounded)
        {
            rigidbody.AddForce(new Vector3(0f, jumpSpeed), ForceMode2D.Impulse);
        }
    }
    public void JumpHigher()
    {
        rigidbody.AddForce(new Vector3(0f, 12), ForceMode2D.Impulse);
    }

    private void FixedUpdate()
    {
        rigidbody.velocity = currentMoveDirection * moveSpeed + Vector2.up * rigidbody.velocity.y;
    }

    public void TriggerMoveLeft()
    {
        currentMoveDirection += Vector2.left;
    }

    public void StopMoveLeft()
    {
        currentMoveDirection -= Vector2.left;
    }

    public void TriggerMoveRight()
    {
        currentMoveDirection += Vector2.right;
    }

    public void StopMoveRight()
    {
        currentMoveDirection -= Vector2.right;
    }
}

脚本 ContinuesButton :

Script ContinuesButton:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ContinuesButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    [SerializeField] private Button targetButton;

    [SerializeField] private Move2D playerMovement;

    [SerializeField] private bool movesLeft;

    private readonly bool isHover;

    private void Awake()
    {
        if (!targetButton) targetButton = GetComponent<Button>();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (movesLeft)
        {
            playerMovement.TriggerMoveLeft();
        } else
        {
            playerMovement.TriggerMoveRight();
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (movesLeft)
        {
            playerMovement.StopMoveLeft();
        } else
        {
            playerMovement.StopMoveRight();
        }
    }
}

您可以看到,我每次按下按钮都会加力.通过这样做,我意识到玩家可以通过多种方式欺骗" .

As you can see i am adding a force every time a button is pressed. By doing this, i realized that the player could "cheat" in many ways.

平台中存在很多障碍,这些障碍很难克服,因为速度足够快.通过在同一按钮上用两根手指按下,我会向球施加双水平力,然后它会快两倍,并且将水平传递.

In the platform there are quite a lot of obstacles that are very hard to pass, because the speed is just enough. By pressing with two fingers on the same button, I would add double horizontal force to the ball, and it would go twice as fast, and pass the level much more easily.

此外,在某些情况下,玩家有时间跳两次并且跳得更高.

Also, in some cases, the player has time to jump twice and go much higher than how it would normally be.

如何避免这两个问题?

我们非常感谢您提供的帮助或什至是很少的信息!

推荐答案

一种适合当前方法的简单方法是在此脚本中添加一个bool字段,该字段用于跟踪当前是否按住了按钮.类似于private bool buttonHeld;

A simple way that should fit into your current approach would be to add a bool field in this script that tracks if the button is currently being held. Something like private bool buttonHeld;

无论何时调用OnPointerDownbuttonHeld == false,都将其设置为true并调用您的命令,否则什么也不做.对于OnPointerUp,如果是buttonHeld == true,请将其设置为false并停止移动. 为了安全起见,在void OnDisable()中将其设置为false.

Whenever OnPointerDown is called and buttonHeld == false, set it to true and call your commands, otherwise do nothing. And for OnPointerUp, if buttonHeld == true, set it to false and do your movemement stop. And for safety, in void OnDisable(), set it to false.

这篇关于用第一根手指检测按钮是否按下,如果用更多手指检测按钮,则不会检测到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆