Тёмный

CarAI-Unity: Demo 

Ahmed Saed
Подписаться 123
Просмотров 3,6 тыс.
50% 1

A C# script that uses NavMesh for path finding and simulates vehicle movement using unity's physics components
Links:
NavMesh Components: github.com/Uni...
Package Repo: github.com/Ahm...
Asset Store: u3d.as/21wR

Опубликовано:

 

17 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 63   
@alvinwafulalusenaka1247
@alvinwafulalusenaka1247 6 месяцев назад
Hello. How can I implement a traffic system where the vehicles don't collide with each other?
@ahmedsaed26
@ahmedsaed26 6 месяцев назад
You would add colliders to the car object and write a small script that utilizes the public variables defined in the carAI script to stop/move the car.
@xdekuz
@xdekuz 2 месяца назад
Can you help me please? I have a problem. After starting the game, AI creates a path and goes through the checkpoints, so, he generates them once and goes through them, which is why the player can just leave, in short, how do I make the AI change checkpoints faster? I'm sorry if the question is incorrect, I'm writing through a translator
@pengyanjia1729
@pengyanjia1729 2 года назад
Is the front of the car determined by the front car object or the car object itself? Because it seems like the latter for me. For some reason my car keeps driving backwards
@ahmedsaed26
@ahmedsaed26 2 года назад
Hi Peng, The front of the car is an empty child object that's placed at the front of the car model The reason behind this is that each model has different dimensions and it's kinda hard to calculate the front from the model itself But this has nothing to do with the car moving backwards, there are couple of reasons for that, - Make sure that MovementTorque is a positive number - Make sure that the objects with the wheel colliders are not flipped
@ContinuedOak
@ContinuedOak 2 года назад
Hey Man, took some time but i got it working, but I have one questions, where do I parent the car parts too (for example wheels and body), no matter where it goes it deforms the model
@ahmedsaed26
@ahmedsaed26 2 года назад
The car parts should be children of the the car object (this object has the script attached to it) similar to the video You should also try to separate the wheel collider from the meshes (geometry) as this causes a lot of problems. The wheel collider fields and the wheel geometry fields in the script shouldn't point to the same objects
@ContinuedOak
@ContinuedOak 2 года назад
@@ahmedsaed26 thank you, it’s currently night time so I’ll try this in the morning
@xdekuz
@xdekuz 2 месяца назад
Thnk you! The best car AI!
@raprawira18
@raprawira18 10 месяцев назад
Hey, my AI car starts rotating up, instead of moving on the ground. Is there a reason you might know as to why this happens?
@ahmedsaed26
@ahmedsaed26 10 месяцев назад
It's really hard to know without actual debugging but it's likely a problem related to the physics "aspects". Make sure that the wheel colliders are setup correctly and that they are not overlapping with other colliders If I were you, I would try to setup the car with a simple script to make sure everything is okay with the setup, then I would use the CarAI script. If you are still stuck or have any questions, don't hesitate to ask.
@raprawira18
@raprawira18 10 месяцев назад
@@ahmedsaed26 I finally fixed it. The colliders were overlapping each other. Thanks for the reply. But another question, is it possible to make another custom destination?
@kapalk9040
@kapalk9040 9 месяцев назад
First of all, well done. Your code works normally when I connect it to the vehicle or something, but I want to make changes in the code. When I open the code, the words 'destination' and 'sourcePostion' appear underlined in the public void RandomPath() and public void CustomPath items of the 'CarAI' script. how can i fix this?
@ahmedsaed26
@ahmedsaed26 9 месяцев назад
Thank you for reaching out. The two variables that you mentioned is defined in the code and the code works correctly. Perhaps it's an issue with your IDE or the syntax highlighter that you are using. Anyway, You can find the `sourcePostion` variable defined at line 137 and line 190 for `RandomPath` and `CustomPath` functions respectively and you can find the `destination` variable defined as a parameter to the local function `calculate` inside both `RandomPath` and `CustomPath` functions. (Based on github.com/Ahmedsaed/CarAI-Unity/releases/tag/v1.0.0) If you have any further questions or concerns, feel free to let me know. I'm here to help!
@mr_shaamii
@mr_shaamii 6 месяцев назад
@@ahmedsaed26 please you can explain whole code of working
@ahmedsaed26
@ahmedsaed26 6 месяцев назад
That would be hard considering that this project was 2 years ago (I barely remember anything about it) and it depends on a lot of mathematics and computer science concepts
@britsnoike4203
@britsnoike4203 Год назад
hey man, is it possible to add obstacle avoidance during runtime, for eg if i put a block infront of the car while its moving, it should stop
@ahmedsaed26
@ahmedsaed26 Год назад
Yes, it's possible to setup obstacle avoidance because the script uses the unity AI API and unity navmesh layers So you just add the obstacles using the NavMesh components
@britsnoike4203
@britsnoike4203 Год назад
@@ahmedsaed26 no but i cant do it during runtime, with other vehicles moving around. any tips for that?
@ahmedsaed26
@ahmedsaed26 Год назад
I have managed to achieve this 2 years ago using colliders, I just added colliders around the vehicle and wrote a script that uses the the public variables to control the car movement (stop it or lower the speed)
@britsnoike4203
@britsnoike4203 Год назад
@@ahmedsaed26 yes but is there a way to make it go around it, if a car stops infront it shouldnt just stop, it should move around it, in that sense
@ahmedsaed26
@ahmedsaed26 Год назад
I created this script to serve as the backbone of your navigation system. You would need to modify and implement a lot of logic to get it working for your use cases. With that being said, it's possible to implement a realtime obstacle avoidance. However, it won't be an easy task as you will need to implement a lot of logic and algorithms for it to work properly. What prevented me from implementing such logic was that it won't be compatible with all environments and would cause other scenarios to break. Hope this helps, feel free to ask any questions
@clashface
@clashface 8 месяцев назад
when ever there is a normal right or left turn there is two way points created and the car misses the sencond one sometimes and its annoying because it has to take a u turn just to take that waypoint...any fix ?
@ahmedsaed26
@ahmedsaed26 8 месяцев назад
Hey 👋, I understand your problem but sadly I don't think that there is an easy fix that works on most scenes This problem is related to the Unity AI API which is responsible for the path generation The script only models car physics to work on the existing Unity's AI API
@clashface
@clashface 8 месяцев назад
@@ahmedsaed26 hey bro can u edit your script a bit that does not stop the car while patrolling at random. in your script the car stops after a while after patrolling I don't want that to happen. I want the car to keep on going on random path without stopping, how can I achieve this.
@ahmedsaed26
@ahmedsaed26 8 месяцев назад
I made it stop as an optimization technique so you don't end up having multiple cars using resources while outside of the player view distance I stopped working on this script 2 years ago but feel free to update it yourself
@clashface
@clashface 8 месяцев назад
@@ahmedsaed26 I just don't want the car to stop, I just want the car to keep going, if u help me with that it would be very helpful.
@ahmedsaed26
@ahmedsaed26 8 месяцев назад
I just took a look at the code and apparently I didn't include the optimization technique that I explained earlier in the package So there is nothing that would make the car stop other than falling to generate a new path that's in the AIFOV (check the documentation)
@emirhana61
@emirhana61 4 месяца назад
I have a 2 lane road. I want the cars to drive in their own lane, how do I do it?
@ahmedsaed26
@ahmedsaed26 4 месяца назад
If your problem is that the whole road (the 2 lanes) are treated as one lane. Then this should be easy to fix. Modify the road prefab to include a hidden object between the lanes. This hidden object should be excluded from the navmesh. This way, when you bake the navmesh, the the lanes will be separated and the car wouldn't have a way to switch lanes. However, if your problem is that you want to instruct the AI to only generate a path in the forward lane. (i.e. exclude the other lane from path generation) That would be hard because Unity AI API wasn't designed with the support of vehicles in mind. The only way I can think of is to apply different navmeshs to each lane type and develop a system that would evaluate the generated path.
@emirhana61
@emirhana61 4 месяца назад
@@ahmedsaed26 Actually, there are 4 lanes in my game, 2 going and 2 coming. These are like normal streets and at the end of each street there are intersections where 4 roads meet. How can I? I'm sorry if I didn't explain it clearly. If you want, if you have a social media account or gmail, I can send you pictures from there.
@emirhana61
@emirhana61 4 месяца назад
@@ahmedsaed26 can you help me
@radu1156
@radu1156 Год назад
hey , i wonder how its possible to add multiple waypoints for the ai ro follow from one waypoint to the next one ?
@ahmedsaed26
@ahmedsaed26 Год назад
You can achieve this by creating your custom script that hadels the waypoints You can save all waypoints and iterate over them, every time the ai reaches a waypoint you set the next waypoint using the AI.CustomPath method Take a look at the documentation especially the programming section Hope this helps.
@ciss7603
@ciss7603 2 года назад
i did all the steps but my car just do nothing, i already bake the navmesh, assign the wheel transform and collider, and the gizmo is already showing if i tick it on
@ahmedsaed26
@ahmedsaed26 2 года назад
Hi Ciss, I am sorry for the inconvenience. There are a lot of reasons for this issue But, I think it's because there is no possible path for the AI to take. Please refer to the documentation for more info Can you also please follow the steps in the video on a new scene like the one in the video. if it works then the problem is with your environment/scene. Try: - to move the AI to another starting point - edit the code to tailor it to your environment/scene
@ciss7603
@ciss7603 2 года назад
update, i tried copied the object from your demo scene, change the mesh renderer and material etc, it worked, but sometimes some cars stop after running the scene for a while
@ciss7603
@ciss7603 2 года назад
@@ahmedsaed26 you dont have to apologize, i am very thankful to you for your codes etc, thank you so much, you help me finish my project for my college, i can feel that the problem is in my terrain and navmesh, probably, once again, thank you so much!!
@gokb01
@gokb01 2 года назад
I added this to my assets from asset store but I can't see NavMeshSurface. Can you help me?
@ahmedsaed26
@ahmedsaed26 2 года назад
Hi Goktug, You need to import another package provided by unity called NavMeshComponents Please read the documentation or follow the video tutorial for more details Hope this answers your question ❤️
@gokb01
@gokb01 2 года назад
@@ahmedsaed26 It helped me thanks. But now when I press "Bake", it does not go to purple like yours. I did same things as you, can you help?
@ahmedsaed26
@ahmedsaed26 2 года назад
Can you please provide more details? I can't really help without knowing the issue Please follow the tutorial or read the documentation for more instructions
@eli_paule3804
@eli_paule3804 Год назад
I got it to work, u know a way to make the car go infinitely around a circular track using this ai?
@ahmedsaed26
@ahmedsaed26 Год назад
It should do this if the track is big enough 1. Make sure that the navmesh is setup properly 2. Maybe try to increase the AIFOV variable 3. You can use a custom destination instead of the randomly generated ones You can do this by creating a script that has the waypoints of the custom path and iterate over each waypoint and assign it to the AI and when the AI reaches it move to the next waypoint Hope this helps. Feel free to ask any questions
@cheungjacky6914
@cheungjacky6914 9 месяцев назад
how to make it follow a way path?
@ahmedsaed26
@ahmedsaed26 9 месяцев назад
You can write a script that calculates the distance between the current waypoint and the car and if the distance is short, update the custom destination variable with the new waypoint Please read the documentation for more information
@cheungjacky6914
@cheungjacky6914 9 месяцев назад
@@ahmedsaed26 thank you for your reply, i’ve tried modify the FixedUpdate to update the waypoint but the car just stop at the first destination even move and allowmove are set to true, btw i gave up and wrote another script to achieve it, thank you very much.
@SevastianosKallinteris
@SevastianosKallinteris Год назад
Hello could we make a call in discord in order to set it up correctly, as I set it up as u shown in a new scene but not worked? Thanx in advance.
@ahmedsaed26
@ahmedsaed26 Год назад
Hello, I am sorry, I won't be able to join the call but you can ask me in the comments or in an email. I would be more than happy to help
@SevastianosKallinteris
@SevastianosKallinteris Год назад
@@ahmedsaed26 it finally worked perfectly. i m so thankful to u
@ahmedsaed26
@ahmedsaed26 Год назад
Glad it worked. You are welcome
@charlesferry9465
@charlesferry9465 2 года назад
Where to change speed of the object(Car)?
@ahmedsaed26
@ahmedsaed26 2 года назад
Hi Charles, You can achieve this in different ways: - At line 47, there is a variable called MovementTorque this variable is multiplied by 400 and assigned to the motorTorque for each wheel collider Take a look at the wheel collider documentation for more info about the motorTorque Anyways, if you want to change the speed you can change the value of MovementTorque For example, You can change MovementTorque from 1 to 1.5 To change the motorTorque from 400 to 600 I created this script 2 years ago, and after looking at it now. I realized that I was bad at naming variables I should have named MovementTorque to torqueMultiplier or something similar I hope this answers your question
@aldrinjosephjude5901
@aldrinjosephjude5901 2 года назад
couldn't get the navmesh part right
@ahmedsaed26
@ahmedsaed26 2 года назад
what exactly is the problem?
@leandro0228
@leandro0228 Год назад
my kart dont move to rightt and left
@ahmedsaed26
@ahmedsaed26 Год назад
Can you please provide more info about your setup and what you are trying to achieve? You can also read the rest of the comments for fixes to common issues
Далее
Setup 3D Pathfinding Agents In Minutes Using Unity
7:41
Unity Vehicle Traffic System Tutorial
6:38
Просмотров 23 тыс.
Cole Palmer Revenge vs Man City 🥶
00:20
Просмотров 2,2 млн
Unity + AI: The Game Dev Revolution
13:28
Просмотров 2,3 тыс.
Making a GAME in ONE HOUR using MY ENGINE
25:51
Просмотров 354 тыс.
Create a navmesh at runtime in Unity
7:11
Просмотров 10 тыс.
Dear Game Developers, Stop Messing This Up!
22:19
Просмотров 720 тыс.
Basic Car Movement in Unity
16:04
Просмотров 101 тыс.
The Most Impressive Scratch Projects
11:00
Просмотров 4,9 млн