【发布时间】: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 脚本,或者知道如何编辑这个脚本?
再说一次,我真的很新,如果我看起来像班上的假人,我很抱歉。
感谢您提供的任何帮助。对此,我真的非常感激。这是完成我的游戏的最后一个障碍。
【问题讨论】:
-
嘿,你想用屏幕上的隐形按钮来控制蝙蝠吗(最简单的解决方案)?在这种情况下,您不需要进行任何光线投射