- Home /
Touchscreen car controls
So I'm developing a game for Android devices. The game is about a taxi in a city. The car, so far, is fully functional on PC (acceleration, steering) but I'm stuck on a part where I would create on screen controls for touch screen. Just four buttons, 2 for acceleration, 2 for steering. Please help me if You can, because I'm stuck on this part for like 3 days and 3 nights.
Have you had a look at the official unity tutorials for the iOS?
Answer by Ranger-Ori · Jul 05, 2012 at 03:50 AM
I've never worked with iOS, however, did you read the scripting reference for the iOS? http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=ios
Answer by nixtwiz · Jul 05, 2012 at 03:49 AM
Use planes in front of the camera with textures for the buttons. Then, send a ray out from where the player touches the screen. Depending on what plane the ray hits, different things happen.
I have no previous raycast experience, but I'll try to do this how I just learned it off some google searching :)
//In javascript
function Update(){
for(var touch : Touch in Input.touches){
if (touch.phase == TouchPhase.Began) {
var theRay : Ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit ;
if (Physics.Raycast (theRay, hit)) {
if(hit.transform.name == "Drive"){
Accelerate();
}
if(hit.transform.name == "Brake"){
Brake();
}
if(hit.transform.name == "TurnLeft"){
TurnLeft();
}
if(hit.transform.name == "TurnRight"){
TurnRight();
}
}
}
}
}
function Accelerate(){
//Do something
}
function Brake(){
//Do something
}
function TurnLeft(){
//Do something
}
function TurnRight(){
//Do something
}
I haven't tested it in a game, but it compiled without error. Hope it works :)
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Moving object to position of finger at screen 1 Answer
iOS / Android Steering 2 Answers
android gui 0 Answers
Android touches called the Input.GetAxis('Mouse X') 0 Answers