【问题标题】:Two player controls for UnityUnity 的两个播放器控件
【发布时间】:2019-06-22 13:51:47
【问题描述】:

我正在尝试创建一个有两个可玩角色的平台游戏。当用户点击屏幕右侧时,第一个字符跳跃,当点击屏幕左侧时,另一个字符跳跃。

我能够让第一个玩家跳跃,但无法让第二个玩家跳跃。

if (Input.GetMouseButtonDown(0) && !IsDead)
{            
    jump = true;
}

此代码适用于一个字符,但我无法让第二个字符跳转。

编辑

@derHugo,感谢您的回复。我尝试实现您提供的代码。它适用于播放器 2,但播放器 1 未按预期工作。这是我所拥有的:

public enum WhichPlayer
{
    Player1,
    Player2
};

public WhichPlayer whichPlayer;

void Update () {

 if (Input.GetMouseButtonDown(0) && !IsDead){            
    Vector2 position = Input.mousePosition;
    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);            
    }

播放器 2:当点击屏幕左侧时,播放器 2 跳跃并播放动画。单击屏幕右侧时,玩家 2 停留在地面上,不播放动画。

播放器 1:当点击屏幕右侧时,播放器 1 跳跃并播放动画。单击屏幕左侧时,播放器 1 仍会跳跃,但不播放动画。我无法弄清楚为什么玩家 1 仍然会因为动画停止而跳跃,那是因为“else”块中的代码。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我没有看到用于决定的检查

    当用户点击屏幕右侧时,第一个字符跳跃,点击屏幕左侧时,另一个字符跳跃。

    这需要两件事:

    • “玩家”需要两个知道是Player1还是Player2

      你可以使用例如类似的枚举

      public enum WhichPlayer
      {
          Player1,
          Player2
      }
      

      并在您的播放器脚本中将其添加为字段

      public WhichPlayer whichPlayer;
      

      并在 Inspector 中设置它。

    • 您需要检查您点击的是屏幕的右侧还是左侧

      if (Input.GetMouseButtonDown(0) && !IsDead)
      {
          // get mouse position
          var position = Input.mousePosition;
      
          // get left or right half of screen
          // it is left if the mouseposition x 
          // is smaller then the center of the screen
          var leftHalf = position.x <= Screen.width / 2;
      
          // finally check player type and screen side
          if(whichPlayer == Player1 && leftHalf || whichPlayer == Player2 && !leftHalf)
          {
              jump = true;
          }
      }
      

      (请参阅Screen.widthInput.mousePosition 均以像素为单位)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多