I want to give my 3d car driving direction using a single touch as per this game reference:
The New #1 Game On The App Store! | Hyperball Gameplay
At present, I have a rotate car method is ready to turn the car in the desired direction, here is the overall structure of the code:
private void RotateCar(int direction)
{
if(direction == 0) {
// move car straight
}
else
{
// turn car left (-1) or right (+1)
}
}
Rotation related code, I have checked with two touches on screen (touch the left side of the screen to turn left, touch on the right side of the screen to turn right), it’s working properly.
Now I want to control the car using a single touch. Currently, I have similar to this kind of plain environments setup.
Endless Car Chase Game Template
At present, I am looking for some code so that I can provide proper value to my RotateCar method.
Now provide your side suggestion to implement single touch car controlling.
You can get the touch position and turn it to world space:
point = cam.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, cam.nearClipPlane));
You can then test that against your car position. For example you could get the direction from the car position to the world touch point by subtracting the car position from the world touch point and then use Vector3.ProjectOnPlane like so:
Vector3 direction = Vector3.ProjectOnPlane(carToWorldTouchPointDirection, camera.transform.forward);
That is getting the direction from the car position to the touch position and then translating it to a direction on the camera viewport plane and thus you will have your direction. This is expecting that your car isn’t always going to be in the center of screen, if it is going to be in the center of screen, you can just stay in screen space and find the halfway point of your screen and if your touch position.x is more than halfway point.x, the direction is right if its less than the direction is left. Then the further the point is from the halfway point, the more noticeable the turn.