【问题标题】:two player mobile touch issue in unity统一的两个玩家移动触摸问题
【发布时间】: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),但在移动设备上无法正常工作。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我建议你测试这个解决方案:(重构你的解决方案) 您分析所有触摸并选择每一侧的触摸..

    我想每个玩家都有一个脚本..

    //Left player
    void Update()
    {
        var touched = Input.touches.Any(t => t.position.x <= Screen.width / 2 && t.phase == TouchPhase.Began);
        if (!isDead && touched)
        {
            //ok left player side 
        }
    
    
    //Right player
    void Update()
    {
        var touched = Input.touches.Any(t => !(t.position.x <= Screen.width / 2) && t.phase == TouchPhase.Began);
        if (!isDead && touched)
        {
            //ok right player side 
        }
    

    如果您希望所有玩家都使用相同的脚本,您可以这样做:

    void Update()
    {
    
        if(!isDead)
        {
            var touchedL = Input.touches.Any(t => t.position.x <= Screen.width / 2 && t.phase == TouchPhase.Began);
            var touchedR = Input.touches.Any(t => !(t.position.x <= Screen.width / 2) && t.phase == TouchPhase.Began);
            if (whichPlayer == WhichPlayer.Player2 && touchedL )
           {
              //ok left player side tapped
           }    
    
            if (whichPlayer == WhichPlayer.Player1 && touchedR )
           {
              //ok right player side tapped
           }    
        }
    }
    

    我给你没有 linq 的解决方案:

    var touched = Input.touches.Any(t => t.position.x <= Screen.width / 2 && t.phase == TouchPhase.Began);
    

    可以替换为:

        bool touched = false;
        foreach(var touch in Input.touches)
        {
           if(touch.position.x <= Screen.width / 2 && touch.phase == TouchPhase.Began)
           {
                touched = true;
                break;
           }
        }
        if (!isDead && touched)
        {
            //ok left player side 
        }
    

    其他样品也一样..

    【讨论】:

    • 它肯定是一个更好的方法。 op 的一个注意事项是,出于性能原因,您应在较大的项目中避免使用 LINQ,手动编写选择逻辑通常要快得多
    • 好的,我注意到你的评论了!我在没有 linq 的情况下编写了相同的解决方案。翻译成no linq code之后就没那么复杂了,思路最重要
    • @Frenchy,感谢您的帮助.....现在工作正常......
    猜你喜欢
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2016-08-15
    相关资源
    最近更新 更多