Simple 2D Enemy Patrolling Unity tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 พ.ย. 2024

ความคิดเห็น • 99

  • @morebblakeyyy
    @morebblakeyyy  ปีที่แล้ว +9

    The scripts from this video are available here:
    www.patreon.com/posts/2d-enemy-patrol-78358557

    • @zoroff5675
      @zoroff5675 ปีที่แล้ว

      sorry but the web page noy openning with me

    • @mariadelmar7469
      @mariadelmar7469 ปีที่แล้ว

      what is vector2 point for? why dont you use time.deltatime

    • @thediamondleaf5111
      @thediamondleaf5111 10 หลายเดือนก่อน

      Sorry I'm just a student, no $ yet. :((((
      Can't purchase your code :((((

  • @quentinmashitah
    @quentinmashitah 21 วันที่ผ่านมา

    Thank you so much!
    And to people who struggled to make it stop at Point A, try this
    if(Vector2.Distance(transform.position, currentPoint.position)

  • @guynamedbob4641
    @guynamedbob4641 ปีที่แล้ว +8

    I was planning on making enemies for my first game very soon, this came at the perfect time thank you!❤

  • @madretzlpiano4817
    @madretzlpiano4817 9 หลายเดือนก่อน +2

    absolutely great video! I just recently started leaning how to program (it was actually last weekend) and I must say: you explained everything very good and made it simple, so that even a beginner like me could easily understand it!

  • @bblakeyyy
    @bblakeyyy ปีที่แล้ว +20

    For those of you having issues, try increasing the float in lines 35 and 40 to a larger number, you may just not be getting close enough to each point. You can control this with a float in your inspector for quick testing :)

    • @morebblakeyyy
      @morebblakeyyy  ปีที่แล้ว +9

      Wow what a smart man

    • @Vexed-dev
      @Vexed-dev ปีที่แล้ว +1

      what number?

    • @AlbertoBerito
      @AlbertoBerito ปีที่แล้ว

      dont work :(

    • @robertyocum7200
      @robertyocum7200 ปีที่แล้ว +1

      @@AlbertoBerito it did for me. I made it 5f to start to make damn sure it worked, and when it did I lowered it incrementally until I was happy with it. So either you made a mistake or you didnt make it a big enough number to actually make contact with your object.

    • @AlbertoBerito
      @AlbertoBerito ปีที่แล้ว

      ​@@robertyocum7200100f and dont work

  • @Haffey.z64
    @Haffey.z64 ปีที่แล้ว +5

    thank you so much for this script. I modified it a lot, so when the enemy touches the player, it plays an attack animation that kills the player if the sword touches him, and after the attack, the enemy starts walking again.

  • @pureegaming1604
    @pureegaming1604 7 หลายเดือนก่อน +1

    thanks! this helped me do something i have wanted to do for a long time!

  • @skyr333
    @skyr333 ปีที่แล้ว +2

    remember to use composite collider if u are using a tilemap

    • @boutroschaaya5925
      @boutroschaaya5925 11 หลายเดือนก่อน

      Why , what does the composite collider do?

  • @WillowCreekGamesLLC
    @WillowCreekGamesLLC ปีที่แล้ว +2

    This video was excellent. Thank you!

  • @sayhan351
    @sayhan351 ปีที่แล้ว +1

    Hi thank you so much for the help it worked for my game! I would love to see a video where you explain how to make the enemies idle for some seconds then move again though if it's possible sometime! Thanks for this brilliant video tutorial!

    • @sayhan351
      @sayhan351 10 หลายเดือนก่อน

      Nevermind, i managed to make the enemies idle, everything has worked excellent! :D

  • @ChrismisHere
    @ChrismisHere ปีที่แล้ว +1

    Yesh! it worked, thank you so much for the tutorial :DD

  • @alexl.8441
    @alexl.8441 หลายเดือนก่อน

    Great video, thank you!

  • @ilya_rusin
    @ilya_rusin ปีที่แล้ว +7

    What do I do if I want to add 2 and more patrolling enemies? Code works only for one enemy. Do I need to add a different script for each new enemy?

    • @calfriedman6981
      @calfriedman6981 6 หลายเดือนก่อน

      I think you can just make 2 new points, although it would be nice if the whole thing could be packaged into a prefab.

    • @julianblok6358
      @julianblok6358 2 หลายเดือนก่อน

      try same script but add new points with new names in script and gameobject

  • @firewarrior8298
    @firewarrior8298 8 หลายเดือนก่อน +1

    I copied this code minus the animation related things on unity, and all it does is make my enemy move until it hits a 2D box collider. Are the anim codes necessary even if you don't have animation frames yet?

  • @hanpolo2727
    @hanpolo2727 ปีที่แล้ว +7

    To those who want to add a flip onto their sprite, add this bit of code to your first "If" and "Else" statement.
    transform.localScale = new Vector3(-1, 1, 1);
    transform.localScale = new Vector3(1, 1, 1);
    Put it above your "rb.Velocity" code. If your sprite goes backwards then just flip the -1 to the other statement

    • @ezzir7003
      @ezzir7003 ปีที่แล้ว

      thanks bro that was helpful

    • @SahanAbid
      @SahanAbid ปีที่แล้ว

      Oke this help tnx

    • @lokilaufeyson9433
      @lokilaufeyson9433 ปีที่แล้ว

      no man if it does not work properly for people whose scale is not 1 it makes the sprites smaller
      instead we can use eulers angle to flip the sprite

  • @mohsen6890
    @mohsen6890 6 หลายเดือนก่อน +1

    hey, the code works but I have the following Issue: If I set let's say the enemy as a parent Object and move the points under it, the code stops working and it just moves past the points

    • @mohsen6890
      @mohsen6890 6 หลายเดือนก่อน

      I just realized my mistake...

  • @gzerem
    @gzerem 11 หลายเดือนก่อน

    Thanks a lot man, you really saved my timed Project, lots of love

  • @valonyaver600
    @valonyaver600 ปีที่แล้ว +2

    for some reason the if (currentpoint == point.transform) is not working correctly at all. I fixed this by making the speed switch direction in the second set of ifs (the ones that check the distance) and always have the velocity at speed.

  • @blackmd4297
    @blackmd4297 ปีที่แล้ว +4

    The caracter doesn't stop at point B and I don't know why because I did everything coorectly. Can someone help me pls ?

    • @letthatbe
      @letthatbe ปีที่แล้ว +5

      It maybe because of distance between enemy and the point.( transform.position, _targetPoint.position) < .5f
      Instead of

    • @morebblakeyyy
      @morebblakeyyy  ปีที่แล้ว

      This. Many people are having this issue and this is most likely why, fixed it for many in the discord

  • @SixFlickz
    @SixFlickz 11 หลายเดือนก่อน +1

    ia nayone could help i got to the stage before flipping the enemy but my character still walks off the edge any fixes?

  • @Soundy777
    @Soundy777 ปีที่แล้ว +1

    Brill bit of info!

  • @LucasOliveira-on7pn
    @LucasOliveira-on7pn 8 หลายเดือนก่อน

    Thanks for the tutorial

  • @skiwi_i
    @skiwi_i ปีที่แล้ว +1

    Why does mine randomly stop after turning? After if give it a push with my player it starts moving again tho

  • @xonestly1123
    @xonestly1123 10 หลายเดือนก่อน

    Very good tutorial! works good, i wonder if there is a way to make it so that the enemy can jump up a little step if needed so it can patrol larger area

  • @3select
    @3select ปีที่แล้ว +1

    My player randomly stops at random points how do i fix this?

  • @sgtghost17
    @sgtghost17 ปีที่แล้ว

    Super helpful, thanks!

  • @HUSEYINBERIK
    @HUSEYINBERIK 10 หลายเดือนก่อน

    thanks for this video @morebblakeyyy very helpful. Im making 2d shooter side scrolling game like the robocop game back in the 90s where I need to shoot in 8 directions is it possible for you to do a tutorial? for example if I want the character to shoot 45 degrees in a diagonal where i have to push down W & D and shoot button to fire that direction

  • @S3L0Licant
    @S3L0Licant 6 หลายเดือนก่อน

    Life saver, I was doing it by have two trigger blocks at the end and when it enters the trigger block it reverses the direction

  • @mariadelmar7469
    @mariadelmar7469 ปีที่แล้ว +2

    what is Vector2 point for?

  • @Patukakkonen
    @Patukakkonen 2 หลายเดือนก่อน

    If i set the collider as "is trigger" it starts going throught walls and misfunctioning

  • @G1MM1CKTUNES
    @G1MM1CKTUNES ปีที่แล้ว +1

    4:42 saving this for later, I gotta go to bed.
    7:06 saving this for later, just got a call slip :/

    • @nevergonnagiveyouup1669
      @nevergonnagiveyouup1669 หลายเดือนก่อน

      Did you finish the video now

    • @G1MM1CKTUNES
      @G1MM1CKTUNES หลายเดือนก่อน +1

      @nevergonnagiveyouup1669 Yeah, didn't help much in the long run since unity being a nightmare to work with pushed me away from coding. I've just been doing music for games and stuff now. I find it to be a lot more fun than coding and from how the folks I'm working with have reacted to my music I'm definitely better at it than coding, and improving my style.

  • @matanlitmanovich2448
    @matanlitmanovich2448 ปีที่แล้ว

    Thanks brother, awesome tutorial! one question tho, where did u use the V2 point u created in line 25? maybe i missed it...

  • @ashitanojoefan98
    @ashitanojoefan98 ปีที่แล้ว

    hey im having a bit of trouble with my enemy for some reason he instead of moving in a platform he just teleports to a random place, got any idea why?

  • @chakceebor7755
    @chakceebor7755 10 หลายเดือนก่อน

    how to do the sudden freeze and idle to look at the player( turning to us) i do 2d games but in 3rd person view. Please help me.

  • @daddymtgtonipp7741
    @daddymtgtonipp7741 ปีที่แล้ว

    Thank you sir!🥰

  • @thediamondgauntlet8888
    @thediamondgauntlet8888 3 หลายเดือนก่อน

    what if there's point C?

  • @swolfix3866
    @swolfix3866 ปีที่แล้ว +17

    doesn't work .-.

  • @amritbhandari2411
    @amritbhandari2411 11 หลายเดือนก่อน

    okay we made enemy but what will it do to the player?

  • @rragy1848
    @rragy1848 ปีที่แล้ว +2

    It doesnt work for me neither. I did exactly like in video, and the enemy just goes right direction continuosly without stopping

    • @morebblakeyyy
      @morebblakeyyy  ปีที่แล้ว

      If you want to join the discord we can help diagnose what you’re missing

    • @lightofficial7321
      @lightofficial7321 ปีที่แล้ว

      yeah same man

    • @burhvfx6664
      @burhvfx6664 9 หลายเดือนก่อน

      same

    • @madretzlpiano4817
      @madretzlpiano4817 9 หลายเดือนก่อน +1

      I know I'm a bit late but I might know a solution (atleast it worked for me)
      I had the same problem, I changed the z to 0 (by PointA and PointB) and since that wasn't enough, I also changed the hight of those two points. If your points are lower or much higher than your object, that you wanna move, then it won't function right.
      So If someone also had this problem: This might be a solution!
      Update: I also had the problem, later on, that my mob would face e.g. right but would go left. As a quick fix: You can just say in your enemyPatrol script under
      void Start()
      {
      flip();
      }
      works fine!

  • @krus4
    @krus4 ปีที่แล้ว

    I have a problem when I clone the enemy the ai doesn't work and when I add new points to it it still doesn't

  • @mariadelmar7469
    @mariadelmar7469 ปีที่แล้ว +2

    que hace el Vector2 point?

  • @Average-Abnormality
    @Average-Abnormality 8 หลายเดือนก่อน

    Hey, I am making a game with a modified version of the code provided in the video. However, I wanted to ensure that I'm using the script in accordance with your terms and conditions. Could you please provide clarification on whether I need to purchase a Patreon subscription to use the script commercially? Additionally, are there any specific attribution requirements I should adhere to?
    I deeply respect your work and want to ensure that I'm following the appropriate guidelines for its usage. Any guidance or clarification you could provide would be greatly appreciated. :)

    • @morebblakeyyy
      @morebblakeyyy  8 หลายเดือนก่อน

      Hey, nope you’re welcome to use the script wherever you please

    • @Average-Abnormality
      @Average-Abnormality 8 หลายเดือนก่อน

      Thanks, appreciate it:)@@morebblakeyyy

  • @CrumblingCrumbler
    @CrumblingCrumbler 3 หลายเดือนก่อน

    Once I flipped it it broke the whole code send help

  • @tjoel2020
    @tjoel2020 ปีที่แล้ว +1

    I wonder why it doesn't work on mine

  • @reyanahtesham1272
    @reyanahtesham1272 ปีที่แล้ว

    Amazing video

  • @MeninoTedioso
    @MeninoTedioso ปีที่แล้ว

    thanks so much!!!

  • @baconbrotha4606
    @baconbrotha4606 ปีที่แล้ว

    Ummmmm for me this does not wanna work for me even with all of the code in it

  • @issaduck9065
    @issaduck9065 ปีที่แล้ว

    Thanks a lot for making this! This video really helped, but I have a one issue- my enemy isn’t going along the straight line, it’s y is changing. For some reason when it patrols vertical (I modified the code), it doesn’t drift like this, so I don’t know what’s happening.

    • @eizenfinch2196
      @eizenfinch2196 10 หลายเดือนก่อน

      in your rigidbody2D you can click the y box on the constraints tab. it wont do that weird zig zag motion anymore :3 👍

  • @danseptemStudios
    @danseptemStudios 3 วันที่ผ่านมา

  • @danideco8326
    @danideco8326 2 หลายเดือนก่อน

    IMPORTANT: THE SHPERES NEED TO BE TOUCHING THE GROUND FOR IT TO WORK, I LOST 4 HOURS OF MY LIFE BECAUSE OF THIS, HOPE IT HELPS!!!!!!

  • @mariadelmar7469
    @mariadelmar7469 ปีที่แล้ว

    el personaje se mueve hacia la izquierda unicamente, hace un flip en el punto A y sigue asi hasta que choca con un collider

  • @SteelOcean6
    @SteelOcean6 ปีที่แล้ว

    when i add the patrol script to my enemy it doesn't let me choose point a, b, or the speed how do i fix this?

    • @Abiodapro
      @Abiodapro ปีที่แล้ว

      Did you set the variables to public?

    • @SteelOcean6
      @SteelOcean6 ปีที่แล้ว +1

      @@Abiodapro that fixed it, thanks!

    • @Abiodapro
      @Abiodapro ปีที่แล้ว

      @@SteelOcean6 No problem :)

  • @TheDumbDonkey
    @TheDumbDonkey 11 หลายเดือนก่อน

    Guys one of the reasons mine didnt work at the start was cuz my z position for my character was set to -279 and my points were at something else lol. make them all 0

  • @ZiyadAlhussayen
    @ZiyadAlhussayen 11 หลายเดือนก่อน

    didn't work for me
    Update: it did work!!!

  • @FaKz92
    @FaKz92 8 หลายเดือนก่อน +1

    good for less enemies, for many enemies, there are much more efficient ways like this for excample:
    private void FixedUpdate()
    {
    isPlat = Physics2D.OverlapCircle(fallCheck.position, 0.2f, 1

  • @LUFFY-bg8vy
    @LUFFY-bg8vy ปีที่แล้ว

    Thx

  • @fiddledydoodle
    @fiddledydoodle 8 หลายเดือนก่อน

    Idk how you all got it to work, shit didn’t for me
    Ended up just using timers instead lol

  • @jeffersonmarvel5189
    @jeffersonmarvel5189 5 หลายเดือนก่อน

    makasih bang

  • @DawidLewandowski-ev7rd
    @DawidLewandowski-ev7rd ปีที่แล้ว

    nice ONE PIECE of code bro , thx for help

  • @Vokoyt
    @Vokoyt ปีที่แล้ว +4

    firssstttt❤❤

  • @harrisonhorton67
    @harrisonhorton67 5 หลายเดือนก่อน +1

    Don't waste your time! This doesn't work

  • @AverageNerdYT
    @AverageNerdYT ปีที่แล้ว

    When I make it a asset and start puting it inn the project the points don't work