• 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
Question by $$anonymous$$ · May 01, 2017 at 03:12 PM · vehicles

Steer towards target

So, t$$anonymous$$s is somewhat of a loaded question and I will try to do my best to explain it.

I am creating a ve$$anonymous$$cle AI system, and the ve$$anonymous$$cle has a Steer(x) function. X ranges from -1(steer left) to 1(steer right).

I'm trying to find the value that I need to pass to that function to steer my ve$$anonymous$$cle accordingly to a target, in t$$anonymous$$s case the next checkpoint.

An example: Imagine a ve$$anonymous$$cle to the right of a checkpoint, t$$anonymous$$s function should return somet$$anonymous$$ng like -0.2 to steer the ve$$anonymous$$cle back towards the checkpoint.

I've posted t$$anonymous$$s question on other forums, and had some pretty indirect answers that more or less just confused me. All of them did bring up trig functions however, but it's somewhat difficult for me to understand being a freshmen in HS.

If somebody could provide any insight on the way to ac$$anonymous$$eve somet$$anonymous$$ng like t$$anonymous$$s, it would be awesome. Thanks!

Comment
ossianboren

People who like this

1 Show 2
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 $$anonymous$$ · May 05, 2017 at 12:59 AM 0
Share

Knowledge bump

avatar image ossianboren · Dec 13, 2018 at 09:05 AM 0
Share

I have the exact same problem. I followed the unity wheel collider tutorial, but changed the steering part. So far my car just goes in circles. My code looks like this:

 directionAngle += Input.GetAxis("Horizontal") * 0.01f;
     rotationAngle = transform.rotation.y;
     if (rotationAngle > Mathf.Sin(directionAngle))
     {
       steering = rotationAngle - Mathf.Sin(directionAngle);
     }
     else
     {
       steering = rotationAngle + Mathf.Sin(directionAngle);
     }
     float clampedSteering = Mathf.Clamp(steering, steeringDampening, -steeringDampening);


1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Kishotta · May 05, 2017 at 01:10 AM

I actually accomplished somet$$anonymous$$ng similar by doing some vector manipulations. if a is the vector from the ve$$anonymous$$cle to the target and b is the target's forward direction, you can get a -1 to 1 steering value by doing Vector3.Dot (Vector3.Cross (a.normalized, b.normalized), Vector3.Up);

Vector3.Cross (a.normalized, b.normalized) gets you a vector pointing either directly up or directly down (assuming you're on flat terrain) with lengths from 0 to 1. The dot product with Vector3.Up will then give you a range from -1 to 1 with -1 being "Target is 90 degrees to the left" and 1 being "Target is 90 degrees to the right". Smaller steering values are given if you're more-or-less on the right track.

I don't have the code with me anymore, so I cant remember for sure if we used Vector3.Up or Vector3.Down, but it should be pretty easy to figure out w$$anonymous$$ch to use with trial and error.

Disclaimer, t$$anonymous$$s ONLY works if the target is actually in front of the ve$$anonymous$$cle. If it's be$$anonymous$$nd (even by just a little bit) you'll get the wrong steering. We solved t$$anonymous$$s by having lots of checkpoints around curves, and generally not having and sharp (>90 degree) angles between checkpoints.

Comment

People who like this

0 Show 3 · 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 $$anonymous$$ · May 24, 2017 at 10:03 PM 0
Share

Thanks for the help,

I found that the AI system was really, well stupid. It seemed to try to over correct itself to much, where it positioned itself in the exact center of the next checkpoint. It would under correct, then over correct throwing it into a spiral.

Do you have any advice on how to make something like this "smarter"?

avatar image Kishotta $$anonymous$$ · May 24, 2017 at 11:26 PM 0
Share

Oh wow. I was really not thinking when I answered this. It's only giving you -1 OR 1, not a range. I'm really sorry I didn't test that before answering.

You can get the magnitude of how much you should steer with Vector3.Cross (a.normalized, b.normalized) and which way you should steer with Vector3.Dot (Vector3.Cross (a.normalized, b.normalized), Vector3.up).

Sorry for the confusion. Should look something like this:

 Vector3 steerCross = Vector3.Cross (vehicleToWaypoint.normalized, transform.forward); // How much to steer (stored in the magnitude)
 float steerDirection = Vector3.Dot (steerCross.normalized, Vector3.up); // Which way to steer (-1 for right +1 for left)
 Steer = steerCross.magnitude * steerDirection; // Multiply them together to get get final steering
avatar image ossianboren · Dec 14, 2018 at 08:09 AM 0
Share

"Disclaimer, this ONLY works if the target is actually in front of the vehicle. If it's behind (even by just a little bit) you'll get the wrong steering. We solved this by having lots of checkpoints around curves, and generally not having and sharp (>90 degree) angles between checkpoints."

There has to be a way to do this without this happening?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how do I make a character enter a vehicle? 1 Answer

Best practices for multiplayer multi crew vehicles. 1 Answer

Whats the correct way of animating vehicles? 0 Answers

Save a player created gameobject with children? 0 Answers

How are cars modelled 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