【问题标题】:How to convert keyboard controls to touch screen in Unity如何在 Unity 中将键盘控件转换为触摸屏
【发布时间】:2019-02-14 09:34:26
【问题描述】:

在这里完成新手。使用 Unity C#。我正在考虑将我的 PONG 游戏从键盘控制转移到触摸屏。这是我的工作键盘代码:

// 玩家 1 => 用 W/S 键控制左击球

public GameObject leftBat;

// 用于初始化 无效开始(){

}

// 每帧调用一次更新 无效更新(){

//Defualt speed of the bat to zero on every frame
leftBat.GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);

//If the player is pressing the W key...
if (Input.GetKey (KeyCode.W)) {

    //Set the velocity to go up 1
    leftBat.GetComponent<Rigidbody>().velocity = new Vector3(0f, 8f, 0f);
}

//If the player is pressing the S key...
else if (Input.GetKey (KeyCode.S)) {

    //Set the velocity to go down 1 (up -1)
    leftBat.GetComponent<Rigidbody>().velocity = new Vector3(0f, -8f, 0f);


}

}

我找到了这段代码并一直在使用它,但现在可以使用了。

使用 UnityEngine; 使用 System.Collections;

公共类 Player_Input_Controller : MonoBehaviour {

public GameObject leftBat;
public float paddleSpeed = 1f;
public float yU;
public float yD;

private Ray ray;
private RaycastHit rayCastHit;

private Vector3 playerPos = new Vector3(0, -9.5f, 0);

// Update is called once per frame
void Update () {

    if (Input.GetMouseButton (0))
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if(Physics.Raycast(ray, out rayCastHit)){
            Vector3 position = rayCastHit.point;

            float yPos = position.y;
            playerPos = new Vector3(Mathf.Clamp(yPos, yU, yD), -9.5f, 0f);
            transform.position = playerPos; 
        }
    }
}

}

谁有我可以使用的触摸屏 Pong 脚本,或者知道如何编辑这个脚本?

再说一次,我真的很新,如果我看起来像班上的假人,我很抱歉。

感谢您提供的任何帮助。对此,我真的非常感激。这是完成我的游戏的最后一个障碍。

【问题讨论】:

  • 嘿,你想用屏幕上的隐形按钮来控制蝙蝠吗(最简单的解决方案)?在这种情况下,您不需要进行任何光线投射

标签: unity3d touch 2d pong


【解决方案1】:

在我看来,您不需要任何光线投射。因为您在 2D 中工作并且您只关心 Input.mousePosition 的 y 值。因此,您可以使用相机的 z 值计算屏幕的范围。如果您的相机位于(0, 0, -10),假设您在游戏中的范围将在您的世界坐标中为-5 - BatOffset5+BatOffset。因此,您需要一个函数来将Screen.height 映射到您的世界坐标范围,正如您从图像中看到的那样。

总之你需要找到Input.mousePosition.y 将其划分为Screen.height。这将为您提供触摸或单击的比率。然后在世界空间中找到位置。

请注意:您也可以使用Input.touchPosition.y。以下脚本将为您执行此操作:

public GameObject cam;
private Vector3 batPos;
private float minY;
private float maxY;
private int Res;
private float deltaY;

void Start () {
    minY = cam.transform.position.z / 2 - gameObject.transform.localScale.y;
    maxY = (-cam.transform.position.z / 2) + gameObject.transform.localScale.y;
    deltaY = maxY - minY;
    Debug.Log(minY + " " + maxY + " " + deltaY);
    Res = Screen.height;

    batPos = gameObject.transform.position;

}

void Update () {
    if(Input.GetMouseButtonDown(0))
    {
        // we find the height we have to go up from minY
        batPos.y =minY + Input.mousePosition.y / Res * deltaY;
        gameObject.transform.position = batPos;
    }       
}

当您用鼠标单击屏幕时,这对我在编辑器中有效。您只需将部分Input.GetMouseButtonDown 更改为Touch 命令,例如Touch.tapCount &gt; 0。这个脚本也应该附加到蝙蝠上。祝你好运!

另外您可以使用正交相机和正交相机尺寸来代替cam.transformation.z

【讨论】:

  • 啊!完美的人。触感很好。它保持在界限内。是否可以让蝙蝠响应拖动功能而不是跳转到点击点/触摸点。
  • 是的,您可以使用Vector3.Lerp。它会将球棒平稳地移动到点击的位置。
  • 另外我现在意识到你可以删除 if(Input.GetMouseButtonDown(0)) 部分,它会拖动蝙蝠。
  • 去掉 if(Input.GetMouseButtonDown(0)) 并用 Vector3.Lerp 替换它,还是只去掉 if 语句?
  • 没有替换只需删除不需要lerp的if部分
【解决方案2】:

可能会有所帮助

  1. 创建脚本。例如 player.cs

    public class PlayerController : MonoBehaviour
     {
          bool  swipeRight = false;
          bool  swipeLeft = false;
          bool touchBlock = true;
          bool canTouchRight = true;
          bool canTouchLeft = true;
    
         void Update()
         {
               swipeLeft = Input.GetKeyDown("a") || Input.GetKeyDown(KeyCode.LeftArrow);
               swipeRight = Input.GetKeyDown("d") || Input.GetKeyDown(KeyCode.RightArrow);
               TouchControl();    
              if(swipeRight)  //rightMove logic
              else if(swipeLeft) //leftMove logic
        }
    
       void TouchControl()
       {
             if(Input.touchCount  == 1 && Input.GetTouch(0).phase == TouchPhase.Began )
            {
                 //Jump logic 
             }
            else  if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && touchBlock == true) 
           {
                  touchBlock = false;
                 // Get movement of the finger since last frame
                 var touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                 Debug.Log("touchDeltaPosition "+touchDeltaPosition);
    
                 if(touchDeltaPosition.x > 0 && canTouchRight == true)
                {
                     //rightMove
                     swipeRight = true; canTouchRight = false;
                      Invoke("DisableSwipeRight",0.2f);
                  }
                  else  if(touchDeltaPosition.x < 0 && canTouchLeft == true)
                 {
                      //leftMove
                      swipeLeft = true; canTouchLeft = false;
                      Invoke("DisableSwipeLeft",0.2f);
                   }       
             } 
     }
    void DisableSwipeLeft()
     {
         swipeLeft = false;
         touchBlock = true;
         canTouchLeft = true;
     }
     void DisableSwipeRight()
     {
         swipeRight = false;
         touchBlock = true;
         canTouchRight = true;
     }
     }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多