Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用第一个手指检测按钮按下,但如果用更多手指按下则不检测_C#_Unity3d - Fatal编程技术网

C# 用第一个手指检测按钮按下,但如果用更多手指按下则不检测

C# 用第一个手指检测按钮按下,但如果用更多手指按下则不检测,c#,unity3d,C#,Unity3d,在我的平台游戏中我有一个球角色,它通过按下三个按钮来移动:一个的“MoveRight”按钮使它向右移动,一个的“MoveLeft”按钮将球垂直移动,使它跳跃 这是我的运动控制代码-一切正常运行: 脚本移动2D: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Move2D : MonoBehaviour { public float moveSpeed

在我的平台游戏中我有一个球角色,它通过按下三个按钮来移动:一个的“MoveRight”按钮使它向右移动,一个的“MoveLeft”按钮将球垂直移动,使它跳跃

这是我的运动控制代码-一切正常运行

脚本移动2D:

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;
    }
}
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();
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类:单一行为
{
公共浮动速度=7f;
公共浮子跳跃速度=7F;
公共bool isGrounded=false;
公共刚体2D刚体;
专用矢量2当前移动方向;
私人空间
{
刚体=GetComponent();
}
公共跳转
{
如果(接地)
{
刚体.AddForce(新矢量3(0f,跳跃速度),ForceMode2D.pulse);
}
}
公众假期(星期日)
{
刚体.AddForce(新矢量3(0f,12),ForceMode2D.pulse);
}
私有void FixedUpdate()
{
rigidbody.velocity=currentMoveDirection*moveSpeed+Vector2.up*rigidbody.velocity.y;
}
公共无效TriggerMoveLeft()
{
currentMoveDirection+=向量2.left;
}
公共void StopMoveLeft()
{
currentMoveDirection-=Vector2.left;
}
公共权力
{
currentMoveDirection+=Vector2.right;
}
公共无效StopMoveRight()
{
currentMoveDirection-=Vector2.right;
}
}
脚本继续按钮:

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;
    }
}
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();
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Events;
使用UnityEngine.EventSystems;
使用UnityEngine.UI;
公共类ContinuesButton:MonoBehavior、IPInterDownHandler、IPInterUpHandler
{
[SerializeField]专用按钮targetButton;
[SerializeField]私人移动2D播放器移动;
[Serialized Field]私人布尔·莫夫斯里夫特;
私人只读文件;
私人空间
{
如果(!targetButton)targetButton=GetComponent();
}
POINTERDOWN上的公共无效(PointerEventData事件数据)
{
if(movesLeft)
{
playerMovement.TriggerMoveLeft();
}否则
{
playerMovement.triggerOverlight();
}
}
POINTERUP上的公共无效(PointerEventData事件数据)
{
if(movesLeft)
{
playerMovement.StopMoveLeft();
}否则
{
playerMovement.StopMoveRight();
}
}
}
正如你所看到的,每次按下按钮时,我都会增加一个力。通过这样做,我意识到玩家可以在很多方面“欺骗”

站台上有很多很难通过的障碍物,因为速度刚好够。通过用两个手指按同一个按钮,我将向球添加两倍的水平力,球的速度将是的两倍,并且更容易通过关卡

此外,在某些情况下,玩家有时间跳两次,跳得比平时高得多

如何避免这两个问题


非常感谢您提供的任何帮助或任何小信息

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

无论何时调用
OnPointerDown
,并且
buttonHeld==false
,都将其设置为true并调用您的命令,否则不执行任何操作。对于
OnPointerUp
,如果
buttonHeld==true
,则将其设置为false并停止移动。
为了安全起见,在
void OnDisable()
中,将其设置为false。

谢谢。我会试试的!