i used the script from my moving platform to the game enemies and wondered why they keep getting stuck on the first waypoint lol. i created a different script for the enemies and copied yours haha. thanks again. youre amazing!
Glad this was helpful! I don’t have anything on FSMs yet, but my next big series is going to be on more advanced enemy AI, and I thinking of using an FSM to run this. Unfortunately, that probably won’t be coming until the end of summer. Thanks for the feedback! Best of luck with your project.
Hello Night, this was a good tutorial! I have a question you wouldn't have happened to make a video on how to make the enemy stop a shoot a projectile when he gets within a certain distance did you? I been trying to find a way to customize a line of code for that function but I'm just stuck.
There’s a few things going on there but: If(Vector2.Distance(player.transform.position, enemy.transform.position) < shootDistance) Put that in update, and the monster will constantly check if he’s in range. After that, there’s a few steps… you need to disable his movement (I would just use a bool called inRange or something like that). Then, you need to actually have him fire the projectile. I know that’s a lot to figure out in your own. I’ll put some thought into maybe creating that in a new series. Sadly, I’ve got a couple other things to finish up before I will get to it. Anyways, thanks for watching!
Great series! I've been following along pretty well up to this point and the monster scripts work too. I've already worked through your tutorial series on "Lets Make a 2D Sidescroller", but it now looks like my platform drop script no longer works. Do you have any idea what to do about this? I'm happy about any tips :)
Glad to hear these series have been helpful! I'm not totally sure what you mean by a platform drop script. Can you give me a little more detail, and then I'd be happy to see if I can help.
@@NightRunStudio Thank you for the quick response! I mean the script you wrote in this tutorial: th-cam.com/video/Gm-TnPakWwU/w-d-xo.html I can still jump through the platform from below, but I can't drop anymore.
Sorry for the confusion! Did this script just stop working altogether once you added EnemyChase? That is strange behaviour as they shouldn't interact with each other at all. If you can tell me more about your setup it will help. If it's easier you can always post a video or screen shot in my discord channel. discord.gg/VDyCb3MR@@maximilianherzen3105
This will play particles when you get spotted so the player knows something has happened using System.Collections; using System.Collections.Generic; using UnityEngine; public class MonsterMovement : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed; public int patrolDestination; public Transform playerTransform; public bool isChasing; public float chaseDistance; public ParticleSystem spottedEffect; void getSpotted() { spottedEffect.Play(); } // Update is called once per frame void Update() { if (isChasing) {
Hello I am stuck in a problem after my monster chase the player it just stops moving because isChase is true and there is no way to false it if I manually false it from inspector than it works pls help
I'm not totally sure I'm understanding, but it sounds like you want to monster to stop chasing if you get too far away? if so, you will need a line of code to check if you are far enough away, and then turn isChasing to be false. This will look something like: if(Vector2.Distance(transform.position, player.transform.position) > 10 (or whatever distance you want) { isChasing = false; } I hope that helps!
hi i have a problem. my enemy flips the opposite direction when it is chasing the player when the player is moving right the enemy flips left , when the player is moving left the enemy flips right when its chasing the player
That usually means that you have a < , > facing the wrong direction somewhere. The other possibility is that your facingRight bool might be backwards. I’d start by double checking those.
@@NightRunStudio thank you for replying I double checked and and i fixed the mistake on the code but now my enemy is moving in reverse when its chasing the player and when its patrolling here is the script using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyAI : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed; public int patrolDestination; public Transform playerTransform; public bool isChasing; public float chaseDistance; // Start is called before the first frame update void Start() {
} // Update is called once per frame void Update() { if (isChasing) { if(transform.position.x > playerTransform.position.x) { transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); transform.position += Vector3.left * moveSpeed * Time.deltaTime; } if (transform.position.x < playerTransform.position.x) { transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f); transform.position += Vector3.right * moveSpeed * Time.deltaTime; } } else { if(Vector2.Distance(transform.position,playerTransform.position) < chaseDistance) { isChasing = true; } else { isChasing = false; } 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(-0.5f, 0.5f, 0.5f); 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(0.5f, 0.5f, 0.5f); patrolDestination = 0; } } } } }
@@mmusiphoko5551 My first thought is to double check your local scale. It looks like you use positive numbers when going left... does your sprite naturally face left? If so, then that part is all good. If not, then switching this should fix it. Remembering that a negative localScale will switch your enemy's direction... does this match your patrol points? You currently have positive numbers when approaching patrol point 1... is that correct, or do you need to switch it? (This will depend on what way your sprite naturally faces). I hope that helps.
I think I fixed the problem, my enemy doesn't chase and patrol in reverse anymore , the negative signs/symbols are in different statements in the code. i pasted the code to show you what i did if (isChasing) { if(transform.position.x > playerTransform.position.x) { transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f); transform.position += Vector3.left * moveSpeed * Time.deltaTime; } if (transform.position.x < playerTransform.position.x) { transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); transform.position += Vector3.right * moveSpeed * Time.deltaTime; } } and 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(0.5f, 0.5f, 0.5f); 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(-0.5f, 0.5f, 0.5f); patrolDestination = 0; } } but now the enemy cant stop chasing the player. I added the isChasing statement but it doesn't work
@@mmusiphoko5551 On a quick scan, I can't see anywhere in your code that you tell it to STOP following you. Any time isChasing is true, you need to check to see if the distance between you and the enemy is GREATER than the stopChaseDistance. Then... if the number gets too large, you make isChasing = false. THEN the enemy will stop patrolling. The code for that is almost identical to the one that checks if you are in chase distance, you just need a new variable so it knows when you are out of range. I hope that helps!
For some reason after my monster locates the player, instead of chasing the player. I start controlling the monster? like if i go right, the monster goes right, and if i go left, the monster goes left. anyone got a solution?
Wow… that’s a first! And a weird one. It sounds like maybe your monster thinks it is reaching the player, and so whenever you move, he follows? If so, it is likely a problem with the transform where one object has child object attached that is not in the same location as the sprite. That way, the invisible object is being reached and making the monster think he’s on top of the player even when he isn’t. That’s my best guess. I can’t think of anything in the code that would cause that, but I can take a look if you want to copy it over.
@@NightRunStudio Im new to coding so i wouldnt be surprised if theres some dumb mistake in here: using System.Collections; using System.Collections.Generic; using UnityEngine; public class MonsterMovement : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed; public int patrolDestination;
public Transform playerTransform; public bool isChasing; public float chaseDistance; void Update() {
Yeah… your code looks good. I’m pretty certain it’s an inspector issue in Unity. If your player and enemy have children objects, take a moment to make sure that are in the same place. For example, all the children of your player should generally have a transform position of 0,0,0, unless they are deliberately offset (like if you have a foot position object that’s purposely a tiny bit lower than your player). Then, check the same thing on your enemy. The behaviour you describe makes it sound like maybe the enemy sprite is not zeroed out with the parent object.
@@donggutierrez9383 There must be something that different from my setup. But I can't figure out what it is without seeing your setup. It's usually something little in the code (but if that doesn't work, we can look at some things in Unity).
Hello please help me, I want to make the ai to stop chasing after me and go back to patrolling when I'm far enough. here is the code using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMovement : MonoBehaviour { public Transform[] patrolPoints; // Transform allows you to keep tract of the position and scale of an object public float moveSpeed; public int patrolDestination; //obviously it's the position you would want the ai to go to public Transform playerTransform; // keeps track of the player's position within the game public bool isChasing; // keeps track whether the ai is in chase mode public float chaseDistance; // determs how close the player is before the ai chases them // Update is called once per frame void Update() { if (isChasing = true) { if(transform.position.x > playerTransform.position.x) // moves the ai towards the direction depending on the player's position { transform.localScale = new Vector3(1, 1, 1); transform.position += Vector3.left * moveSpeed * Time.deltaTime; } if (transform.position.x < playerTransform.position.x) { transform.localScale = new Vector3(-1, 1, 1); transform.position += Vector3.right * moveSpeed * Time.deltaTime; }
} else // if the ai isn't chasing the player { if(Vector2.Distance(transform.position, playerTransform.position) < chaseDistance) // if the player is close then the ai will chase them { isChasing = true; } else { isChasing = false; } if (patrolDestination == 0) //then it patrols { //transform.position is the position of the object //MoveTowards takes into account the current position of the object, the desired position and the movement speed // Vector2.Distance calculates the distance between two objects // current position where you would want to move transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime); // last line is the speed and Time.deltaTime would make the object move based on time and not the frames which makes movement smoother // the position of the object this script is used on, and check to see the distance between it and the desired position in this case the patrolPoints if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f) { transform.localScale = new Vector3(-1, 1, 1);// makes the sprite of the object face left patrolDestination = 1; //moves the object towards destination 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); //makes the sprite of the object face right patrolDestination = 0; } } }
To stop chasing after a certain distance, you would just need to add another if statement at the top of you isChasing section. This would just check the distance between the player and the enemy and then turn off isChasing if that distance is too great. You would want a variable for the max chase distance (perhaps call it aggroDistance). It would look something like: if(Vector2.Distance(transform.position, player.transform.position) > aggroDistance) { isChasing = false;}
Hi, i cant get the enemy to stop chasing the player pls help this is my code using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class EnemyMovement : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed; public int patrolDestination; public Transform playerTransform; public bool isChasing; public float chaseDistance; // Update is called once per frame void Update() { if (isChasing) { if(transform.position.x > playerTransform.position.x) { transform.localScale = new Vector3(-1, 1, 1); transform.position += Vector3.left * moveSpeed * Time.deltaTime; } if (transform.position.x < playerTransform.position.x) { transform.localScale = new Vector3(1, 1, 1); transform.position += Vector3.right * moveSpeed * Time.deltaTime; } } else { if (Vector2.Distance(transform.position, playerTransform.position) < chaseDistance) { isChasing = true; } if (Vector2.Distance(transform.position, playerTransform.position) > 5) { isChasing = false; } //Check the patrol destination of the monster if (patrolDestination == 0) { transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime); if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f) { //Send the enemy to a new destination 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) { //Send the enemy to a new destination transform.localScale = new Vector3(-1, 1, 1); patrolDestination = 0; } } }
At a glance, your code looks about right. Is the isChasing book getting set to false when you get far enough away? That would be the first thing I’d check. If not, then you know the problem is with how you’re setting isChasing, if so, then the problem might be in your patrol route section.
HI Night,this was a good tutorial, How can i do my enemy stop chasing my player? this is my code using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMove : MonoBehaviour { public Transform[] patrolPoints; public float moveSpeed; public int patrolDestination; public Transform playerTransform; public bool isChasing; public float chaseDistance; void Update() { if (isChasing) { if(transform.position.x > playerTransform.position.x) { transform.localScale = new Vector3(1, 1, 1); transform.position += Vector3.left * moveSpeed * Time.deltaTime; } if (transform.position.x < playerTransform.position.x) { transform.localScale = new Vector3(-1, 1, 1); transform.position += Vector3.right * moveSpeed * Time.deltaTime; } } else { if(Vector2.Distance(transform.position, playerTransform.position) < chaseDistance) { isChasing = true; } 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; } } } } } thank for u help!
Thanks for the encouraging words! The only thing you are missing here is an else statement to make isChasing become false again. You will want to put this right below your isChasing = true line: isChasing = true; } else { isChasing = false } That way, when the monster is outside of chase distance, it should revert to its usual pattern. I hope that helps!
In my attempt to make this beginner-friendly, this method isn't super adaptable. That said, you could more or less duplicate the logic here, but with a second player transform that you would check. You could add an else if statement so that if the player isn't within chase range, the enemy checks for player 2 next. That said, if you are finding there is a lot more than simple chasing that you need, you may want a more sophisticated enemy system. I do have a more complex series if you're interested (though I haven't added any 2-player specific stuff yet). th-cam.com/video/cQtUN6JnMFI/w-d-xo.htmlsi=ezb6Wj9NsTOv1YNY Whatever you decide, best of luck with your project!
thanks bro i spent ages looking for a genuinely understandable tutorial on this topic and im so glad i finally found one. great help thanks!
liked instantly,
explained in easy language with depth.
u r amazing
That is so good to hear! Thanks for sharing. And good luck with your project!
i used the script from my moving platform to the game enemies and wondered why they keep getting stuck on the first waypoint lol. i created a different script for the enemies and copied yours haha. thanks again. youre amazing!
That's awesome! I love hearing when this code makes it into people's games and helps them accomplish what they are aiming for. Thanks for sharing!
thank you so much . I was in need of this video😃
Glad it was helpful! Thanks for sharing.
Hey, Tysm, for the code; it helped me so much! However, I am wondering if I could make a chase limit for the enemies.
your tutorial is very clear for me, tks
Thanks for sharing that! It's good to know.
thanks man you really make a good tutorial
Thanks for that!
This worked so well! tysm~
bro thank you so much and btw do you have a tutorial video about finite state machine?
Glad this was helpful! I don’t have anything on FSMs yet, but my next big series is going to be on more advanced enemy AI, and I thinking of using an FSM to run this. Unfortunately, that probably won’t be coming until the end of summer. Thanks for the feedback! Best of luck with your project.
Thank you so much
You're most welcome!
Hello Night, this was a good tutorial! I have a question you wouldn't have happened to make a video on how to make the enemy stop a shoot a projectile when he gets within a certain distance did you? I been trying to find a way to customize a line of code for that function but I'm just stuck.
There’s a few things going on there but:
If(Vector2.Distance(player.transform.position, enemy.transform.position) < shootDistance)
Put that in update, and the monster will constantly check if he’s in range. After that, there’s a few steps… you need to disable his movement (I would just use a bool called inRange or something like that). Then, you need to actually have him fire the projectile.
I know that’s a lot to figure out in your own. I’ll put some thought into maybe creating that in a new series. Sadly, I’ve got a couple other things to finish up before I will get to it.
Anyways, thanks for watching!
Great series! I've been following along pretty well up to this point and the monster scripts work too. I've already worked through your tutorial series on "Lets Make a 2D Sidescroller", but it now looks like my platform drop script no longer works. Do you have any idea what to do about this? I'm happy about any tips :)
Glad to hear these series have been helpful!
I'm not totally sure what you mean by a platform drop script. Can you give me a little more detail, and then I'd be happy to see if I can help.
@@NightRunStudio Thank you for the quick response! I mean the script you wrote in this tutorial: th-cam.com/video/Gm-TnPakWwU/w-d-xo.html
I can still jump through the platform from below, but I can't drop anymore.
Sorry for the confusion! Did this script just stop working altogether once you added EnemyChase? That is strange behaviour as they shouldn't interact with each other at all. If you can tell me more about your setup it will help.
If it's easier you can always post a video or screen shot in my discord channel. discord.gg/VDyCb3MR@@maximilianherzen3105
Thank you
This will play particles when you get spotted so the player knows something has happened
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterMovement : MonoBehaviour
{
public Transform[] patrolPoints;
public float moveSpeed;
public int patrolDestination;
public Transform playerTransform;
public bool isChasing;
public float chaseDistance;
public ParticleSystem spottedEffect;
void getSpotted()
{
spottedEffect.Play();
}
// Update is called once per frame
void Update()
{
if (isChasing)
{
if (transform.position.x > playerTransform.position.x)
{
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
transform.localScale = new Vector3(-1,1,1);
}
if (transform.position.x < playerTransform.position.x)
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
transform.localScale = new Vector3(1,1,1);
}
}
else
{
if (Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
{
getSpotted();
isChasing = true;
}
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;
transform.localScale = new Vector3(-1,1,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;
transform.localScale = new Vector3(1,1,1);
}
}
}
}
}
That's a nice addition! Thanks for sharing.
Cool br0❤
Thanks!
Hello I am stuck in a problem after my monster chase the player it just stops moving because isChase is true and there is no way to false it if I manually false it from inspector than it works pls help
I'm not totally sure I'm understanding, but it sounds like you want to monster to stop chasing if you get too far away?
if so, you will need a line of code to check if you are far enough away, and then turn isChasing to be false. This will look something like:
if(Vector2.Distance(transform.position, player.transform.position) > 10 (or whatever distance you want)
{
isChasing = false;
}
I hope that helps!
@@NightRunStudio thanks it works now I wanted the enemy to go back to patrolling if my player goes too far it works now
Awesome! Glad to hear it’s working now.
hi, my enemy's scale is 0.4, 0.4, 0.4 so what should i do?
In that case you would just switch your x-value to -.4f.
hi i have a problem. my enemy flips the opposite direction when it is chasing the player
when the player is moving right the enemy flips left , when the player is moving left the enemy flips right when its chasing the player
That usually means that you have a < , > facing the wrong direction somewhere. The other possibility is that your facingRight bool might be backwards. I’d start by double checking those.
@@NightRunStudio thank you for replying I double checked and and i fixed the mistake on the code but now my enemy is moving in reverse when its chasing the player and when its patrolling
here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public Transform[] patrolPoints;
public float moveSpeed;
public int patrolDestination;
public Transform playerTransform;
public bool isChasing;
public float chaseDistance;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isChasing)
{
if(transform.position.x > playerTransform.position.x)
{
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else
{
if(Vector2.Distance(transform.position,playerTransform.position) < chaseDistance)
{
isChasing = true;
}
else
{
isChasing = false;
}
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(-0.5f, 0.5f, 0.5f);
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(0.5f, 0.5f, 0.5f);
patrolDestination = 0;
}
}
}
}
}
@@mmusiphoko5551 My first thought is to double check your local scale. It looks like you use positive numbers when going left... does your sprite naturally face left? If so, then that part is all good. If not, then switching this should fix it.
Remembering that a negative localScale will switch your enemy's direction... does this match your patrol points? You currently have positive numbers when approaching patrol point 1... is that correct, or do you need to switch it? (This will depend on what way your sprite naturally faces).
I hope that helps.
I think I fixed the problem, my enemy doesn't chase and patrol in reverse anymore , the negative signs/symbols are in different statements in the code. i pasted the code to show you what i did
if (isChasing)
{
if(transform.position.x > playerTransform.position.x)
{
transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
and
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(0.5f, 0.5f, 0.5f);
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(-0.5f, 0.5f, 0.5f);
patrolDestination = 0;
}
}
but now the enemy cant stop chasing the player. I added the isChasing statement but it doesn't work
@@mmusiphoko5551 On a quick scan, I can't see anywhere in your code that you tell it to STOP following you. Any time isChasing is true, you need to check to see if the distance between you and the enemy is GREATER than the stopChaseDistance.
Then... if the number gets too large, you make isChasing = false. THEN the enemy will stop patrolling. The code for that is almost identical to the one that checks if you are in chase distance, you just need a new variable so it knows when you are out of range.
I hope that helps!
The Enemy keeps following the player
You probably just need to add an “else” statement so that if the enemy’s distance is more than your chaseDistance isChasing will be set to false.
@@NightRunStudio It turned out like this
if (isChasing)
{
if (transform.position.x > playerTransform.position.x)
{
transform.localScale = new Vector3(1, 1, 1);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(-1, 1, 1);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else
{
if (Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
{
isChasing = true;
}
else
{
isChasing = false;
}
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;
}
}
}
}
@@guylhermecostasilva2573 That looks great! Thanks for sharing (I'm assuming its all working for you?). Cheers!
For some reason after my monster locates the player, instead of chasing the player. I start controlling the monster? like if i go right, the monster goes right, and if i go left, the monster goes left. anyone got a solution?
Wow… that’s a first! And a weird one. It sounds like maybe your monster thinks it is reaching the player, and so whenever you move, he follows? If so, it is likely a problem with the transform where one object has child object attached that is not in the same location as the sprite. That way, the invisible object is being reached and making the monster think he’s on top of the player even when he isn’t. That’s my best guess. I can’t think of anything in the code that would cause that, but I can take a look if you want to copy it over.
@@NightRunStudio Im new to coding so i wouldnt be surprised if theres some dumb mistake in here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterMovement : MonoBehaviour
{
public Transform[] patrolPoints;
public float moveSpeed;
public int patrolDestination;
public Transform playerTransform;
public bool isChasing;
public float chaseDistance;
void Update()
{
if(isChasing)
{
if(transform.position.x > playerTransform.position.x)
{
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if(transform.position.x < playerTransform.position.x)
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else
{
if(Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
{
isChasing = true;
}
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;
}
}
}
} }
Yeah… your code looks good. I’m pretty certain it’s an inspector issue in Unity. If your player and enemy have children objects, take a moment to make sure that are in the same place. For example, all the children of your player should generally have a transform position of 0,0,0, unless they are deliberately offset (like if you have a foot position object that’s purposely a tiny bit lower than your player). Then, check the same thing on your enemy. The behaviour you describe makes it sound like maybe the enemy sprite is not zeroed out with the parent object.
@@NightRunStudio YOOO. i zeroed out the transform position of the sprite / child of the enemy and now it works properly. Thanks a lot my guy
@@NightRunStudio def deserves a sub
my enemy is not stop to chase my player, why?
If you want to post your code, I'd be happy to take a look.
@@NightRunStudio wait
@@NightRunStudio it it same to your code but my enemy is not stop chasing my player
@@donggutierrez9383 There must be something that different from my setup. But I can't figure out what it is without seeing your setup. It's usually something little in the code (but if that doesn't work, we can look at some things in Unity).
@@NightRunStudio I'ill send my code
Hello please help me, I want to make the ai to stop chasing after me and go back to patrolling when I'm far enough.
here is the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public Transform[] patrolPoints; // Transform allows you to keep tract of the position and scale of an object
public float moveSpeed;
public int patrolDestination; //obviously it's the position you would want the ai to go to
public Transform playerTransform; // keeps track of the player's position within the game
public bool isChasing; // keeps track whether the ai is in chase mode
public float chaseDistance; // determs how close the player is before the ai chases them
// Update is called once per frame
void Update()
{
if (isChasing = true)
{
if(transform.position.x > playerTransform.position.x) // moves the ai towards the direction depending on the player's position
{
transform.localScale = new Vector3(1, 1, 1);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(-1, 1, 1);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else // if the ai isn't chasing the player
{
if(Vector2.Distance(transform.position, playerTransform.position) < chaseDistance) // if the player is close then the ai will chase them
{
isChasing = true;
}
else
{
isChasing = false;
}
if (patrolDestination == 0) //then it patrols
{
//transform.position is the position of the object
//MoveTowards takes into account the current position of the object, the desired position and the movement speed
// Vector2.Distance calculates the distance between two objects
// current position where you would want to move
transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime); // last line is the speed and Time.deltaTime would make the object move based on time and not the frames which makes movement smoother
// the position of the object this script is used on, and check to see the distance between it and the desired position in this case the patrolPoints
if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f)
{
transform.localScale = new Vector3(-1, 1, 1);// makes the sprite of the object face left
patrolDestination = 1; //moves the object towards destination 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); //makes the sprite of the object face right
patrolDestination = 0;
}
}
}
}
}
To stop chasing after a certain distance, you would just need to add another if statement at the top of you isChasing section. This would just check the distance between the player and the enemy and then turn off isChasing if that distance is too great. You would want a variable for the max chase distance (perhaps call it aggroDistance). It would look something like:
if(Vector2.Distance(transform.position, player.transform.position) > aggroDistance)
{ isChasing = false;}
Hi, i cant get the enemy to stop chasing the player pls help
this is my code
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public Transform[] patrolPoints;
public float moveSpeed;
public int patrolDestination;
public Transform playerTransform;
public bool isChasing;
public float chaseDistance;
// Update is called once per frame
void Update()
{
if (isChasing)
{
if(transform.position.x > playerTransform.position.x)
{
transform.localScale = new Vector3(-1, 1, 1);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(1, 1, 1);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else
{
if (Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
{
isChasing = true;
}
if (Vector2.Distance(transform.position, playerTransform.position) > 5)
{
isChasing = false;
}
//Check the patrol destination of the monster
if (patrolDestination == 0)
{
transform.position = Vector2.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
if (Vector2.Distance(transform.position, patrolPoints[0].position) < .2f)
{
//Send the enemy to a new destination
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)
{
//Send the enemy to a new destination
transform.localScale = new Vector3(-1, 1, 1);
patrolDestination = 0;
}
}
}
}
}
At a glance, your code looks about right. Is the isChasing book getting set to false when you get far enough away? That would be the first thing I’d check. If not, then you know the problem is with how you’re setting isChasing, if so, then the problem might be in your patrol route section.
@@NightRunStudio thank you
HI Night,this was a good tutorial, How can i do my enemy stop chasing my player?
this is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
public Transform[] patrolPoints;
public float moveSpeed;
public int patrolDestination;
public Transform playerTransform;
public bool isChasing;
public float chaseDistance;
void Update()
{
if (isChasing)
{
if(transform.position.x > playerTransform.position.x)
{
transform.localScale = new Vector3(1, 1, 1);
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < playerTransform.position.x)
{
transform.localScale = new Vector3(-1, 1, 1);
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
}
else
{
if(Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
{
isChasing = true;
}
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;
}
}
}
}
}
thank for u help!
Thanks for the encouraging words! The only thing you are missing here is an else statement to make isChasing become false again. You will want to put this right below your isChasing = true line:
isChasing = true;
}
else
{
isChasing = false
}
That way, when the monster is outside of chase distance, it should revert to its usual pattern.
I hope that helps!
@@NightRunStudio Appreciate!
Good luck with your project!
If I have two players? How do I set both of them to be chased?🫣
In my attempt to make this beginner-friendly, this method isn't super adaptable. That said, you could more or less duplicate the logic here, but with a second player transform that you would check. You could add an else if statement so that if the player isn't within chase range, the enemy checks for player 2 next.
That said, if you are finding there is a lot more than simple chasing that you need, you may want a more sophisticated enemy system. I do have a more complex series if you're interested (though I haven't added any 2-player specific stuff yet). th-cam.com/video/cQtUN6JnMFI/w-d-xo.htmlsi=ezb6Wj9NsTOv1YNY
Whatever you decide, best of luck with your project!
@@NightRunStudio wow I didn’t expect that you will reply🫣 thank you so so much! love your tutorials
@nessagatto2960 thanks! I always love hearing when people find these videos helpful.