在 unity2D 中使用 C# 更改 android 设备的游戏控制
Changing game controls for android device using C# in unity2D
我有一个为网络构建的突破式游戏,它使用鼠标左右移动球拍,点击球就会发射。我想要实现的是在我的 android 设备上安装此 运行 并具有正确的控件,因为在测试时它根本无法正常工作,因为当我触摸 [=] 上的屏幕时15=] 球发射并且桨控制移动奇怪!这是球拍和球的脚本,如果有人可以帮助或指出我的正确观点,我将非常感激,因为只是找到了我的脚。
public class Paddle : MonoBehaviour {
private Ball ball;
void Start(){
ball = GameObject.FindObjectOfType<Ball> ();
}
void Update () {
MoveWithMouse ();
}
void MoveWithMouse(){
Vector3 paddlePos = new Vector3 (4.7f, this.transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp (mousePosInBlocks, 4.7f, 11.3f);
this.transform.position = paddlePos;
}
}
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
void Start () {
paddle = GameObject.FindObjectOfType<Paddle> ();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if (!hasStarted) {
//lock ball relative to the paddle
this.transform.position = paddle.transform.position + paddleToBallVector;
//wait for mouse press to start
if (Input.GetMouseButtonDown (0)) {
hasStarted = true;
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D collision){
Vector2 tweak = new Vector2 (Random.Range(0f,0.2f),Random.Range(0f,0.2f));
if (hasStarted) {
GetComponent<AudioSource> ().Play ();
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
球发射的原因是 Input.GetMouseButtonDown(0)
在移动设备上触摸屏幕时 returns 也是如此。为了抵消这种情况,您可能需要使用特定于移动设备的控件。例如,如果你想在手指抬起时释放球......你可以这样做:
if (Input.GetTouch(0).phase == TouchPhase.Ended)
为了用手指位置移动球拍,你可以在你的更新方法中加入这样的东西:
if (Input.TouchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.GetTouch(0);
paddlePos = new Vector3(touch.position.x, transform.position.y, transform.position.z);
transform.position = paddlePos;
}
我有一个为网络构建的突破式游戏,它使用鼠标左右移动球拍,点击球就会发射。我想要实现的是在我的 android 设备上安装此 运行 并具有正确的控件,因为在测试时它根本无法正常工作,因为当我触摸 [=] 上的屏幕时15=] 球发射并且桨控制移动奇怪!这是球拍和球的脚本,如果有人可以帮助或指出我的正确观点,我将非常感激,因为只是找到了我的脚。
public class Paddle : MonoBehaviour {
private Ball ball;
void Start(){
ball = GameObject.FindObjectOfType<Ball> ();
}
void Update () {
MoveWithMouse ();
}
void MoveWithMouse(){
Vector3 paddlePos = new Vector3 (4.7f, this.transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp (mousePosInBlocks, 4.7f, 11.3f);
this.transform.position = paddlePos;
}
}
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
void Start () {
paddle = GameObject.FindObjectOfType<Paddle> ();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
if (!hasStarted) {
//lock ball relative to the paddle
this.transform.position = paddle.transform.position + paddleToBallVector;
//wait for mouse press to start
if (Input.GetMouseButtonDown (0)) {
hasStarted = true;
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D collision){
Vector2 tweak = new Vector2 (Random.Range(0f,0.2f),Random.Range(0f,0.2f));
if (hasStarted) {
GetComponent<AudioSource> ().Play ();
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
球发射的原因是 Input.GetMouseButtonDown(0)
在移动设备上触摸屏幕时 returns 也是如此。为了抵消这种情况,您可能需要使用特定于移动设备的控件。例如,如果你想在手指抬起时释放球......你可以这样做:
if (Input.GetTouch(0).phase == TouchPhase.Ended)
为了用手指位置移动球拍,你可以在你的更新方法中加入这样的东西:
if (Input.TouchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.GetTouch(0);
paddlePos = new Vector3(touch.position.x, transform.position.y, transform.position.z);
transform.position = paddlePos;
}