Changing game controls for android
This is my first ever post here so hope i get this question right! i have a breakout style game built for web that uses mouse to move the paddle left and right and when clicked the ball launches. what i am trying to achieve is have this running on my android device with the correct controls as at the moment upon testing it does not work well at all as the moment i touch the screen on the phone the ball launches and the paddle controls move strange! here are the scripts for the paddle and the ball if someone can help or point me right i will really appreciate it as only just finding my feet in unity. here is the paddle script
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;
}
}
using UnityEngine; using System.Collections;
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;
}
}
}
Comment