Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by POLYGAMe · Feb 15, 2012 at 09:41 AM · aislerpmovetowards

MoveTowards... a smoother way?

Hi there,

I have a basic AI script for my CPU controlled cars that requires LOADS of waypoints as I am using MoveTowards... which is very linear. I tried Slerp but that just did all kinds of crazy stuff. Is there an easy way to smooth out the transition between waypoints?

Here's my code:

 function OnTriggerEnter (other : Collider) 
 {
     if (other.gameObject.CompareTag ("Waypoints"))  // Checks if colliding with waypoint via tag
     {
         print(Waypoints[iIndex]);
         
         // When a waypoint is hit, increase array index so car moves on
         if (iIndex < (Waypoints.Length - 1))
         {
             ++iIndex;
         }
         else 
         {
             iIndex = 0; // Move car back to Waypoint 01
         }
     }
 
 }
 
 // TODO set up brake zone collision areas
 
 
 function Update () 
 {
     if (!LevelSetup.g_bGameIsPaused && LevelSetup.g_bGameInPlay == true)
     {
         // ACCELERATION
         if (!bCarIsBraking)
         {
             if (fSpeed < fMaxSpeed)
             {
                 fSpeed += fAcceleration;
             }
             else
             {
                 fSpeed -= fBrakeForce;
             }
             
              transform.position = Vector3.MoveTowards(transform.position,Waypoints[iIndex].transform.position,(fSpeed * Time.deltaTime));
          }
     }     
 }

Cheers!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by FishBone · Feb 15, 2012 at 04:48 PM

You probably want to calculate a hermite spline.

Found this link on google, have not tried it myself so can't take any responsibility https://www.auto.tuwien.ac.at/wordpress/?p=495

Or you could make your own function using the math on http://cubic.org/docs/hermite.htm

Comment
Add comment · Show 2 · 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 POLYGAMe · Feb 15, 2012 at 07:16 PM 0
Share

That looks awesome! I think that's pretty much what I need. I'll have a play around today. I always feel a bit guilty about using someone else's code and I do like to try and do it all myself, as I am still learning, but what the hell... that's what it's there for :)

avatar image shaneparsons · Jul 05, 2016 at 07:07 PM 1
Share

First link is broken

avatar image
0

Answer by Owen-Reynolds · Feb 15, 2012 at 05:07 PM

A cheap way is to always move forwards, but smooth-in your rotation. In other words, when you get close to a waypoint, the car starts to turn towards the next one. This is from a working project (which didn't look too bad):

 Vector3 dest2 = dest; dest2.y = transform.position.y; // only rotate around y
 Quaternion wantFace = Quaternion.LookRotation(dest2 - transform.position);
 transform.rotation = Quaternion.RotateTowards(transform.rotation,
               wantFace, 90 * Time.fixedDeltaTime);
 // 90 is degrees/sec

Then just set velocity or position based only on transform.forward.

Comment
Add comment · Show 4 · 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 POLYGAMe · Feb 15, 2012 at 07:15 PM 0
Share

Thanks Owen, the problem is with my game is that I don't really use rotation as it's a simple 2D quad with sprites mapped to it, so it's always facing the camera. I only rotate the GameObject to face the next waypoint so that the math isn't thrown out for changing the textures, depending on the relevant angles to the other player cars. It's a 2.5D SNES-styled "$$anonymous$$ode7" racer... hope that makes it more clear what I'm trying to get across. LOL.

avatar image POLYGAMe · Feb 15, 2012 at 07:49 PM 0
Share

Hmmm, looking at that I'm not sure if I'll be able to slow my cars down to simulate braking into a corner... ah, I'll figure it out. LOL.

avatar image Owen-Reynolds · Feb 15, 2012 at 10:01 PM 0
Share

If you wanted to use physics but 2D-style graphics, you can create a modelless rigidbody/collider. Your 2D sprite could track the position only (in code,) and rotate on its own, using your current scheme.

avatar image POLYGAMe · Feb 15, 2012 at 11:00 PM 0
Share

Yeah, that might work. I've got it working pretty well at the moment with LOADS of waypoints... gets the job done... but I might have a play anyway.

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

8 People are following this question.

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

Related Questions

Basic AI Locked Axis 1 Answer

Can you help me make this code turn smoothly? 0 Answers

How do I moveTowards all the items in a list one at a time? 1 Answer

Error with Rotating script 2 Answers

how to stop something from following in the y axis 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