Hey, i am working on a 2D car racing game for android device. I have coded the touch movement for my car. But the problem is the car is going beyond the track. How can i restrict the car movement, i mean how can i code for my car to stay in the screen(Screen resulation is 480*800 and car sprites max position is 4.2 and min -4.2). Here r my C# car controller script.
using UnityEngine;
using System.Collections;
public class carController : MonoBehaviour {
public float carSpeed;
// Update is called once per frame
void Update () {
if (Input.touchCount == 1) {
Touch touch = Input.touches[0];
if(touch.position.x < Screen.width/2){
transform.position += Vector3.left * carSpeed * Time.deltaTime;
}
else if(touch.position.x > Screen.width/2){
transform.position += Vector3.right * carSpeed * Time.deltaTime;
}
}
}
}