- Home /
Give Car Direction Using Single Touch
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.
Answer by highpockets · Mar 06, 2019 at 12:48 PM
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.
@highpockets thanks for your quick reply - let me test this and reply back to you about result.
I have implemented something like this:
if (Input.Get$$anonymous$$ouseButton(0))
{
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
Vector3 carToWorldTouchPositionDirection = transform.position - touchPosition;
Vector3 direction = Vector3.ProjectOnPlane(carToWorldTouchPositionDirection, Camera.main.transform.forward);
Debug.Log("direction: " + direction);
}
Whether I touch on the car left or right, its always giving me the same result.
So what to do next in this??
You’re subtracting the touch position from the car position which should give you the opposite of what you need, but it should be giving you different results if you click on either side of the car in any case.
Try adding:
Debug.DrawRay(transform.position, (touchPos - transform.position) 5, Color.green); Debug.DrawRay(transform.position, direction 5, Color.red);
The green will show the direction before you project it to the screen and the red will show you the projection on the screen but from the car.
Check that and let me know what happens.
I also assumed you had the camera behind the car, but from the direction vector readings, it looks like you are above. You might want to try changing the normal in ProjectOnPlane() from camera.transform.forward to car.transform.up (or whatever direction vector points through the roof of the car if you are top down.. that will project the direction on the plane that the car wheels are parallel to.
Here is the debug rays output as per the above code:
I have also tested car.transform.forward but it's not giving proper values.
Here is the car following camera position:
Your answer
Follow this Question
Related Questions
Move object with touch 3D with script 0 Answers
Force 3D touch IOS 1 Answer
3d car left and right touch input 2 Answers
Unity Touch Help! 0 Answers
How to detect whether a particular touch has ended? 1 Answer