Building a Traffic System in Unity
Вставка
- Опубліковано 27 чер 2024
- In this video we take a look at how to build a simple but effective waypoint-based traffic system. With this system, you'll be able to create believable pedestrian and vehicle traffic behaviours that will add a much more lifelike feeling to your environments.
In this video we mainly focus on pedestrian traffic but the same logic could apply to vehicles in your game.
Be sure to LIKE and SUBSCRIBE if you enjoyed this guide! Share the video for extra love!
--------------------------------------------------------------------------------
Want to support the channel?
Get the GameDevGuide mug and other merch! ☕ - www.gamedevguide.store
Use these links to grab some cool assets from the asset store:
Get the Must Have Assets! - assetstore.unity.com/top-asse...
Free Unity Assets! - assetstore.unity.com/top-asse...
New on the Asset Store! - assetstore.unity.com/top-asse...
Top Paid Asset Store Packages - assetstore.unity.com/top-asse...
Asset Store Partners - assetstore.unity.com/lists/as...
--------------------------------------------------------------------------------
Socials and Other Stuff:
• Subscribe - ua-cam.com/users/gamedevguide?...
• Join the Discord - / discord
• Twitter - / gamedevguideyt
• Facebook - / gamedevguideyt
• Instagram - / gamedevguideyt
In under 20 minutes I learned how to make something that would have cost me $50 on the asset store
EXACTLLYYYYY!! Wonderful time to be alive!
more like 15 minutes
Compare it with this video, then.
I use an engine without an asset store. I have to make EVERYTHING!
Indeed! but much time required to build the whole system! Here comes asset store!
Yes please do a full fledged tutorial series on this! I would love to see how you make cars interact with pedestrians and traffic signs!
Man, I discovered your channel yesterday while looking at how to extend the Timeline! Since then the YT algo has fed me with 5 other of your videos, and I absolutely love your way of teaching, some might not realize the amount of work behind these but I'm very impressed. Thanks a lot.
dude, do you even realize how amazing is the content you are doing?
How do you not have a million subs yet? This was amazingly presented, and very easy to follow! Great stuff
Have you shared this video so he get a million of subs yet? Why braggin if not?
@@MaksMikhnevych I think you may have misunderstood what he was saying. He was saying that he deserves a million subscribers because of how well put together the video was. He didn't mean it in a negative way.
@@bmxchamp4 I think you misunderstood me. I was saying sharing video with others is a way to increase subscribers, while bragging about not having a 1m subscribers does nothing to it.
When you realize that your Unity Engine version doesn't like this code and so NOTHING WORKS.
This closed a huge gap in knowledge within only 18 minutes, amazing. Thank you for sharing
I'm interested in seeing how you would implement an anti collision system for pedestrians so they don't have glitchy bumpy movement due to colliders. I would imagine using a spherical or circular collider and a script that uses some sort of dampening or weight to slow down and push pedestrians around without being so harsh.
You could give them some sort of force field constraint or even make them physics based.
Or you could make it using a Unity build in navmesh to save ton of work. Set sidewalk to walkable, road to not walkable, twak some navmesh agent settings to get more natural movement and you can also easily implement obstacle avoidance. Then you can even make some interesting mechanics that will simulate how people normally move around. Example: go to friend's house > go for shopping > return home.
I'd do some research into flocking behaviors. That would even give the added benefits of people walking in groups. (But maybe also people randomly switching who they're friends with)
@@markiewicz.szymon navmesh does'nt help you in creating a lane traffics system
Though it might endup quite system taxing for a large project, but to avoid ped collision, you could add a raycast or preferably a spherecast to the AI script. And detect other objects before a collision occurs. This could also be added to for obsticles on the sidewalk and later for avoiding getting run over :)
Still waiting the sequel to this one, such a perfect video, the topic of npc behavior and population in games is so good, i love this, thanks bro!
Bro, the quality! I wish every tutorial was so on point as yours. Subbed and good luck with yt career!
Fantastic video as always Matt!
I love your teaching process. Looking forward to seeing your channel blow up! :)
Sweet , concise and on point. Where you don't leave the viewer with some dead end but with new possibilities.Please make more videos
This is one of the best examples I have seen, please continue this.
I'm really loving all your videos. Very informative with out holding your hand. Please keep up the great work. :)
Wow! Great tutorial! A rather complicated subject perfectly reviewed in under 20 minutes 👍🤓
This is exactly what I was looking for. Thank you!
wow, this is awesome dude. looking forward to going through your other videos!
Thank you ! You give a lot of very effective and new advices, compared to other guys, that’s very nice.
I learned a lot thanks to this tutorial, especially the way to make tools for easily editing a level.
Really nice work. I love both how thought out the traffic tool is and also you covering how to create the UI for it. Definitely create a sequel to expand on the traffic system like you mentioned.
If you would consider doing more tutorials that cover the basics of setting up an editor UI like the one you have here, I think a lot of people would be interested in that as well.
your contents are so helpful, I get to learn advanced stuffs that nobody else provides :)
Love your videos man and I can't wait to see more tutorials from you ;)
Holy shi! This is probably a Gem of a tutorial! Can't believe It took me years to find this type of quality content
Really would like to see the car traffic system you had in mind :)
2:08 When you said that I knew that this vid waqs going to be amazing! :D thanks
Great video Matt! Super cool stuff and really clearly explained. I'd be interested to see this interacting with Navmesh, which might also address some of the inter-person collision stuff. Great work!
Thanks Matt. Yup, I avoided navmesh for sake of simplifying the contents however I'm intending to cover it in the sequel video extending this system.
really good guide! thanks for sharing!
Dude, Fantastic work!
I am forever grateful for this tutorial
This is just absolutely brilliant. Thank you so much.
Great video mate. I've worked on a similar system but ended up using basic gizmos, having these unique editor controls would of made my work much easier 😮
Love your work!
I just discovered your Channel. You are a life saver
Great video! hopefully the tutorials about traffic system will continue ;)
Very good quality tutorial! Expanding on this topic would be awesome
That's awesome, thank you for this video!
Please do another one with more complex behaviour, or even cars! :)
Amazing. Instantly subscribed. Thank you so much. Please keep making awesome videos.
Thanks for a wonderful video! Indeed it is a great starting point for us to build a more complex system.
Thank you very much! Really useful, please continue :D
This is something unbelievable. Thank you very much!!
Traffic sounds really interesting! :)
very, very useful content. Great video!
Keep it up Matt.
This is AWESOME!
HEY PLZ GIVE THE navigation controller full code plz
Very cool. Well done!
You've earned a sub! Great content!
Brilliant asset, thank you for sharing your skills for free.
Would love to see more! 🤓
This is cool dude, your video is awesome. I learn a lot
Amazing tutorial!
Phenomenal video!
LOVE IT!!!!!!!! Thanks so much bro
I would love to see more!
Subscribed immediately, great job
Amazing, Just amazing!
fantastic tutorial thank you so much
As someone who knows game development but not c# this is a perfect way to help make crash coursing the language fun! I have to subscribe now lol.
This is really cool!
Great channel, lots of very useful content here. Could you do a tutorial on turn-based games? It's a topic that isn't covered in tutorials a lot so it will definately have value I think. I'm extremely interested in a system as used in the game "Dead in Bermuda".
Keep up the good work!
You did a really good job!! Thanks a lot.
HEY PLZ GIVE THE navigation controller full code plz
Great video. And cool system.
Amazing Video!
GTA V's traffic system must have a parameter called "stupidity" set to 200000
It actually does!
Devs made the AI capable of making bad decissions and failing in porpouse cause, as u know, idiots do exist in real life too. Sadly, as u also commented, that chance may be set to "2000000" cause it happens more often than it should x)
@@selvesa You're talking about GTA V and if their traffic system has a stupidity ratio?
Ofcourse it does, that's a really obvious question lmfao
@@selvesa Aaand literally all of peds have stupidity assign to them
It's incredibly impressive tvh
static BOOL IS_SOCCERMOM_AI() { return invoke(TRUE); }
Great video, thank you
Would love to see an expanded version of this.
This is just amazing..
Awesome video Eric! This can be extended into various possibilities. If it's possible please upload the project repo on github. Even the project without this demo scene will work too. Thanks!
I love it please make more videos and if possible can you make a video about how to add behaviour to the pedestrians
Keep up the good work.You've got new Subscriber
Would love to see one with vehicles!
You are Amazing!
Use should make probably more videos like this, can you make video on any of the topic like realistic combat and pakour
This tutorial is neat! It would be cool to see a few more things!
In a few demonstrations, it showed the Eric's spawning wherever they wanted to, but seeing them spawn on the path would be cool!
Also, I saw him cutting corners a little bit.
Perhaps we could also create a path that can only be travelled one way, so all of the traffic goes in one direction.
Also, a button to automatically close a path would be neat too!
it would be weirder if they WEREN'T cutting corners
Awesome. That's me subscribed.
nice vid and good/fast presentation. subbed. you can do mini series on same topic with 2 or 3 videos max. so you can dive into details and also change the topic shortly after the series for non interested viewers. for example, this video topic is good but it feels like it cut half. thanks.
Amazing...well done and very crisp tutorial..probably if you could teach c# I think everyone will learn it clearly..from your tuts
Cool.This video is very useful
HEY PLZ GIVE THE navigation controller full code plz
Another great video, would love more AI based tutorials. Thanks.
GODLIKE!
great job thank you very much good luck
Very comprehensive video, thanks a lot for sharing. Are you planning on sharing the project? The only thing that I would like to see is the behavior of pedestrians waiting by the traffic light when red. I know you could change the range to zero so pedestrians don't cross, but that wouldn't be the natural behavior. The expectation would be that the pedestrian chooses to head across but has to wait for the green light.
This video is amazing! Thank You!
Would you mind doing a video going over the CharacterNavigationController?
Agreed! Please go over the CharacterNavigationController
Not much difficult to write the script as 90% script has shared in the tutorial! I used this into my follow tut vid.
Here is what I came up with. I don't know if you're still working on the project, but maybe there are others that could benefit. I got to this by what he showed on screen in the script editor and what public variables were available in the Unity Inspector (currently, I don't have animations on my character so I cut those lines)-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PedestrianCharacterController : MonoBehaviour
{
public Vector3 destination;
Vector3 lastPosition;
public bool reachedDestination;
public float stopDistance = 1;
public float rotationSpeed;
public float minSpeed, maxSpeed;
public float movementSpeed;
Vector3 velocity;
private void Start()
{
movementSpeed = Random.Range(minSpeed, maxSpeed);
}
private void Update()
{
if (transform.position != destination)
{
Vector3 destinationDirection = destination - transform.position;
destinationDirection.y = 0;
float destinationDistance = destinationDirection.magnitude;
if (destinationDistance >= stopDistance)
{
reachedDestination = false;
Quaternion targetRotation = Quaternion.LookRotation(destinationDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
else
{
reachedDestination = true;
}
velocity = (transform.position - lastPosition) / Time.deltaTime;
velocity.y = 0;
var velocityMagnitude = velocity.magnitude;
velocity = velocity.normalized;
var fwdDotProduct = Vector3.Dot(transform.forward, velocity);
var rightDotProduct = Vector3.Dot(transform.right, velocity);
}
}
public void SetDestination(Vector3 destination)
{
this.destination = destination;
reachedDestination = false;
}
}
@@cassidynoble5466 Thank you so much, you saved me hours of confusion.
@@cassidynoble5466 Thank you for this! Also, do you have an idea of decelerating and accelerating the object at each waypoint to make it similar to vehicle behaviour?
Good Video...So this is the magic of video games ??? Cool
This is such an amazing video, one issue I've run into though is when you pause your game. There will be a NAN position error. I've found a workaround for this by not putting the timescale to 0 but to 0.001f. It's not the most beautiful fix but hey it's something.
high level deep knowledge ...
Feels like cyberpunk npcs to me, great job!
Did you ever make a video on vehicle traffic? Would love a detailed guide on that especially on a multi-lane road
Where to say but i dont care about trafic, this video is so useful for rts games
Love it
This is a really cool tutorial, a lot of things I didn't even know.
When you created a random direction at 12:18, I'm pretty sure you can just leave out the f in the random.range values, so
it will return an integer and you won't need to RoundToInt ^^
Edit: You got a like
I wish this was uploaded earlier! I already made my own traffic system. It is almost similar to yours except yours does not spawn depending on player position yet
That's a pretty nifty circular linked list implementation ;) Awesome content!!
Awesome video! maybe you can take this system a bit further. Thanks
This was amazing, how come you have so few subscribers?!? I only just found your channel and I instantly subscribed, I hope to see more videos of yours in future!
Great video, but did you ever get back to look into how to interact with vehicles and pedestrians together as you mentioned at the end?
I'd be interested to see how this would look if you use the waypoint system as one factor in some sort of boid controller. Then, you could add more clever avoidance of other pedestrians, random grouping of pedestrians for "friends" walking together (via attraction/speed matching), and I'm sure there's other things you could do with it. I might play around with this.
At 9:00, I think you forgot the line
newWaypoint.previousWaypoint = selectedWaypoint;
Fantastic video, though! This waypoint stuff is extremely useful.
This is a good starting point, however many improvements could be made (such as not having a game object for each waypoint)
Got the full screen of the controller for the crowd mesh? It seems that you just show few lines of it. Awesome tutorial; would love to see how did you solve the issue with car jerking around while calculating collision among each other and how to make them move realistically.
Oh man I want to do this since years but had no time for it. Good to see a video about it. I already made something similair in garrysmod (you can code ingame there and share results). I made there a AI vehicle which two modes, track and pathfind. Track is just looping a waypoint-path and pathfinding will work like a star. It was so nice to sit on own vehicle and see how it moves :D. Same thing I wanted to make for airplane aswell but yea.
i remember doing shit like this with gmod too, wanted to create an AI system with WAC because i prefered its more realistic controls to LFS' far too floaty controls. well, at least for helicopters anyway
@@dmas7749 Cool I used ACF sometimes as well. I have a helicopter with ACF cannons but parented to holos. It has no really AI but it follows commands like attack, move, land , fly to target position. It was funny to give the command to attack me so I had enemy to fight lol.
But I think my tanks had the AI stuff because they were hovering.
Do you plan to do a follow up on this video? It would be great to learn how to add cars to the scene as well.
This tutorial is great, but I want to ask, is this waypoint based on point-wise or area-wise waypoint ? since it looks better than normal waypoints
0:15 actually they survive the fall :D If you look at them, they never fall down, but instead, they are webbed to the wall
Hi nice tutorial I learned quite a bit . Question . How do you make them stop at random ratios and would any negative values through off the uniformed walking ??
Nice, I think we can expand this system to cars