3. Enemy Patrol Routes--Just Add Enemies! (Unity Tutorial)

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

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

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

    Thank you so much for explaining how Vector2 works. I've used other's scripts with it before, but never understood what it meant.

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

      Awesome! I love hearing when one of these videos is helpful in that way. Thanks for sharing.

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

    Thank you so much for your videos ... So far i am excited af to watch them and learn to build a 2D game demo..!!!
    I made a parent gameobject for the patrol points and added the script to the child enemy to make it work.

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

      We’ll done! I wish you the best with your demo.

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

    So well explained, my brain is so messed up I can't handle 90% of the tutorials out there. The way you explain it is perfect. Glad I found this channel. Looking forward to seeing what else I can learn. I'm currently doing Brackey's Gam Jam, and making my first game! Trying to do as much as possible to practice.

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

      That's awesome! Game Jams are one of the best ways to learn. When I got started, I found that repetition was really important, so mini projects are great as you get to re-use a lot of the same code elements. Good luck with the Jam!

  • @JohnDoe-il2pg
    @JohnDoe-il2pg ปีที่แล้ว +1

    Thanks for such an easy-to-understand Tutorial !

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

      You bet! Glad to hear it was useful.

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

    Hi, thanks for uploading such helpful videos, I just want to ask once you in the enemies sight and they start to follow you, will they follow you forever or is there a distance where they will go back to their route?

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

      At the moment, the enemy will not go back. That said, in the Update method, under your if statement for isChasing, you could add a line of code that checks when the enemy is outside your chase radius (the same line of code you use to start the chasing, but with a > sign instead of a

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

      That's works perfect.....you should make this entire series into a Udemy course, I paid and done a two different courses and yours was x10 better. You really explain very well.

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

      Thanks; that’s really good to hear. I’ll have to check out the udemy idea, too!

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

    I've got a few problems in it,
    first one is the enemy which is continuously jumping to the end of point 0, and the second is it is not coming to left side and staying at right end point by continuously hopping.

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

      If it is hopping, that almost always means it cannot reach the patrol point. Try moving the patrol up or down a little to make sure it came be reached. That usually resolves this problem.

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

    Hi, my enemy is having a problem where he moves to one of the points and just stops there, did i mess up the code?
    public class MovimentoInimigo : MonoBehaviour
    {
    public Transform[] patrolPoints;
    public float moveSpeed;
    public int patrolDestination;

    // Update is called once per frame
    void Update()
    {
    if(patrolDestination == 0)
    {

    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f)
    {
    patrolDestination = 1;
    }
    if(patrolDestination == 1)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[1].position) < .2f)
    {
    patrolDestination = 0;
    }
    }
    }
    }
    }

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

      Usually that means that the enemy is not able to get close enough to the patrol point to trigger a turnaround. While playing, try switching to scene view and move the patrol point up or down a little. Chances are a little adjustment should get everything working.

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

      @@NightRunStudio He still freezes when he reaches the first point, tried moving it to every possible point so the enemy moves but still no success

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

      @@migs_2559 Alright, well that was worth a try (it's usually the thing that gets people). Taking a look at your code, I think the issue may be some misplaced curly brackets. At the moment, once you switch your destination, you are missing a closed bracket. So now, it will only check if you have patrol destination #2 IF you have patrol destination set to 1 (which will never happen).
      While you are adding the fix, it is often helpful to include some debug statements. For example, if you add some Debug.Log("reached destination 0"); at the point where you detect destination 0, you can find out which parts of your code are firing and which parts are failing to be exectued.
      I hope that helps!

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

      @@NightRunStudio you were correct! I missed a curly bracket, thank you very much!
      I'm doing my graduation project based on a 2d game and this helped me a lot on the enemies i'm putting in

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

      So glad to hear it was helpful! Good luck with the project.

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

    este canal debe tener mas vistas

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

      Thanks! I’d sure like that, too!

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

    Everything went smoothly when im using this code. its just that, it scales my mushroom to a bigger size, not just flip, I dont know why.

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

      If you are flipping using localScale, you need to use the negative version of whatever you set your scale to. So if you have changed the scale of the object to something other than 1, -1 wont work for flipping. In that case, you could just multiple it by -1 (so it will just become the negative version of itself), or hardcode in the negative version of whatever your scale is set to. Hope that helps!

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

      @@NightRunStudio Didnt expect youd reply but man Thank you for the info

  • @randomperson.0-0
    @randomperson.0-0 2 ปีที่แล้ว +2

    i've got a few problems with this
    the first on is that it only goes to one direction and can't go to the other.That only happens when i set the patrol destination to 2 but when i set it to one or zero it starts having a panic attack
    The second one is from the other video about enemy chase.The code doesn't work

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      I'm happy to take a look at your code for the enemy chase.
      As for the freak out, that usually means that the enemy is unable to properly reach one of your patrol points (often, it is set too high or too low and the enemy can actually get to it)

    • @randomperson.0-0
      @randomperson.0-0 2 ปีที่แล้ว

      @@NightRunStudio i fixed the freakout i will also send the code
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class Patrol : MonoBehaviour
      {
      public float moveSpeed;
      public Rigidbody2D rb;
      public Transform playerTransform;
      public bool isChasing;
      public float chaseDistance;
      public Transform[] PatrolPoints;
      public int patrolDestination;
      // Start is called before the first frame update
      // Update is called once per frame
      void Update()
      {
      if (isChasing)
      {
      if (playerTransform.position.x > playerTransform.position.x)
      {
      transform.localScale = new Vector3(1, 1, 1);
      transform.position += Vector3.left * moveSpeed * Time.deltaTime;
      }
      if (playerTransform.position.x < playerTransform.position.x)
      {
      transform.localScale = new Vector3(-1, 1, 1);
      transform.position += Vector3.right * moveSpeed * Time.deltaTime;
      }
      if (Vector2.Distance(playerTransform.position, playerTransform.position) < chaseDistance)
      {
      isChasing = true;
      }
      }
      else
      {
      {
      transform.position = Vector2.MoveTowards(transform.position, PatrolPoints[0].position, moveSpeed * Time.deltaTime);
      if (Vector2.Distance(transform.position, PatrolPoints[0].position) < .2f)
      {
      transform.localScale = new Vector3(1, 1, 1);
      patrolDestination = 1;
      }
      }
      if (patrolDestination == 0)
      {
      transform.position = Vector2.MoveTowards(transform.position, PatrolPoints[1].position, moveSpeed * Time.deltaTime);
      if (Vector2.Distance(transform.position, PatrolPoints[1].position) < .2f)
      {
      transform.localScale = new Vector3(-1, 1, 1);
      patrolDestination = 0;
      }
      }
      }
      }
      void FixedUpdate()
      {
      }
      }

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      @@randomperson.0-0 So... I think your problems are stemming from the fact that you are comparing your Player transform to your player transform. What you want to check is the difference between your player's transform and the enemy's. Otherwise, the chase conditions will never be true.

    • @randomperson.0-0
      @randomperson.0-0 2 ปีที่แล้ว

      @@NightRunStudio hmm alright thanks for the advice
      tho another problem is that my enemy only goes to one destination

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

      @@randomperson.0-0 No problem. As for the enemy only going to one destination, I can think of 2 reasons:
      1) Make sure that your code is comparing the enemy's position to his desired position
      2) Make sure that he can actually reach the destination (that it's not too high or too low). You can check this by firing a Debug.Log when his position is == to the desired position. If this fires, you'll know he is reaching it, if not, you know he can't reach it).

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

    You are a life saver👍

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

      Haha. Glad it helped. Good luck with your project!

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

    I'm having problems. When my enemy reaches patrol point 0, it just stays there and doesn't turn start moving to patrol point 1. My code is exactly as it is in the video.

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

      This is usually a problem with the way things are set up in Unity. My first suggestion would be to put a debug.log statement in your OnTrigger method (right at the top). That way you can make sure that your enemy is recognizing when it reaches the patrol point. If that’s working, one other common problem is that people often mix up the patrol points and put them on the wrong sides of the enemy (so when it reaches one, it knows to go to the next one, but can’t because it’s in the wrong direction). The debug.log is a good place to start though, as it should help you diagnose where the problem is.
      Let me know if it is still giving you trouble after you try that. Cheers!

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

      Hey, so it turns out I forgot to change one of the numbers in the code. It's now working fine. I really wish I had realized this sooner. 😅

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

      @@BozKolodziejczyk Oh, I hate when that happens! So glad to hear you figured it out. Good luck with your project!

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

    Hello and thank you for the video :) I have a weird problem that when I add the script made in this video to the character it stops the player damage from working.. Soon as I take it off though the player damage works as normal though!

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

      That's pretty strange, since this one only deals with movement. I guess it depends on how your damage script works. Feel free to pop into my discord and share your code if you want. I'd be happy to take a look.

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

    hey having a problem with the flipping part, when it flips it will teleport the sprite half way to the other patrol point.

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

      That probably means your sprite’s pivot point isn’t centred (so it is flipping based on an axis that is off to one side). If you click on you sprite in the assets folder, the go to your sprite editor, you should see a little blue circle on the sprite. Make sure this circle is in the centre, as that is the point from which you will flip. I hope that helps!

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

      @@NightRunStudio I got it working my z wasn't the same for the sprite and the parent object. it was fixed once I made them the same.

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

      @@onedigits9755 That makes sense. Glad to hear you figured it out!

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

      ​@@NightRunStudio Hi, mine has this exact problem but the pivot is centered and the z is the same on both the parent and sprite. Is there anything else that could possibly resolve this issue?

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

      @@sillytoonss Is your x position the same on the child as on the parent? One thought is that your parent may be flipping, but if it is centered at a different x position than the child, the child would appear to teleport.

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

    i have a problem where the enemy goes to the other side of the map, then starts doing the patrol, also for some reason he no longer has a collision, meaning he doesnt collide with anything anymore and goes out of bounds, any idea on the fix?

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

      This is usually an issue to do with parenting. If the enemy, or the patrol point is a child, it will add its parent's position to its own... As a general rule, make sure that the parent of the enemy and of the patrol points has 0, 0, 0 as its position. Then the children should behave better. Hope that helps!

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

    Nice vid again! 🔥🔥

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

    Hi! I have a problem where my enemy only moves to the left, even when i assigned it to the patrol point on the right it still goes left. And also it only moves to one direction. I tried putting Debug.Log in the if(patrolDestination == 0) and the console shows nothing.

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

      It definitely sounds like your enemy is failing to switch destinations (especially if your debug isn't printing to the console). This could be a number of problems. Does your enemy continuously move left, or does it move left and then get stuck once it gets close to your patrol point?
      If it's the latter, then it is usually a case of the enemy getting close to the patrol point, but not actually being able to reach it. This can be fixed by moving the patrol point to a more accessible location (often it is set too high, or is in the ground so the enemy can't get there). You can test this while in play mode by going into scene mode and moving the patrol point at runtime.
      If that doesn't work, feel free to paste your code and I can take a look. Good luck!

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

      @@NightRunStudio This is the code, my enemy is also not able to go back and forth between those patrol points. What i understand is that the enemy does not even recognize the patrol points? That being said i tried readjusting the patrol points in terms of height and distance from each other. Also thanks for the early reply
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class MonsterMovement : MonoBehaviour
      {
      public Transform[] patrolPoints;
      public float moveSpeed;
      public int patrolDestination;
      // Update is called once per frame
      void Update()
      {
      if (patrolDestination == 0)
      {
      transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
      if (Vector2.Distance(transform.position, patrolPoints[0].position) < 0.2f)
      {
      patrolDestination = 1;
      Debug.Log("Reached patrol point 0");
      }
      }
      if (patrolDestination == 1)
      {
      transform.position = Vector2.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
      if (Vector2.Distance(transform.position, patrolPoints[1].position) < 0.2f)
      {
      patrolDestination = 0;
      Debug.Log("Reached patrol point 1");
      }
      }
      }
      }

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

      @@ahmadzufar1112 The code looks good. To solve this, I will need to know what your enemy is doing? Does he move left forever, or does he move for a while and then get stuck?
      The fact that your debugs are not printing tells you that the enemy is never reaching the patrol points. What might be helpful is to add a debug right under the MoveTowards lines. Something like:
      Debug.Log("Distance to patrol point = " + Vector2.Distance(transform.position, patrolPoints[0].position);
      this code, should constantly tell you how far the enemy is from the patrol point (that way you can see if it is actually counting down and where he is close to the patrol point or not).

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

      @@NightRunStudio drive.google.com/file/d/173v6bX75p9xYQ8jnzVjuJb2MOkw9-RPs/view?usp=sharing this is a video on how my enemey behaves, it goes only to the left endlessly

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

      ​@ahmadzufar1112 I know this video is over a year old but did you ever find a solution? I am having the same issue.

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

    I struggle to understand this..
    I watched this video 3 times, and cant understand it very well.
    I wonder if Ill watch it 10 more times will I get it :D ?

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

      Which part is giving you trouble?

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

      ​@@NightRunStudio I do not understand the part with transform.position and when you need to use methods like MoveTowards and and Distance. I guess it does take some time to learn... How long did take for you to learn C# ?

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

      I had been coding for about 6 months when I started this channel, and about 3 years now. I still keep learning stuff all the time. The trick is to keep working at it(which it sounds like you are!).
      MoveTowards and Distance are helper functions that Unity provides to keep us from having to type more code. As with most things in coding, there are often many ways to do any single task. I chose this one for the video as it requires less code, but there’s lots of way to go about it.

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

    it still moves after its dead help

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

      That's strange. The easiest way around this is to destroy the enemy when it runs out of health (Destroy(gameObject), but if you don't want to do that, you may need to turn off the patrol script. (Feel free to pop into the discord server if you want to get into details).

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

    THANK YOU MAN!

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

    thanks a lot you are the best 😁

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

      You're welcome! So glad to know it was helpful.

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

    Hello, i am newbie btw and thanks for the video. i do your tutorial but i have problem now. i actually wrote the exact same codes like yours but there are several problem where the line "if(Vector2.Distance(...) < .2f" was detected error. the error message is "Assets\Scripts\Enemy\EnemyPatrol.cs(28,53): error CS0019: Operator '

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

      Hmmn… that sounds like it might be a brackets error. If you could post the entire if line I might be able to see what’s up.

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

      @@NightRunStudio this is my entire if codes
      if(Vector2.Distance(transform.position, patrolPoints[1].position < 0.2f))
      {
      transform.localScale = new Vector3(-1, 1, 1);
      patrolDestination = 0;
      }

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

      @@faizalaliapriyono1096 Okay! It's definitely a bracket problem. The coordinates for you Vector2.Distance should be in their own set of brackets, and the comparison (the < .2f) should be outside the brackets. Right now, the code isn't sure what to do with the less than sign. It should read:
      if(Vector2.Distance(transform.position, patrolPoints[1].position) < 0.2f)
      this way it can find the distance, THEN compare it to .2f.
      I hope that helps!

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

      @@NightRunStudio oh thanks mate, my bad for not realize that.. thank you so much for helping me. love your video👍👍

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

    I love your tutorials but this one did not work. I dont know what ı am doing ı tried every thing but it does not work. So if you can reply to this commen ı wil share the info of the situation.

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

      Feel free to give me some more details (or to pop into Discord for easier back and forth). That said, a common problem in this video is that the patrol point needs to be set at exactly the right height so the enemy can actually move to it (if it's too high, he will gyrate back and forth underneath it).
      Additionally, some people parent the patrol points (to clean things up), but then miss the point that the new parent has it's own set of coordinates. Now, when they enemy tries to find the patrol point, he adds the parent's position and the patrol point's together). Anyways, thought those might help, but feel free to give more details if not.

    • @NotVortex-i3v
      @NotVortex-i3v 4 วันที่ผ่านมา

      I had the same issue

  • @David.179
    @David.179 ปีที่แล้ว

    I am having a problem. The enemy moves but when it reaches a point it just keeps moving whitout stoping. I have tried changing them or adjusting the height but nothin works. Here is the code if it helps:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class EnemyPatrol : MonoBehaviour
    {
    public Transform[] patrolPoints;
    public float moveSpeed;
    public int patrolDestination;
    // Update is called once per frame
    void Update()
    {
    if(patrolDestination == 0)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[0].position) < 0.2f)
    {
    transform.localScale = new Vector3(1, 1, 1);
    patrolDestination = 1;
    }
    }
    if(patrolDestination == 1)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[1].position) < 0.2f)
    {
    transform.localScale = new Vector3(-1, 1, 1);
    patrolDestination = 0;
    }
    }
    }
    }

    • @David.179
      @David.179 ปีที่แล้ว +1

      Nevermind actually I mare it so that the points move with the enemy so it would never reach them. After i realized the scale was weird but i maneged to fix it somehow and it works great. Thanks for the tutorial!

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

      Glad you figured it out! Good luck with your project.

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

    how would you make the enemy ai jump over obstacles though

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

      Probably the easiest is with a collider set to trigger that you put in front of the enemy, then in the ontrigger method, if the collision object is tagged as ground you apply a new velocity to your rigidbody so the enemy jumps.

  • @Yes.Im.Mr.Anderson
    @Yes.Im.Mr.Anderson ปีที่แล้ว

    While using that system you have to create a points in hierarchy main fork for each enemy, so its VERY BAD idea to do it like that. What if you have just 10 enemies? For each you create 20 patrol points in main tree...

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

      I hear you. I definitely wouldn’t suggest this for games where you have large levels. This is more aimed at beginners, who appreciate the visual approach to setting patrol points. But your point is taken. If you have much more than ten enemies in a scene things can get untenable.

    • @Yes.Im.Mr.Anderson
      @Yes.Im.Mr.Anderson ปีที่แล้ว

      @@NightRunStudio Thnx friend, so what about the idea to create a video with a really good system? I didnt found a good one for 2D, so this content could be really unique and it really helps a lot of peoples do less mistakes even at the start of their gamedev way.

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

      I’ve been thinking about doing just that. This was one of my first series, and I’ve got some ideas to rework these early videos. I feel like we could make this system more effective while maintaining the beginner-friendly nature. Based on your comment, I can see there’s a need for it.

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

    thank you

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

    What happens if u use it for 2 monsters

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

      I would just create separate patrol points for each monster, and then you should be all good.

  • @sweet-tuniverse
    @sweet-tuniverse ปีที่แล้ว

    SO I have a very odd problem. My enemy weak point just falls off and lays on the floor once they start to move and I have no idea why. They still hurt me but I have to find the invisible weak point on the floor to kill them now. Any tips on how to fix it?

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

      Did you put the rigidbody on the weakpoint? That would explain why it suddenly has physics.

    • @sweet-tuniverse
      @sweet-tuniverse ปีที่แล้ว

      @@NightRunStudio Oh it was! But now it just stays in place where the enemy spawned in. It doesn't follow at all.

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

      Did you put the rigidbody back on the parent object (and remember to hook it up in your inspector?). If that’s not it, it might be time to try putting some debug.logs in your code to test which lines are running and which are not being executed.

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

    Hello thanks for the tutorial!! I got a problem, in my case is for a 3d game, I don´t know why it´s wrong:
    I put some Debugs to see if it works and they are always running the same time, (both ifs)
    Also I try to move the points like you said to others in the comments but It didn´t work.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class EnemyPatrol : MonoBehaviour
    {
    public Transform[] patrolPoints;
    public float moveSpeed;
    public int patrolDestination;
    void Start()
    {

    }
    void Update()
    {
    if (patrolDestination == 0)
    {
    transform.position = Vector3.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
    if (Vector3.Distance(transform.position, patrolPoints[0].position) < .2f);
    {
    patrolDestination = 1;
    Debug.Log("oki");
    }
    if (patrolDestination == 1)
    {
    transform.position = Vector3.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
    if (Vector3.Distance(transform.position, patrolPoints[1].position) < .2f);
    {
    patrolDestination = 0;
    Debug.Log("doki");
    }
    }

    }
    }
    }

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

      Alright, so I'm pretty sure your problem comes down to a misplaced bracket. (Man, I hate those things sometimes! :)
      In the video, you may notice that the if statements contained the patrolDestination checks are fully self-contained. In your code, the if(patrolDestination == 1) line is actually WITHIN the same logic that checks if (patrolDestination == 0).
      To fix, you will need to have TWO closed brackets after the line that sets your new destination:
      patrolDestination = 1;
      }
      }
      That should help to keep the logic for each destination separate. Hopefully that solves your problem. If not, at least we fixed one problem, and you can let me know and I'll take another look.
      Cheers!

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

    i have a problem
    the problem is that the weak point don't move with the monster
    can you help me please?
    and sorry for my bad english

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

      Is the weak point on your parent object, along with the script? If so, they should be moving properly. I usually try to keep physics (rigidbody and colliders) on objects that are NOT the sprite-that way the sprite doesn’t get separated from the rest of the object.

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

      @@NightRunStudio
      Can I share my screen with you?
      So you can help me?

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

    when I hit the play button my enemy gets too much small it is a problem can fix it ? pls

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

      That sounds like a problem with your scale on the enemy’s transform component. If you have changed the enemy’s scale to something other than 1, and then use scale.x to flip the enemy, it can mess up the sizing. For example, if your x scale for the enemy is 3.2 then when he flips it will need to be -3.2 I would start by checking those values.

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

      @@NightRunStudio Pls check it

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

      @@NightRunStudio Yes I change the scale of enemy X to 2.5 Y to 2.5 Z to 2.5

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

      @@niveditaph8778 So, it will be important to make sure that when the enemy turns around (when he reaches the patrol point), you set his x value to -2.5 (not to -1). That would cause shrinking.

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

      Sorry I forgot to add f in front of the number that is why I was having an error Thanks for your help :)

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

    My enemy is bouncing up and down instead of turning around even tho i raised the patrolPoints

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

      If he bounces up and down, that almost always means he can't reach the destination point. Adjusting the point should help. That said, you could also add a Debug.Log("hit destination point"); that goes off when he hits the patrol point (that way you can know for sure if the enemy is actually reaching it.

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

      @@NightRunStudio i took away the rigid body and now it works

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

      @@TheFortnitePacifist If there was more than one that would totally do it! Thanks for sharing your solution--I'm sure others will find it useful down the road.

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

    hiya, ran into a problem, my enemy disappears once I play the game? my player can still take damage and the enemy still gives damage but my enemy is invisible lol

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

      Did I figure this one out? Sounds like an issue with your sprite renderer. If you want to send me a copy of your code I can take a look.

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

      @@NightRunStudio Hi!! yes I did, turns out my enemy's z coordinate was higher than the camera lol. I love your videos! they're helping me with my college project! Would I be able to mention your tutorial in my research? In order to get a good grade I need to have research along with the game.

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

      That’s awesome! It’s so gratifying to hear my tutorials are being helpful for projects like this. You can absolutely mention them in your research! Good luck with you project!

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

      @@NightRunStudio Thank you so much!

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

    My problem is that my sprite Starts going to Patrol point zero, but doesn't stop and keeps going, heres my code if I did something wrong
    public Transform[] patrolPoints;
    public float moveSpeed;
    public int patrolDestination;
    // Update is called once per frame
    void Update()
    {
    if (patrolDestination == 0)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f)
    {
    patrolDestination = 1;
    }
    }
    if (patrolDestination == 1)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
    if (Vector2.Distance(transform.position, patrolPoints[1].position) < .2f)
    {
    patrolDestination = 0;
    }
    }
    }

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

      This problem usually means that the enemy can’t quite reach the patrol point. It is often fixed by just lifting or lowering the patrol point a tiny bit.

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

      I tested it by moving the patrol point while the game was running and it didn't work
      @@NightRunStudio

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

      Hmmn… sounds like a problem with the setup in Unity. The enemy should always move toward a patrol point (hence the MoveTowards function). If it is moving past the point and keeps going then it sounds like it is targeting the wrong thing (as it will always move toward its target).
      Other than checking the Unity setup, I would also put in some debug.logs. For example I would put some under the if(Vector2,Distance statement… I suspect it is not firing, but if it does it would at least eliminate the idea that the enemy is moving to the wrong target.

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

    He doesnt move?

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

      He definitely should :) If you want to give some more detail, I can help you trouble shoot. Or you can pop into my discord server and share your code for feedback. discord.gg/6TBz62sV

  • @toustik5161
    @toustik5161 2 ปีที่แล้ว

    Hi im begginer in unity and i was doing your script but it didn t work i check if i have it right and i have but my enemy is moving but not to the points can you help me pls?

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      So here's the thing: I designed this to be highly visual (so you can see the patrol points very clearly), but sometimes, it runs into trouble if the patrol points are placed at locations the object cannot reach. Sometimes, just moving the patrol point a little higher or lower fixes the issue. That said, I'd also be happy to take a look at your code if you want to paste it here.

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

      @@NightRunStudio Thank you for your responce i tried moving it up and down but it doesent work but i can send you my code here it is:
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class MonsterMovement : MonoBehaviour
      {
      public Transform[] patrolPoints;
      public float moveSpeed;
      public int patrolDestination;
      // Update is called once per frame
      void Update()
      {
      if(patrolDestination == 0)
      {
      transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
      if(Vector2.Distance(transform.position, patrolPoints[0].position) < .2f)
      {
      transform.localScale = new Vector3(1, 1, 1);
      patrolDestination = 1;
      }
      }
      if (patrolDestination == 1)
      {
      transform.position = Vector2.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
      if (Vector2.Distance(transform.position, patrolPoints[1].position) < .2f)
      {
      transform.localScale = new Vector3(-1, 1, 1);
      patrolDestination = 0;
      }
      }
      }
      }

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      @@toustik5161 Your code looks good--so it will definitely be a problem in Unity. I suspect that the issue probably has to do with a parent object.
      In your hierarchy, do you have a parent object that your patrol points are children of? (Probably an empty object called "Patrol Points" or something like that?). If so, look at the Transform component on the parent object. The x, y, z values for your position should all be 0. If they have other numbers, they could be affecting the location that your monster is moving to.
      NOTE: once you reset the parent object to 0, 0, 0, you will probably need to adjust the patrol points.
      I hope that helps!

    • @toustik5161
      @toustik5161 2 ปีที่แล้ว

      @@NightRunStudio I don t know what im doing bad but nothing works, but thank you for your help.

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      Did you reset the parent object? That is almost always the issue. If not, I'm not sure what seems to be causing the problem. So sorry we couldn't find it.

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

    THX THX THX THX THX

  • @DTTGamerGAMEPLAYSeCURIOSIDADES
    @DTTGamerGAMEPLAYSeCURIOSIDADES 2 ปีที่แล้ว

    im having a problem with this
    the first code por patrol point 0 and 1 is working how it should but when i try to repeat the process but with patrol point 3 and 4 the mob just stay still without moving . heres the code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class MOnstermoves : MonoBehaviour
    {
    public Transform[] patrolPoints;
    public float moveSpeed;
    public int patrolDestination;

    // Update is called once per frame
    void Update()
    {
    if(patrolDestination == 3)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[3].position, moveSpeed * Time.deltaTime);
    if(Vector2.Distance(transform.position, patrolPoints[3].position) < .2f)
    {
    patrolDestination = 4;
    }
    }
    if(patrolDestination == 4)
    {
    transform.position = Vector2.MoveTowards(transform.position, patrolPoints[4].position, moveSpeed * Time.deltaTime);
    if(Vector2.Distance(transform.position, patrolPoints[4].position) < .2f)
    {
    patrolDestination = 3;
    }
    }
    }
    }
    i would really appreciate the help

    • @NightRunStudio
      @NightRunStudio  2 ปีที่แล้ว

      Is this code for a different enemy, using different patrol points? if so, I think the problem is that you will still want to call these patrol points 0 and 1.
      The array you make will also start by calling the first patrol point 0, no matter what. So each enemy you add will use the same code (you just need to put different patrols in the array in Unity).
      I hope that helps!

    • @DTTGamerGAMEPLAYSeCURIOSIDADES
      @DTTGamerGAMEPLAYSeCURIOSIDADES 2 ปีที่แล้ว

      @@NightRunStudio yes thats was the problem thank you

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

    Hi, im running into a problem where the enemy keeps bouncing and flying around and teleporting but ive moved around the points everywhere and the enemy just doesnt stop bouncing high not like the bounce in the video this one flys all over

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

    the ending step is having som problems. the scale of my monster is below 1 and thus in the decimals which doesnt work. But when i use 1, 1, 1. the sprite gets stretched after pressing play since the x scale is supposed to be lower

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

      One tip I always suggest is not to ever change the scale (or animate) the parent object. If you do, you lose the ability to manipulate its scale and cane run into conflicts with your animator. As a rule, I do physics and flipping on the parent, and then any animation or scaling on the child. That might help.
      If you don’t want to change anything, you could just try using your own numbers. Instead of 1 and -1 for your x values, try setting the to the positive and negative values that your sprite is set to. Or you could try multiplying the x value by 1 and -1 instead of directly setting it. Hope that helps.

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

    I have a few problems I encountered.
    First, the enemy changes in Z position when I press play. It also disappears, but the collider is still there. So how do I make the enemy stay in the same Z position in play when it was in the scene?

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

      Have you checked the z position of your patrol points? It's possible the enemy is orienting to those (Also, if your patrol points have parents in the hierarchy, make sure they are also zeroed out).

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

      @@NightRunStudio Yes, and it's on 1.88 on the Z. But it changes position if I press play.

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

      @@Hammerheadnerd Are you in 2D? IF so, you want to keep your z set to 0. It's also possible that an animation is causing problems with the z...

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

      @@NightRunStudio Alright, I'll try.

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

      @@NightRunStudio I have changed the Z position for all of the sprites, but even now the enemy is still in the same position as it was before if I press play.