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 –