• 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 Sondre-S · Jan 10, 2014 at 11:53 PM · waypoint systemtraffic

Okay approach for TrafficAI?

I've written my traffic logic this way:

  • Each car uses a waypoint script that reads from a path/waypoint container.

  • When the car reaches the end of the path(at an intersection or T-junction) it access a script in the waypoint container object, which contains an array with 3 new paths/waypointcontainers.

  • It uses a random number (between 1 and 3)to get a new waypoint container from this array.

It works great right now but, when I have "mapped" my city model this way, there will be lots of scripts "linked" together. I'm worried that this is a bad way to do it(performance wise).

Thanks in advance:)

Comment
xt-xylophone

People who like this

1 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by HappyMoo · Jan 16, 2014 at 03:16 AM

That's a very basic system... your cars don't go anywhere... they just drive around randomly. But if that's all you need, it can be implemented very high performance.

The number of scripts doesn't matter if you do it right, because you don't need to do work per link, but only work per car. So I could have a street network with one million nodes, but every car just knows the node it's coming from and the node it's going to and maybe some values it calculated once when it decided which node to target next, so it doesn't need to calculate it every time, like: startTime, completeDrivingTime. So every time it decided which node it needs to go next, it does:

 distance = (target-origin).Magnitude;
 completeDrivingTime = distance/drivingSpeed;
 startTime = Time.time;

and then in each frame you just do this:

 drivingTime = Time.time-startTime;
 if (drivingTime>=completeDrivingTime) pickNewTargetAndTurnCarInDirection();
 else
 {
   Vector3 newpos = origin + (target-origin) * (drivingTime/completeDrivingTime);
   transform.position = newpos;
 }

when it reached the destination, it picks the next node. It doesn't matter how many nodes you have, besides from memory, because just the cars are the active parts. There's also no kind of Physics or the likes involved.

Comment
Jamora
BritishGuy

People who like this

2 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 HappyMoo · Jan 21, 2014 at 03:12 PM 0
Share

did that help?

avatar image Jamora · Jan 21, 2014 at 04:16 PM 1
Share

I'm fairly certain the OP accepted this answer but just forgot to mark it. If I'm wrong, the OP may unmark (demark?) this.

avatar image Sondre-S · Jan 22, 2014 at 04:24 PM 0
Share

sorry, I'm new here and I didn't know I had to mark the answers.

avatar image

Answer by xt-xylophone · Jan 15, 2014 at 09:48 PM

It sounds like a decent way to do it. It mainly depends how you are doing the car/node interaction. Doing tonnes of distance checks will be a huge overload, using Unity's trigger system would be the best way. I.e. put a trigger on the intersection and in the car script it will have OnTriggerEnter which will call when it's collider enters the trigger.

Id also recommend using Gizmos to draw lines between your intersection points so you can quickly make sure all your roads link up. They're pretty easy to use, like if you have a list of nodes in each node, loop through them and use Gizmos.DrawLine to draw a line between them, maybe a little of the ground too so you can actually see it.

Comment
Sondre-S
BritishGuy

People who like this

2 Show 0 · 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

Answer by Sondre-S · Jan 22, 2014 at 04:22 PM

thanks for the answers so far.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How can I switch paths by pressing a button? 1 Answer

Train Track pathfinding - Navmesh? Waypoints? Spline? 1 Answer

Handling large scale data for traffic simulation 1 Answer

How to play an animation with waypoints 1 Answer

correct waypoint system? 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