Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by siddharth3322 · Mar 06, 2019 at 12:13 PM · 3dtouchcartouch controlsrotatetowards

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 20 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siddharth3322 · Mar 06, 2019 at 01:36 PM 0
Share

@highpockets thanks for your quick reply - let me test this and reply back to you about result.

avatar image siddharth3322 · Mar 08, 2019 at 04:03 PM 0
Share

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.

alt text

So what to do next in this??

single-touch-direction.png (47.0 kB)
avatar image highpockets siddharth3322 · Mar 08, 2019 at 05:05 PM 0
Share

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.

avatar image highpockets siddharth3322 · Mar 08, 2019 at 05:26 PM 0
Share

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.

avatar image siddharth3322 highpockets · Mar 11, 2019 at 09:09 AM 0
Share

Here is the debug rays output as per the above code: alt text

I have also tested car.transform.forward but it's not giving proper values.

Here is the car following camera position: alt text

car-rays.png (151.8 kB)
car-camera-position.png (62.4 kB)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges