【发布时间】:2020-05-14 17:37:54
【问题描述】:
我正在尝试创建一个有两个可玩角色的平台游戏。当用户点击屏幕右侧时,第一个字符跳跃,当点击屏幕左侧时,另一个字符跳跃。这是我到目前为止的代码:
public enum WhichPlayer
{
Player1,
Player2
};
public WhichPlayer whichPlayer;
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && !IsDead)
{
Touch touch = Input.GetTouch(0);
Vector2 position = touch.position;
bool leftHalf = position.x <= Screen.width / 2;
if (whichPlayer == WhichPlayer.Player1 && !leftHalf || whichPlayer == WhichPlayer.Player2 && leftHalf)
{
jump = true;
animator.SetBool("Jump", true);
}
} else
{
jump = false;
animator.SetBool("Jump", false)
}
问题是,如果我在屏幕右侧点击 5 次,然后再点击左侧 1 次,右侧的玩家会跳跃 5 次,然后左侧的玩家会跳跃一次。有什么方法可以确保玩家在任何一侧被点击时立即跳跃,而不是等待所有其他点击完成。我尝试使用 GetMouseButtonDown(0),但在移动设备上无法正常工作。
【问题讨论】: