• 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
1
Question by MD_Reptile · Dec 02, 2012 at 06:44 PM · aicarwaypointvehiclesteering

Steering an AI vehicle to a waypoint using float of -1 to 1?

NEW (Updated) QUESTION:

Forum Thread: http://forum.unity3d.com/threads/161147-Basic-City-Traffic-System-Need-help-getting-it-working-right.

WebPlayer: http://ninjeticstudios.com/WebPlayerAIDriver/Webplayer/webplayer.html

I created a simple AI to follow waypoints, by getting the angle of my vehicle, and the angle to the next waypoint, and having my vehicle turn to face that angle. Its not the best though - I need a better way to get it from waypoint to waypoint while maintaining its lane!

Now it finds its next waypoint, and tries to drive around, but, it sort of starts "oscillating" around the line to the next waypoint (my lanes). I got rid of it a little, by expanding my float from just -1 or 1, to allow numbers in between, but now it sort of is slower to respond, but drives wit less oscillation now. How can I get it really accurate to its lane, so it looks natural in its movement and maintains that lane better?

this bit of code deals with getting to the actual waypoints by steering:

 FindAngle();
         
         // Right Turning
         if(DesiredAngle - OurAngle >= 3 && DesiredAngle - OurAngle <= 18)
         {
             CarControlAI.SteerInput = 0.2f;
         }
         if(DesiredAngle - OurAngle >= 19 && DesiredAngle - OurAngle <= 36)
         {
             CarControlAI.SteerInput = 0.4f;
         }
         if(DesiredAngle - OurAngle >= 37 && DesiredAngle - OurAngle <= 54)
         {
             CarControlAI.SteerInput = 0.6f;
         }
         if(DesiredAngle - OurAngle >= 55 && DesiredAngle - OurAngle <= 72)
         {
             CarControlAI.SteerInput = 0.8f;
         }
         if(DesiredAngle - OurAngle >= 73 && DesiredAngle - OurAngle <= 90)
         {
             CarControlAI.SteerInput = 1.0f;
         }
         
         // Left Turning
         if(DesiredAngle - OurAngle <= -3 && DesiredAngle - OurAngle >= -18)
         {
             CarControlAI.SteerInput = -0.2f;
         }
         if(DesiredAngle - OurAngle <= -19 && DesiredAngle - OurAngle >= -36)
         {
             CarControlAI.SteerInput = -0.4f;
         }
         if(DesiredAngle - OurAngle <= -37 && DesiredAngle - OurAngle >= -54)
         {
             CarControlAI.SteerInput = -0.6f;
         }
         if(DesiredAngle - OurAngle <= -55 && DesiredAngle - OurAngle >= -72)
         {
             CarControlAI.SteerInput = -0.8f;
         }
         if(DesiredAngle - OurAngle <= -73 && DesiredAngle - OurAngle >= -90)
         {
             CarControlAI.SteerInput = -1.0f;
         }
         
         if(DesiredAngle - OurAngle < 3f && DesiredAngle - OurAngle > -3f)
         {
             CarControlAI.SteerInput = 0;
         }

I guess I need to get a smoother transition between floats, as well as knowing to turn sharply if the waypoint is close...hmm

ORIGINAL QUESTION (Not dealing with this same problem so much):

I have a physics based vehicle AI, specifically the dodge charger from the unity car tutorial "alt" system. It takes input to move forward and brake, and a float for turning between -1 and 1.

I need to take my current waypoint and get the angle of that waypoint from the car, and start turning the vehicles wheels to face the waypoint. I feel dumb but all my attempts are junk so far.

How can I move them around by waypoints: Clue me in, how do you point your car at those waypoints with -1 to 1 input? Do you take the angle in relation to your car, then just turn until that angle gets smaller?

How could this be done? You rock if you have an actual script showing how to do this, and you really rock if its in C# :)

So let me clarify, the steering of my PHYSICS BASED VEHICLE is controlled by a float, which is from -1 to 1, and if its -1, steering is completely left, and if its 0.5 then steering is halfway to the right yaknow....

Here is a quick image to show you what I want incase I am explaining bad:

http://imageshack.us/a/img703/2941/turncarunity.png

Comment
Add comment · Show 1
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 TheDarkVoid · Dec 02, 2012 at 07:57 PM 0
Share

you can use a script to deter$$anonymous$$e whether the next waypoint is on the right or left, and then just turn right/ left directly

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by kag359six · Dec 02, 2012 at 09:39 PM

I'm not EXACTLY sure what you are asking for. Do you want the car to turn fully to the left if the angle between the waypoint and the car is negative? and vice versa if positive? This code should rotate the car towards the waypoint (not tested so not sure).

 public Vector3 carPos;
 public Vector3 waypointPos;
 
 Vector3.RotateTowards(carPos, waypointPos, speed * Time.deltaTime, 0f);

Or maybe you can check the angle between the two objects and if it is negative return -1 otherwise return 1.

     public int returnDirection(Quaternion carRot, Quaternion waypointRot) {
         
         float angle = Quaternion.Angle(carRot, waypointRot);
         
         if(angle > 0 && angle < 90) {
             
             return 1;    
             
         } else if(angle > 90 && angle < 270) {
             
             return -1;    
             
         } else if(angle > 270 && angle < 360) {
             
             return 1;    
             
         } else {
             
             return 0;    
             
         }
         
     }

Okay so here is what I'm thinking:

 public float returnDirection(carRotation, waypointRotation) {
 
      float angle = Quaternion.Angle of carRotation and waypointRotation
 
      return the sin of "angle"
 
 }
Comment
Add comment · Show 6 · 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 kag359six · Dec 02, 2012 at 10:39 PM 0
Share

lol sorry i forgot to set the return type ill update the answer

avatar image kag359six · Dec 02, 2012 at 10:52 PM 0
Share

I changed the code for returning -1 and 1. before it didnt work. But now if you are facing in the direction of the waypoint, it should return positive, and will return negative if facing away. The catch though is that it only returns -1 or 1, not in between. It's not exactly what you want but it's a start.

avatar image kag359six · Dec 02, 2012 at 11:10 PM 0
Share

I was playing around with that code, and I think if you return the Sin of the angle between the car and waypoint it will get you what you are looking for. That way it will return a value for every direction, such as 1 if it is 90 degrees, or 0.7 if it is 45 degrees. However when i used Unity's mathf.Sin it doesn't return the same value a calculator would, so you might need to find a work around. It may have something to do with Unity using radians but I'm not familiar with that. I'll write pseudo code for you (its not usable) to demonstrate what i mean. (will be in an edit of my answer)

avatar image MD_Reptile · Dec 02, 2012 at 11:36 PM 0
Share

@kag359six

new problem same subject - see revised question

avatar image kag359six · Dec 03, 2012 at 10:13 PM 0
Share

$$anonymous$$aybe you should set waypoints for each lane, and depending on the lane the car is in, you use the waypoints for, lets say, the left lane, or the right lane depending on where the car is. I'm not sure as it is getting difficult to keep up with your progress lol. Perhaps setting a variable (such as the -1 and 1 that you used for turning) for what lane the car is in can help. $$anonymous$$y thoughts are kind of all over the place, so let me sum it up. $$anonymous$$ake two seperate sets of waypoints for each lane, and depending on what lane you want the car to be in, you change a variable that switches the lane and therefore changes the set of waypoints.

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.

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

11 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

Related Questions

Unity 5 Standard Asset Car Waypoint AI problem with accuracy 0 Answers

Steering Overcompesation 0 Answers

how to make a vehicle drive down a road 1 Answer

Cars in City 0 Answers

Check whether car has passed a point in world space 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