0
我有一个为网络构建的突破式风格的游戏,它使用鼠标左右移动划桨,并在单击球时启动。我试图实现的是有这个在我的Android设备上运行正确的控件,因为在测试时它不能很好地工作,因为当我触摸手机上的屏幕时,球启动和桨控件移动奇怪!这里有桨和球的脚本,如果有人能够帮助或指出我的权利,我将非常感激它,因为只是在团结中找到我的脚。在Unity2D中使用C#更改Android设备的游戏控制
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;
}
}
}
我明白了!谢谢你的帮助ryemoss –