• 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 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
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
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
Add comment · 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
2

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
Add comment · 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
0

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

thanks for the answers so far.

Comment
Add comment · 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

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

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

Roads and traffics system Made my system extremely slow 0 Answers

create waypoints with an array without using "Tags" 0 Answers

Board-game pathfinding system (Similar to Mario Party style) 1 Answer

Simplified vehicle physics for ambient traffic vehicles 1 Answer

Waypoints C# unity 3d 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