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

Ойындар

This video shows how to create customizable patrol routes for your enemies. Although designed for a 2D sidescroller, this same technique can be easily adapted to a top-down game with more than two points for patrolling.

Пікірлер: 139

  • @trollgasm
    @trollgasm10 ай бұрын

    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

    10 ай бұрын

    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-il2pg11 ай бұрын

    Thanks for such an easy-to-understand Tutorial !

  • @NightRunStudio

    @NightRunStudio

    11 ай бұрын

    You bet! Glad to hear it was useful.

  • @melkorceo5232
    @melkorceo5232 Жыл бұрын

    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

    Жыл бұрын

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

  • @bobbville
    @bobbville2 жыл бұрын

    Nice vid again! 🔥🔥

  • @NightRunStudio

    @NightRunStudio

    2 жыл бұрын

    Thanks!

  • @LumpAbundance
    @LumpAbundance Жыл бұрын

    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.

  • @nihalkapoor8617
    @nihalkapoor861710 ай бұрын

    You are a life saver👍

  • @NightRunStudio

    @NightRunStudio

    10 ай бұрын

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

  • @aliserakkoyun9780
    @aliserakkoyun9780 Жыл бұрын

    thanks a lot you are the best 😁

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

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

  • @bartwillems7896
    @bartwillems7896 Жыл бұрын

    THANK YOU MAN!

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    Haha! Glad it helped 😀

  • @leahgarrett9538
    @leahgarrett95382 ай бұрын

    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

  • @valemejia7214
    @valemejia7214 Жыл бұрын

    este canal debe tener mas vistas

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    Thanks! I’d sure like that, too!

  • @vishnuvimal7602
    @vishnuvimal7602 Жыл бұрын

    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

    Жыл бұрын

    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.

  • @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.

  • @luxe1098
    @luxe1098Ай бұрын

    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

    26 күн бұрын

    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!

  • @wadetwynham4734
    @wadetwynham473411 ай бұрын

    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

    11 ай бұрын

    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

    11 ай бұрын

    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

    11 ай бұрын

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

  • @stuartwright2531
    @stuartwright25316 ай бұрын

    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

    6 ай бұрын

    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.

  • @impidimp9331
    @impidimp93313 ай бұрын

    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

    3 ай бұрын

    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

    3 ай бұрын

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

  • @randomperson.0-0
    @randomperson.0-0 Жыл бұрын

    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

    Жыл бұрын

    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

    Жыл бұрын

    @@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 { transform.localScale = new Vector3(-1, 1, 1); transform.position += Vector3.right * moveSpeed * Time.deltaTime; } if (Vector2.Distance(playerTransform.position, playerTransform.position) { isChasing = true; } } else { { transform.position = Vector2.MoveTowards(transform.position, PatrolPoints[0].position, moveSpeed * Time.deltaTime); if (Vector2.Distance(transform.position, PatrolPoints[0].position) { 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) { transform.localScale = new Vector3(-1, 1, 1); patrolDestination = 0; } } } } void FixedUpdate() { } }

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    @@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

    Жыл бұрын

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

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    @@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).

  • @Osoclevergames
    @Osoclevergames2 ай бұрын

    thank you

  • @NightRunStudio

    @NightRunStudio

    2 ай бұрын

    Glad to help!

  • @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) { 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) { transform.localScale = new Vector3(-1, 1, 1); patrolDestination = 0; } } } }

  • @David.179

    @David.179

    Жыл бұрын

    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.

  • @gingerbread9645
    @gingerbread964511 ай бұрын

    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

    11 ай бұрын

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

  • @gingerbread9645

    @gingerbread9645

    11 ай бұрын

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

  • @NightRunStudio

    @NightRunStudio

    11 ай бұрын

    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.

  • @onedigits9755
    @onedigits9755 Жыл бұрын

    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

    Жыл бұрын

    @@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.

  • @user-vu9vf4ph9b
    @user-vu9vf4ph9b Жыл бұрын

    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!

  • @user-vu9vf4ph9b

    @user-vu9vf4ph9b

    Жыл бұрын

    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

    Жыл бұрын

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

  • @ahmadzufar1112
    @ahmadzufar1112 Жыл бұрын

    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) { 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) { 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

    4 ай бұрын

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

  • @faizalaliapriyono1096
    @faizalaliapriyono1096 Жыл бұрын

    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 { 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 if(Vector2.Distance(transform.position, patrolPoints[1].position) this way it can find the distance, THEN compare it to .2f. I hope that helps!

  • @faizalaliapriyono1096

    @faizalaliapriyono1096

    Жыл бұрын

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

  • @laith9250
    @laith925011 ай бұрын

    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

    11 ай бұрын

    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

    11 ай бұрын

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

  • @migs_2559
    @migs_25597 ай бұрын

    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) { 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) { patrolDestination = 0; } } } } }

  • @NightRunStudio

    @NightRunStudio

    7 ай бұрын

    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

    7 ай бұрын

    @@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

    7 ай бұрын

    @@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

    7 ай бұрын

    @@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

    7 ай бұрын

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

  • @user-qi8nh8pj6e
    @user-qi8nh8pj6e Жыл бұрын

    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.

  • @AidenTheRealShadowwolf
    @AidenTheRealShadowwolf8 ай бұрын

    how would you make the enemy ai jump over obstacles though

  • @NightRunStudio

    @NightRunStudio

    8 ай бұрын

    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.

  • @luka_sv1897
    @luka_sv18973 ай бұрын

    it still moves after its dead help

  • @NightRunStudio

    @NightRunStudio

    3 ай бұрын

    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).

  • @Yes.Im.Mr.Anderson
    @Yes.Im.Mr.Anderson8 ай бұрын

    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

    8 ай бұрын

    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

    8 ай бұрын

    @@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

    8 ай бұрын

    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.

  • @toustik5161
    @toustik5161 Жыл бұрын

    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

    Жыл бұрын

    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

    Жыл бұрын

    @@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) { 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) { transform.localScale = new Vector3(-1, 1, 1); patrolDestination = 0; } } } }

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    @@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

    Жыл бұрын

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

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    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.

  • @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) { 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) { 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!

  • @dreadshin4469
    @dreadshin4469 Жыл бұрын

    THX THX THX THX THX

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    Haha... so glad it helped!

  • @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

    Жыл бұрын

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

  • @Goofy4841
    @Goofy48417 ай бұрын

    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) { 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) { patrolDestination = 0; } } }

  • @NightRunStudio

    @NightRunStudio

    7 ай бұрын

    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

    6 ай бұрын

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

  • @NightRunStudio

    @NightRunStudio

    6 ай бұрын

    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.

  • @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

    Жыл бұрын

    @@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

    Жыл бұрын

    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

    Жыл бұрын

    @@NightRunStudio Thank you so much!

  • @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

    Жыл бұрын

    @@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.

  • @cesurrex7347
    @cesurrex73473 ай бұрын

    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

    3 ай бұрын

    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.

  • @bartwillems7896
    @bartwillems789611 ай бұрын

    He doesnt move?

  • @NightRunStudio

    @NightRunStudio

    11 ай бұрын

    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

  • @DTTGamerGAMEPLAYSeCURIOSIDADES
    @DTTGamerGAMEPLAYSeCURIOSIDADES Жыл бұрын

    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) { 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) { patrolDestination = 3; } } } } i would really appreciate the help

  • @NightRunStudio

    @NightRunStudio

    Жыл бұрын

    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

    Жыл бұрын

    @@NightRunStudio yes thats was the problem thank you

  • @Hammerheadnerd
    @Hammerheadnerd7 күн бұрын

    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

    7 күн бұрын

    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

    7 күн бұрын

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

  • @NightRunStudio

    @NightRunStudio

    7 күн бұрын

    @@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

    7 күн бұрын

    @@NightRunStudio Alright, I'll try.

  • @Hammerheadnerd

    @Hammerheadnerd

    7 күн бұрын

    @@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.

Келесі