Simple Enemy AI in Unity (State Machine, Find Target, Chase, Attack)

Поделиться
HTML-код
  • Опубликовано: 27 сен 2024

Комментарии • 391

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  4 года назад +26

    Here's the basic Enemy AI I've used in several of my games, very easy to make and easily expandable to fit any game!
    🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel!
    ✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php

    • @arturkaczan1776
      @arturkaczan1776 4 года назад +2

      I just have bought a Hyper Knights for fun :)

    • @willkeith8699
      @willkeith8699 4 года назад

      Does this work for 3D projects as well?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      @@willkeith8699 Sure, everything is working with Vector3

    • @aceofdiamonds1285
      @aceofdiamonds1285 4 года назад

      Will this work with nav mesh agents attached?

    • @Dailymoresuccess
      @Dailymoresuccess 4 года назад

      Can you show tutorial for pathfindingMovement ?

  • @serialchiller9183
    @serialchiller9183 4 года назад +14

    This intro song has become iconic. Please don't change it even though you change the visuals. X) Keep up the good work!

  • @jvcmarc
    @jvcmarc 4 года назад +58

    hey, can you make a video about how to make updates/patches for a released game?

    • @gouravsoniya2366
      @gouravsoniya2366 7 месяцев назад +1

      ​@Shoo897 she was little straight forward 😂

  • @minibubblegum5108
    @minibubblegum5108 3 года назад +6

    PLEASE BEAWARE for people confused at different codes from 5:53, there has been some slight tweaking in values, so practice with the project files given above.
    I depressed myself for using 1f as reached position distance. if you are deleting the Enemy AI script and creating your own in the project files, see the Enemy AI script in the files first.
    And thank you Code Monkey for showing me a new way to use A star.

  • @electromorphous
    @electromorphous 4 года назад +5

    You're absolutely owning it with these videos. These are really very helpful.

  • @abandonedgoals4067
    @abandonedgoals4067 4 года назад +18

    Just wanted to say, love your channel! :)

  • @alperatmaca6430
    @alperatmaca6430 3 года назад +1

    dude i've been searching non-stop for a tutorial ike this! thanks for the amazing tutorial!

  • @matt10y27
    @matt10y27 4 года назад +8

    This is cool! Such a clean and concise way to implement the state changes with an enum and then a switch case.

  • @Adidaas
    @Adidaas 4 года назад +86

    Hahah, I can't believe it. I always thought that State Machines was gonna be something complicated to learn. But State Machine is literally just switch cases of different enums. That's super straight forward.
    Maybe it's just the name ."State Machine" sounded so intimidating.
    Thanks for the clear and concise tutorial! And especially thanks for not murmuring and for writing correct and clean code to follow!

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +14

      Hehe yup the name State Machine does sound intimidating, I had that exact thought when I first heard the term until I realized that's what I had been doing all along.

    • @anjobihis2052
      @anjobihis2052 4 года назад

      This is so true lol!

    • @ludovicmahieu8278
      @ludovicmahieu8278 4 года назад +2

      The version with abstract state is much more complicated (but powerful) :)

    • @ludicrouS_406
      @ludicrouS_406 3 года назад +3

      State Machine are just facy words for a simple thing that makes people feel smart 😂

    • @asdfghjkl-jk6mu
      @asdfghjkl-jk6mu 7 месяцев назад

      @@ludicrouS_406 what else would you name it to describe it accurately lmao?

  • @ScrubInstitute
    @ScrubInstitute 3 года назад +1

    Why I like code monkey? Because he explain everything in a simple way. He deserves more subscribers. If you want to get more views, I recommend to change the thumbnail style.(just a suggestion)

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      I'm glad you found the videos helpful!

    • @ScrubInstitute
      @ScrubInstitute 3 года назад

      @@CodeMonkeyUnity why don't you make a racing game?

  • @Andrew90046zero
    @Andrew90046zero 4 года назад +3

    I haven't gotten deep into Ai in my game yet, and I was planning on making an entire fully blown State Machine class just to do wandering and movement. But this is exactly what I needed. And When I get around to it I will actually do what you did with the enums.

  • @medmel2160
    @medmel2160 4 года назад +2

    thanks for the quality content as usual. Gratz in advance for the 100k sub ! You deserve it and deserve much more

  • @fardin3m39
    @fardin3m39 4 года назад +3

    its so awesome he popup all previous tutorial that maybe you have question about at the start

  • @Nicoulex
    @Nicoulex 4 года назад +4

    Your channel has insane content ! keep on rockin'

  • @neozoid7009
    @neozoid7009 2 года назад +1

    Awesome Tutorial and enemy system

  • @TheStigChristensen
    @TheStigChristensen Год назад +1

    Would love to see a similar video for a friendly AI! A bot to help in singleplayer, when there's not a second player around

  • @rafaelcorrea7959
    @rafaelcorrea7959 4 года назад +2

    You should publish Code Monkey utilities as a free asset in the unity asset store.

  • @generichuman_
    @generichuman_ 2 года назад

    Damn, I never thought of just having a static instance to use in another script. Avoids making spaghetti with dependencies, super clean!

  • @USBEN.
    @USBEN. 2 года назад +1

    Beautiful.

  • @insaneduane
    @insaneduane 3 года назад +3

    Thank you for this easy to follow and well explained tutorial on making a State Machine/FSM. Many other videos on this subject tend to get convoluted and "lost in the weeds" so to speak. I've used the Mecanim system as a FSM but I've found that to be a bit buggy (OnStateEnter and OnStateExit seems to not always work (probably my fault)) so I used your method and my VR zombies are acting and sounding like I want them to. I liked this video and I am subscribed. Thanks again!

  • @filipgawlik3259
    @filipgawlik3259 4 года назад

    And this time I was not disappointed.

  • @anjobihis2052
    @anjobihis2052 4 года назад +4

    Code Monkey, how did you learn to code like this? Is it because that you code so much and research by self-teaching yourself i.e. reading C# and unity API documents? Learning from others? How did you come up with this advanced yet simplified codes?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +3

      It's really all about experience, I've been writing code for 20 years so in all that time I've written some basic AI implementation dozens of times.
      It really is just trial and error, start trying to do something, think of exactly what I need to accomplish my goal, and try to make it while constantly trying different approaches, reading the manuals and googling for answers.
      So yeah, the more you do it the better you become.
      Best of luck in your own journey!

    • @anjobihis2052
      @anjobihis2052 4 года назад +1

      @@CodeMonkeyUnity Thank you so much, Code Monkey! I appreciate this a lot! And your videos are sooo good!! Keep em coming! :D

  • @rahulkumarjha2404
    @rahulkumarjha2404 4 года назад +1

    Thanks for this awesome tutorials .

  • @Exisss
    @Exisss 4 года назад +1

    Really Helpful, as always :)

  • @II-wu3wz
    @II-wu3wz 4 года назад +3

    Where can I find "EnemyPathfindingMovement" you mentioned? You said you created it in another video, is it A*?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +2

      Yes it's A* ruclips.net/video/alU04hvz6L4/видео.html

    • @II-wu3wz
      @II-wu3wz 4 года назад +5

      ​@@CodeMonkeyUnity There is no EnemyPathfindingMovement, nor a GridPathfinding, I watched every Tutorial for this, it's a bit frustrating. :D
      I imported now parts of your project and adapted it to my project, but I can't access namespace GridPathfinding,
      in EnemyPathfinding it throws me a nullreferenceexception at line 97
      "pathVectorList = GridPathfinding.instance.GetPathRouteWithShortcuts(GetPosition(), targetPosition).pathVectorList;"
      But I have the GridPathfinding in my Project, can you help me?
      I also did a Debug, the reference is just null, the folder for namespace GridPathfinding is in the same folder for me other AI stuff.
      And in which tutorial you created all this stuff? Definitly not in A* :D
      I have to add:
      Reading all the other answers you give here seems like you don't really know the content of your own videos,
      there is no EnemyPathfinding, no GridPathfinding.

    • @vishwahsiva8738
      @vishwahsiva8738 4 года назад

      @@II-wu3wz seams like the codes are in EnemyAI.unity pakage check it out

    • @ceciruiz4262
      @ceciruiz4262 3 года назад

      @@II-wu3wz I have the same error and I haven't found a solution :(

    • @TheKodiakDJ
      @TheKodiakDJ 3 года назад

      I don't know if I'm in time to help anyone out in this thread, but for people looking for those scripts in the future, they are in the subfolder TopDownShooter\Scripts in the EnemyAI unity package.

  • @tostpl8258
    @tostpl8258 4 года назад

    I think it's really good thing to show us your function (like getrandomdirection) . Sorry for my English if I did smth wrong

  • @juicedup14
    @juicedup14 4 года назад +1

    Thanks patrons

  • @lennyseed9255
    @lennyseed9255 3 года назад

    Thank you for this awesome video

  • @glebgrozin6872
    @glebgrozin6872 4 года назад +1

    You're the man. Bless

  • @david-spliso1928
    @david-spliso1928 4 года назад +1

    Thank you CoMo.

  • @Stinger-rq4gy
    @Stinger-rq4gy 3 года назад

    I saw this its great, thank you sooooooooooooooooooooooooooooooooooooooo much.

  • @levanapakidze1606
    @levanapakidze1606 3 года назад +1

    isn't returning a static instance of a player game dependant code ? what if I attach that script to multiple instances ?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Yes, if you have a design that involves the enemy targeting more than just the player you need to modify that.

  • @ezekielakande5501
    @ezekielakande5501 4 года назад

    Finally found someone useful

  • @carrihook1094
    @carrihook1094 Год назад

    When I download the project I get the error LWRP does not exist in the namespace UnityEngine.Experimental.Rendering and The type of namescape name 'Light2D' could not be found

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад

      LWRP has been renamed into the Universal render pipeline, I covered that in the FAQ on the download page, you just need to change the using statement

  • @RawHunterX
    @RawHunterX 4 года назад +2

    Very informative, if youd like to expand upon it in the future, heres a suggestion: Do the New Mario Bros. Goomba behaviour. Basically whats new compared to the old ones is that besides patroling and falling off edges, it siwtches to chase the Player if in sight. However, it also pauses for a second, jumps up a bit, then it charges faster and if you jump over it, it changes the position to face the Player but slides a bit backwards due to the charge momentum.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Yeah doing some Platformer/SideScroller AI is definitely something I want to cover.

  • @rameezsafdar6563
    @rameezsafdar6563 4 года назад

    This is so informative, i wish you had an advanced AI tutorial as well

  • @SolidMotionVFX
    @SolidMotionVFX 3 года назад

    Great video. How do i setup so enemies if close to each other they should avoid on and other.

  • @terry-
    @terry- 8 месяцев назад

    Great!

  • @josueccama303
    @josueccama303 4 года назад +1

    hi, nice video. Please make video about isometric tile map with ECS. thanks

  • @SwampySongs
    @SwampySongs 3 года назад

    Do you recommend navmeshes for things like this?
    The problem I run into is adding complexity/randomness to a navmesh.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Your Movement method is separate from the AI logic, you can use a NavMesh or just an A* Grid or in the video it uses no pathfinding. All of them work just fine.

  • @pythora4173
    @pythora4173 4 года назад +1

    I downloaded the project files and put them in Unity, but it didnt work because there were so many errors. Fix?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      What didn't work?

    • @pythora4173
      @pythora4173 4 года назад

      @@CodeMonkeyUnity I'm not really sure, but the enemy ai script had a lot of error messages

  • @emmesplaylists
    @emmesplaylists Год назад

    Hi, I added the package to my project, but now I have a problem I don't know how to solve (I'm new to Unity): The type or namespace name 'LWRP' does not exist in the namespace 'UnityEngine.Experimental.Rendering' (are you missing an assembly reference?) [Assembly-CSharp]. Does anyone have a tip about what should I do?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад

      The Lightweight Render Pipeline has been since renamed into the Universal render pipeline, now it's under UnityEngine.Rendering.Universal;

  • @EVERYTHING-ey9em
    @EVERYTHING-ey9em 3 года назад

    Just need this in 3d

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      The AI doesn't care what type of perspective you use ruclips.net/video/3zxTigjJr24/видео.html

    • @EVERYTHING-ey9em
      @EVERYTHING-ey9em 3 года назад

      Thanks

  • @jessenance8889
    @jessenance8889 4 года назад

    Wonder if it could be modified to my turn based game. Each move is limited to a certain range.

  • @akashverma4280
    @akashverma4280 4 года назад +1

    Hello, thanks for tutorial! I wanted to ask an unrelated question. Do you think making mobile games as an indie dev would pay off? I'm in last year of graduation of my CS degree. I'm thinking of trying to pursue gamedev full-time.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      Definitely give it a try, especially while you're still in school, but don't risk going at it full-time, do it as a side project along with a stable job.
      It's an extremely competitive industry and very difficult to make a living ruclips.net/video/Vlcop0uxFIQ/видео.html

    • @akashverma4280
      @akashverma4280 4 года назад

      @@CodeMonkeyUnity Thanks for taking time to reply. I have already watched that video cuz I'm already subbed. It had some nice tips. But working with a full time job would definately wear me out. I have plenty of time now, I'm at home due to current pandemic situation, I got no loans to pay and I have no urgent need for money.
      What I'm saying is that, I try gamedev till my graduation and see how it goes. And if it didn't go well I'll say goodbye to it and get a corporate job, but if it did go well (ik chances are very thin) then I consider it doing full-time. Also, where I live, cost of living is less.
      I could really use some guidance right now. Because I don't know any game dev in person. Thanks for reading.

  • @xplo8950
    @xplo8950 3 года назад

    Is there a playlist encompassing all of the tutorials in order

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      Most of the tutorials are standalone, so just browse my video list and look for the topics that interest you

  • @DoraTheExplorerrr
    @DoraTheExplorerrr 4 года назад

    thank you hope its will work

  • @MrFarkasOfficial
    @MrFarkasOfficial 4 года назад +1

    alright, alright, you`ve got my subscription.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Glad you like what you see!

    • @MrFarkasOfficial
      @MrFarkasOfficial 4 года назад

      ​@@CodeMonkeyUnity i`m still a noob though. having trouble with the EnemyPathfindingMovement part of the void awake function. console says the namespace can`t be found. my guess is i need another c sharp script, the EnemyPathfindingMovement script but i don`t know where to find it.

    • @MrFarkasOfficial
      @MrFarkasOfficial 4 года назад

      @@CodeMonkeyUnity nevermind, solved! :D

  • @estusmedia1887
    @estusmedia1887 4 года назад +1

    I've followed this up until the point where you run the first test, I have all of your project files but when I try to run the test with what i have so far I get an issue with Object reference not set to an instance of an object EnemyPathfindingMovement.SetTargetPosition and because of it my Enemy won't move at all, how do I fix? The error only appears on play.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      You have something set to null, add a Debug.Log to figure out what it is

    • @estusmedia1887
      @estusmedia1887 4 года назад

      @@CodeMonkeyUnity I eventually figured out it's tied to the fact I don't have the Aim System because I'm looking to make a Melee character, but it seems in order to use the Pathfinding, you need to use the aim system, which then involves me basically needing to use the entire project for the topdown shooter due to how everything is linked together?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      ​@@estusmedia1887 The Pathfinding does not require any aim system at all, it just has a Grid and calculates a path, it has no concept of Units or Aim or anything, it's a completely independent system. Check out the Grid System videos and the Pathfinding one ruclips.net/p/PLzDRvYVwl53uhO8yhqxcyjDImRjO9W722

    • @estusmedia1887
      @estusmedia1887 4 года назад

      I've done both of those just having difficulties with this one now, I'll come back to it when I have a better understanding and experience and hopefully I'll be able to understand, appreciate all the great videos!

  • @thederbly6009
    @thederbly6009 Год назад

    Is the path finding script required for this to work?

  • @Rabellois
    @Rabellois 4 года назад

    Could you do a tutorial about State Machines without enum? Using different classes for each state

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      I'm not a big fan of inheritance so I normally use simple enum state machines.
      However in Battle Royale Tycoon each Building has its own logic in a separate class and then within that class I have the normal enum state machine. So I might look into making a video on how that works.

  • @sonicherores
    @sonicherores 4 года назад +1

    Can this logic be Used for 3d games :D? thanks for the great tutorials as always your essentials have helped me make stuff so much faster.

    • @caxopog
      @caxopog 4 года назад

      Of course it can. U even can copy this code in 3D game and it may work even without additional code

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Sure, the logic for handling the various states has no 2D or 3D dependency.

    • @sonicherores
      @sonicherores 4 года назад

      @@CodeMonkeyUnity Thank you again I thought as much but had no time to test it due to life up to what point in dev are we allowed to use your essentials incase of release. Nowhere near but goos to ask early thanks again for the wonderfull tutorials!

  • @raposaarvorelua4099
    @raposaarvorelua4099 4 года назад

    thank you for the tag "unity 3d" when its not for 3d

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Everything shown here works exactly the same in 3D, all the functions even use Vector3's
      Go ahead and apply it to your own 3D meshes and everything will work.

  • @bellatorpoeta
    @bellatorpoeta 3 года назад

    How would I apply this state machine to a turn-based system with several enemy units? I would need to keep track of which unit is being controlled as well as have an indicator to not go to a new unit until done, but doesn't break reset the variables?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Each Unit would have their own state machine and then a general Turn System would handle who is active, I covered something like that here ruclips.net/video/0QU0yV0CYT4/видео.html

  • @Sartoris36
    @Sartoris36 4 года назад

    Thank you for this video! Do you have any suggestions on how to make the enemy avoid obstacles without using pathfinding? Perhaps by turning when encountering an obstacle, or something similar? That would be very helpful.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      If you just want avoidance like keep away then just keep a reference to it and test the distance.
      But if you want avoidance in order to reach a target then thats the definition of Pathfinding so I'm not sure what you're exactly trying to do.

    • @Sartoris36
      @Sartoris36 4 года назад

      @@CodeMonkeyUnity Thank you for your reply. I should have been clearer: what I am referring to is a system for the enemy to chase the player or look for him without using A* pathfinding, but by linecasting around him and basically "feeling out" the environment. So for example, when he "feels" or "sees" an obstacle, he turns to go around it. Here is an example of that: twitter.com/i/status/1089989454704582656
      I would love to see you implement something like this :)

  • @adeled8833
    @adeled8833 2 года назад

    Thank you! I have made it looking less frizzy on frames (if there are many obsticles and the path is updated often) by making a square each time im setting a new roaming path around the roaming enemy x+ -5 y+ -5 (which is 11*11 possible values) ,and then checking if there are other enemies or obsticles, then substracting these grids out the 11*11 list, then choosing random one from the clean list

  • @dementovoid2255
    @dementovoid2255 2 года назад

    How can I get EnemyPathfindingMovement to be recognized by Unity? Do I have to follow the A* Pathfinding tutorial before I follow this tutorial?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад

      Yes, or use any other pathfinding system you want

  • @thederpderp7758
    @thederpderp7758 3 года назад

    Thanks!

  • @thomasbottino8858
    @thomasbottino8858 4 года назад

    Hello great tutorials u do big thanks they help Great. I bought some behavior designer and other but it was buggy and not as versatile as learning the basic that u offer us. One question about the patrolling state tho your map is very empty but how to deal with map with wall and obstacle where destination would be in the obstacle or even outside the map.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Add some pathfinding ruclips.net/video/alU04hvz6L4/видео.html

  • @ChadRepson
    @ChadRepson Год назад

    Thanks code monkey!

  • @zarceh
    @zarceh 4 года назад +1

    how do i make bots attack each other?
    like in an chaotic battle royalle

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      What do you mean by attack each other? Attack the closest? If so just go through a list of all units and find the closest.
      You need to define more detail on exactly what type of behaviour you're trying to create.

    • @zarceh
      @zarceh 4 года назад

      Code Monkey i was thinking of doing an simple AI that would attack at a certain range, and would follow the clossest AI... like hunting it down, if they get really close (almost touching the other AI) the bot would start attacking.

  • @lucamassari6708
    @lucamassari6708 Год назад

    Can you use the same script applied to different enemies that don't trigger the chasing together?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад

      What do you mean "trigger the chasing together"? Each script instance is separate, if you have 3 enemies and one starts chasing the player it won't affect the others, if the others are far from the player they will stay in their patrol state.

  • @RatonBeton
    @RatonBeton 10 месяцев назад

    Can you compare a vector whit a regular number?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  10 месяцев назад +1

      No, a Vector3 is composed of XYZ which are 3 numbers. You can't compare 3 numbers to one.
      What are you trying to do?

    • @RatonBeton
      @RatonBeton 10 месяцев назад

      @@CodeMonkeyUnity i'm trying to check if the position betwen a enemy and a checkpoint is less then 0.5

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  10 месяцев назад

      Then you want to use Vector3.Distance(); to get the distance between two points
      Or vector3.magnitude to get the size of a vector

    • @RatonBeton
      @RatonBeton 10 месяцев назад

      @@CodeMonkeyUnity ty

  • @Krebzonide
    @Krebzonide 4 года назад

    I would like to make 2 ai attack each other. Is there a way to detect only the closest enemy so I don't have to run a large for loop for each unit?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      If your enemies have Colliders you can use Pysics2D.OverlapCircle(); to locate all colliders near a certain position

  • @Roadis
    @Roadis 4 года назад

    What is the best way to do this with ECS? I was thinking about adding components or deleting them when they should do other tasks. But how is the Performance for that?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Yeah I guess either adding/removing components or just a component with a State enum. Multiple components might be better since you can store state specific data.
      Adding/Removing components is extremely fast so unless you're changing state hundreds of times every frame it shouldn't be an issue.

  • @evggg
    @evggg 4 года назад

    Hey! You are not using ShootingTarget state. All shooting locates in ChaseTarget state.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Yes, that state is meant to be a dummy state while the animation plays.

  • @Hypocat
    @Hypocat 4 года назад

    Hey, I hope this is not stupid question but where is the video where you do the EnemyPathFindingMovement? I can't find it.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +3

      Here ruclips.net/video/alU04hvz6L4/видео.html

    • @Hypocat
      @Hypocat 4 года назад

      @@CodeMonkeyUnity thank you very much.

  • @qtpigames
    @qtpigames 4 года назад

    How to make AI help player kill enemy?

  • @Irategamefan
    @Irategamefan 2 года назад

    I'm just wondering but could this work in a 3D space? By Space I mean Space like a flying sim?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад +1

      Sure, the logic is based on Vectors, it doesn't care if those vectors have a y of 0 or not

  • @IrvinMenezes
    @IrvinMenezes 4 года назад

    I'm still lost, how does the float work as a measure of distance?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Do you mean the float reachedPositionDistance? It's just a value based on the unit scale of your objects.
      In my case, my characters are about 6 units tall so I set the value accordingly. If your characters are only something like 1 unit tall then change the value.

  • @caprariualex2100
    @caprariualex2100 4 года назад +1

    Your EnemypathfindingMovement script please?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Here ruclips.net/video/alU04hvz6L4/видео.html

    • @nautics9927
      @nautics9927 4 года назад +1

      @@CodeMonkeyUnity i downloaded it but i can only find scripts called pathfinding, no enemypathfinding

  • @sife-i9n
    @sife-i9n 3 года назад

    does this work for multiple agents ???

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      Sure, each agent can have its own AI

    • @sife-i9n
      @sife-i9n 3 года назад

      @@CodeMonkeyUnity i dont know why but if i create a new object and add a nav mesh agent component both of them stop if i removed it the other one works fine??????? any suggestions ?

  • @Rejnols
    @Rejnols 4 года назад

    When i open the project in Unity i get a bunch of compile errors. Any idea why?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      What compile errors? You're probably missing some packages

    • @Rejnols
      @Rejnols 4 года назад

      @@CodeMonkeyUnity A bunch of different ones. Just closed down the project and started working on something else. I downloaded the package files from your website, is there others i need to get somewhere?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      @@Rejnols The project files contain all the code and assets but you still need to set up your project. If you see errors related to lights then you probably need to open the Package Manager and install the Universal RP

  • @nightyonetwothree
    @nightyonetwothree 4 года назад

    i miss how returning from shooting state works. I mean i see current code, but cant understand the mechanism

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      The animation system receives a callback function which gets called when the animation completes.
      So he goes into the ShootingTarget state and when the animation completes it runs that callback which resets the state.

  • @BlastGamer77
    @BlastGamer77 Год назад

    The roaming code given in the first part of this video doesn't work. The EnemyPathfindingMovement script will always result in a NullReferenceException on the "pathVectorList = GridPathfinding.instance.GetPathRouteWithShortcuts(GetPosition(), targetPosition).pathVectorList;" line, even if the code is copied one to one from what you have in this video and even if the pathVectorList is made beforehand (As to not make it null). The error only happens in play mode but it prevents the whole roaming script from working properly.

    • @concov3000
      @concov3000 Год назад

      I'm getting the same issue when it come to the EnemyPathfindingMovement. Visual Studios tells me that the namespace couldn't be found :( Any fix to this issue?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад

      Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
      Maybe you don't have the GridPathfinding set up, maybe your unit is off-grid, maybe theres no path to that point

    • @BlastGamer77
      @BlastGamer77 Год назад

      @@CodeMonkeyUnity I did. I tested all three parts and the one that returned a NullReferenceException is the GridPathfinding.instance.GetPathRouteWithShortcuts(GetPosition(), targetPosition) part. But I have GridPathfinding setup. In fact I got it directly from the files given for this lesson on your website.

    • @BlastGamer77
      @BlastGamer77 Год назад

      @@CodeMonkeyUnity Are you going to answer my comment? Either your code doesn't work, or you failed to include critical information in your own tutorial.

    • @BlastGamer77
      @BlastGamer77 Год назад

      @@CodeMonkeyUnity Ok I have tried literally everything; your code does not work. I fixed my initial issue but now the object refuses to move even when using your exact code. The export on your website does not work; there's no scene and no way to test it. This entire tutorial is useless.

  • @artemiymak8332
    @artemiymak8332 3 года назад

    Hello, where I can look "previos video" with pathfinding system?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      It's here ruclips.net/video/alU04hvz6L4/видео.html

    • @frazoni.
      @frazoni. 3 года назад

      @@CodeMonkeyUnity I was just about to ask the same, thanks for still answering questions even though the video is a bit "old". Cheers :)

  • @anonymous-oq4fv
    @anonymous-oq4fv 4 года назад

    hey code monkey i love ur videos but i had an issue and it is that when i put "private EnemyPathfindingMovement pathfindingMovement;" in the console puts me "The type or namespace name 'EnemyPathfindingMovement' could not be found" do you think you can help me with that i appreciate it

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      You dont have any class in your project named EnemyPathfindingMovement

  • @DoguethekillerYT
    @DoguethekillerYT 4 года назад

    Does it work for 3d games or in 3d projects?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Sure, there's nothing here limited to any perspective ruclips.net/video/3zxTigjJr24/видео.html

  • @billjohnson9880
    @billjohnson9880 4 года назад

    If I make commercial game someday, can I use code samples or utility codes in Code Monkey?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Yup! Feel free to use anything you learn from these videos

  • @Chrischek89
    @Chrischek89 4 года назад

    Invalid Video ID on Project files link. Did you switch the location?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      Seems I put the wrong link, here it is: unitycodemonkey.com/video.php?v=db0KWYaWfeM
      Thanks!

  • @chrisfarlee6496
    @chrisfarlee6496 2 года назад

    So one thing i was wondering, is it better to use A* for path finding or a 2d NaveMesh?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад +2

      A* is the algorithm, NavMesh uses A* to calculate the path. Do you mean Grid vs NavMesh? That depends on the structure of your levels

    • @chrisfarlee6496
      @chrisfarlee6496 2 года назад

      @@CodeMonkeyUnity Good to know thank you! and i guess i did mean Grid vs mesh, sorry! i've only been working with unity for around 8 months and am still learning by day! I found a 2DNavMesh on Github that allows you to use unity's built in NavMeshAgent component directly and i've been using that for top down enemies, and i was just wondering if that's the most optimal way to implement it i guess!

  • @ejazsafi4759
    @ejazsafi4759 4 года назад

    HEY! i made a minion with switch statement and nav mesh agent.
    and first case is destroy turret
    and after destory base. BUT when turret is destoryed it says "The object of type 'GameObject' has been destroyed but you are still trying to access it.". help me fix it
    how can i say ? if turret is destroyed switch case..?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      What are you trying to do? Destroying the game object should be the last thing you do, first handle everything you need to do related to destroying the tower and only after everything is done do you destroy the game object

    • @ejazsafi4759
      @ejazsafi4759 4 года назад

      @@CodeMonkeyUnity i fixed it tnx! it was just basic syntax error... :)! but thank you!

  • @macadenmiles1333
    @macadenmiles1333 4 года назад

    Im confused, Where is the enemyPathfinding script that you made in a previous video? this is the most confusing part of the entire video

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      You can implement the Pathfinding in any way you want. Your AI should not depend on a specific Pathfinding implementation, it should be completely separate.
      I covered Pathfinding here ruclips.net/video/alU04hvz6L4/видео.html

  • @douwemeijer6556
    @douwemeijer6556 4 года назад

    Hi I was just wondering if u could use the same code In a 3D game. Of course with the changes of the vector positions

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Sure, there's nothing here that is unique to 2D, everything already works with Vector3s ruclips.net/video/3zxTigjJr24/видео.html

  • @produccionesr7168
    @produccionesr7168 4 года назад

    I would like to make a metal gear game, do you think this can be of use to me for that?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Sure, all AI is just expanding upon this same concept.

  • @hoodini_gamer8769
    @hoodini_gamer8769 4 года назад +1

    can you make a battle royale

  • @TheTrolderia
    @TheTrolderia 3 года назад

    In a 2d top down game, how do i make my AI walk random between 1-5 sec and then stop for 1-3 sec, and then walk again? :)

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Add a simple timer, when you reach the position change the state to start counting the timer, when timer is done go back into the patrol state and back into the next position.

    • @TheTrolderia
      @TheTrolderia 3 года назад

      ​@@CodeMonkeyUnity Thanks for quick respons - But what kind of code do i need? It's a random position, so he dosen't reach it?
      Is it like when he walked beetween 1-5 -X or -Y pos, change pos?

  • @marioshusband3700
    @marioshusband3700 4 года назад

    when i download the thing I needed to download it says i need to an associate an app with it, a little help?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      You mean the .unitypackage file? Just open Unity and drag the file onto the Project window

    • @marioshusband3700
      @marioshusband3700 4 года назад

      Code Monkey yes I mean that, thank you!

    • @marioshusband3700
      @marioshusband3700 4 года назад

      Code Monkey and also can you make a Pastebin of the pathfinding script we need to do this and send the link of it to me?

  • @belddap8909
    @belddap8909 3 года назад

    Can I use machine learning for AIs?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +2

      Sure! ruclips.net/video/zPFU30tbyKs/видео.html

  • @rahulkumarjha2404
    @rahulkumarjha2404 4 года назад

    Does the codemonkey.utils pack gets automatically downloaded when downloading the entire project file or we have to download it separately . And do we also need to install it ?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад +1

      I normally include it in the project files so just by importing them into a new project and hitting play everything works.
      There's no need to "install", just import them into your Unity project which will copy all the source files.

    • @rahulkumarjha2404
      @rahulkumarjha2404 4 года назад

      Thank You So Much!!

  • @sirspectre3485
    @sirspectre3485 3 года назад

    Can you use this method for 3d enemies?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      Sure, the AI doesn't care how the camera is set up or if the visual is a mesh or a sprite

  • @jonathanmcginley6835
    @jonathanmcginley6835 3 года назад

    Where can I find the Enemy pathfinding movement script?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      I covered pathfinding here ruclips.net/video/alU04hvz6L4/видео.html

    • @BlastGamer77
      @BlastGamer77 Год назад

      @@CodeMonkeyUnity No you didn't. That video has nothing called EnemyPathfinding in it. Don't say something is in a video that isn't actually there.

  • @TheTekkenNetwork
    @TheTekkenNetwork 4 года назад

    Hey Code Monkey Where is Your Path Finding video ink please provide it

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Here ruclips.net/video/alU04hvz6L4/видео.html

  • @kaushikimudgal4640
    @kaushikimudgal4640 4 года назад

    This isn't actually AI though is it? Just clever code..I am new to this so need to clarify

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Clever code is AI. Are you thinking of Machine Learning? That is a type of AI.

  • @chakibchemso
    @chakibchemso 3 года назад

    nice! But not scalable you probably should implement the state pattern by having an interface abstraction and many concrete strategies so you just switch
    This allows for better scalability and allows States to include change trigger code inside of them
    Fun fact r* used this pattern in all their gtas
    They call them tasks and there's thousands of them
    Like Ped_Complex_EnterCar and Cop_Simple_Arrest
    As simple as this they made open world games with crazy big amount of tasks peds can can follow + they use some sort of queue to sequence their behaviour very useful in cutscenes

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      As always you must choose the approach that fits your design, this simple approach works great for simple scenarios, for something much more complex yes you should go with a more scalable approach.

  • @beginnereasy
    @beginnereasy 2 года назад

    What's the difference between public and private?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад

      Public can be accessed from anywhere, private only from inside that class ruclips.net/video/pD27YuJG3L8/видео.html

  • @bogdanmilicevic7291
    @bogdanmilicevic7291 4 года назад

    Is this working for 3d games?

  • @ahsanuirham
    @ahsanuirham 2 года назад +1

    Hey Brother, How if I used your script, to make a 2D Game Platformer, which means is like a metroidvania Games, which that games, moved horizontally, and vertically, basically? How is it? Your Script, is possible, to used in a 2D platformers? Please answer me..... Like I explained in that above my messages..... Please answer me Brother?.....

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад

      The only difference would be pathfinding since you'd need to make jump/drop links.
      For the movement it's all the same, you just use the AI to calculate the move direction and move the character, just like in a top down game ruclips.net/video/ptvK4Fp5vRY/видео.html

  • @devonchapman4927
    @devonchapman4927 4 года назад

    Would this work for 3d games?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 года назад

      Sure, the AI logic is the same, you just need to use a different movement script and perhaps also add a raycast when targeting to make sure the target isn't blocked by elevation

  • @Charlie-ei4wh
    @Charlie-ei4wh 3 года назад

    "get the utils" "go to the previous video to write the code".......

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      That's how the learning process works, you learn how to build something then you build some more on top of it and so on. There's no magic involved in programming, all code needs to be written at some point.

    • @Charlie-ei4wh
      @Charlie-ei4wh 3 года назад

      @@CodeMonkeyUnity I understand that but when I have to be directed to another video before I’ve even finished the first one it just gets quite irritating
      But you’re content is still great! no doubt about that

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      ​@@Charlie-ei4wh Sadly the only other alternative would be to make every video 5 hours long to build every single system from scratch, that wouldn't really work.
      It does work in my courses though, in there I can start completely from scratch and go through the whole 20 hour course step by step.