I have implemented your code and disable the returnstartpos wich is what i want. but how can i make them wondering around instead of staying stationery when i leave the chase area. Also is there a way to make the movement have a ease out while they stop chasing?
I have no idea why but my player gets stuck on the box collider 2d for the chase trigger. Everything is set to on trigger and my code is fine. But If i place the chase trigger box with enemies it gets them all stuck. If the player touches the box collider 2d he just bounces off. If he is under it and jumps he gets stuck in it.
Hey there! Awesome video You just saved me hours lol! I was wondering if you could tell me how to have multiple enemies with their own chase boxes cause right now if I add a new collider and the player enters the area it will just trigger the original enemy who is at a different spot in my level and chase the player. Hope that makes sense.
When you create a new collider with the ChaseControls script, you need to assign enemies in the inspector (drag and drop enemies you want to be affected).
Do you have anything in the works for "get hit knock back effect? Like castlevania when you get hit by an enemy or jump on them and get knock back effect damage?
You need to change the approach a bit. Forget about this big collider. You can add smaller colliders on choke points and there enable chasing. For disabling chasing, maybe detect collision on enemies, when they touch collider on the end of the area, set chase variable to false.
It can be something in your code or it's a problem with detection. If the detection is the problem, select Rigidbody on the player, and set collision detection to continuous.
Your guide is very useful and simple! But i have one last problem is my enemy don't just Idle in one place but keep following me even i got out of the trigger point, it still just won't return to it starting Point but keep track me to death. What should I do to fix it? The Start Point seem not work.
Assuming that all is good with the code, maybe add the kinematic rigidbody 2d component on "Chase Trigger" object. Also, on the player in the rigidbody2d settings set "Collision Detection" to Continuous
Hi, use "Debug.Log" to detect the problem and see what functions are called. I would first check with debug if the collision between the player and the box trigger is detected.
Sorry, but I'm not an expert in 3D games so I wouldn't know 100%. Of course, you need to use 3D colliders and change the code for 3D colliders, the rest of the code should work.
I'm not sure that I understand you but try to make a basic patrol enemy that will turn when he detects the wall in front or end of the ground. If you want to manipulate that movement, so that the enemy can't go directly to the wall or to the edge of the ground, you can create triggers that will just collide(detect collision) with enemies. When the enemy enters the trigger, flip him in the code. You can place those triggers anywhere you need. Hope it helps ;)
In this case, the array holds a reference to the enemies. Add enemies you want to control to the array. Then we enable/disable chase for each enemy in the array. Note that the ChaseController script goes on your trigger collider.
@@RootGames Sorry I have a similar question, when i put public FlyingEnemy[] enemyArray; in my script unity says that "The type or namespace "FlyingEnemy" could not be found"
This is because the variable GameObject player is null for enemies(since it's destroyed). In the enemy script, we find the "player" in the Start() function and that happens only once, after you destroy it, reference to the player is lost. This approach is for restarting the whole level. In your case, you need somehow tell enemies about the new respawned player. My suggestion is to do it in the ChaseControl since you already have the array of enemies. In addition to setting the chase variable to true, you need to pass them an object(player) that has entered the collider.
In the Flying Enemy script make this function. public void FindPlayer(GameObject newPlayer) { player = newPlayer; } In the Chase Control script, call that function in the foreach loop like this: foreach (FlyingEnemy enemy in enemyArray) { enemy.chase = true; enemy.FindPlayer(collision.gameObject); }
Trigger colliders need to be a part of the enemy prefab that you will instantiate, or just make them follow the player by default when instantiated, you can find the player with some tag and pass its position
I don't believe that copy-paste is a good way of learning to code. You will learn much more if you do it yourself step by step and understand the process and logic behind the code. Try it, this is a fairly easy challenge :)
For me the flip method wasnt working so i asked chatgpt and did it this way and it worked private void Flip() { if (transform.position.x > player.transform.position.x) { transform.localScale = new Vector3(-1, 1, 1); // Flip horizontally } else { transform.localScale = new Vector3(1, 1, 1); // Reset scale } }
Excuse me,
but my Player does not seem to hear the collider and the enemy remains stationary
Any suggestions?
thanks for the tutorial, i rly like it!
Nice and easy way of showing it. Good stuff
I don't have a sprite for the enemy but a skeleton animation of Spine App. ... still a great tutorial.
I have implemented your code and disable the returnstartpos wich is what i want.
but how can i make them wondering around instead of staying stationery when i leave the chase area.
Also is there a way to make the movement have a ease out while they stop chasing?
You can use Randon.insideUnitCircle to choose random positions inside the radius of the enemy.
video for flying enemy dash attack please????
i set the Y position for my enemy to be 180, then when i enter your code, i play the game, the position was like before i set the Y position.
I have no idea why but my player gets stuck on the box collider 2d for the chase trigger. Everything is set to on trigger and my code is fine. But If i place the chase trigger box with enemies it gets them all stuck. If the player touches the box collider 2d he just bounces off. If he is under it and jumps he gets stuck in it.
Hey there! Awesome video You just saved me hours lol! I was wondering if you could tell me how to have multiple enemies with their own chase boxes cause right now if I add a new collider and the player enters the area it will just trigger the original enemy who is at a different spot in my level and chase the player. Hope that makes sense.
When you create a new collider with the ChaseControls script, you need to assign enemies in the inspector (drag and drop enemies you want to be affected).
Do you have anything in the works for "get hit knock back effect? Like castlevania when you get hit by an enemy or jump on them and get knock back effect damage?
I covered that in my course on Udemy/Skillshare 😊
I have a collider that disables when I crouch and he stops chasing, anyway to fix this?
You need to change the approach a bit. Forget about this big collider. You can add smaller colliders on choke points and there enable chasing. For disabling chasing, maybe detect collision on enemies, when they touch collider on the end of the area, set chase variable to false.
very smart way..keep it up
Thank you, I will 👍
hello, somehow everything works but the flying enemies don't go back when my player leaves the box collision, is there something i missed?
It can be something in your code or it's a problem with detection. If the detection is the problem, select Rigidbody on the player, and set collision detection to continuous.
Your guide is very useful and simple! But i have one last problem is my enemy don't just Idle in one place but keep following me even i got out of the trigger point, it still just won't return to it starting Point but keep track me to death. What should I do to fix it? The Start Point seem not work.
Assuming that all is good with the code, maybe add the kinematic rigidbody 2d component on "Chase Trigger" object. Also, on the player in the rigidbody2d settings set "Collision Detection" to Continuous
@@RootGames I fixed it! Thanks for your advice! Your guide is great! Keep making more guide in the future! Thank you so much!!!!
Hello! How can you make it so the enemy wont overlap player's sprite?
Hi! Try to add offset to the player (targeted) position. Or just make the movement stop when their colliders/triggers touch.
hi the chase trigger is not activating the chase when the player enters the box collider
Hi, use "Debug.Log" to detect the problem and see what functions are called. I would first check with debug if the collision between the player and the box trigger is detected.
I am learning well.
If it is 3D instead of 2D, where can I edit the script?
Sorry, but I'm not an expert in 3D games so I wouldn't know 100%. Of course, you need to use 3D colliders and change the code for 3D colliders, the rest of the code should work.
Very Nice
Can you make patrol enemy but only with distance we choose
I'm not sure that I understand you but try to make a basic patrol enemy that will turn when he detects the wall in front or end of the ground. If you want to manipulate that movement, so that the enemy can't go directly to the wall or to the edge of the ground, you can create triggers that will just collide(detect collision) with enemies. When the enemy enters the trigger, flip him in the code. You can place those triggers anywhere you need. Hope it helps ;)
thanks for the video
I'm having trouble understanding the array in ChaseController script. What excatly are we referring too?
In this case, the array holds a reference to the enemies. Add enemies you want to control to the array. Then we enable/disable chase for each enemy in the array.
Note that the ChaseController script goes on your trigger collider.
@@RootGames Sorry I have a similar question, when i put public FlyingEnemy[] enemyArray; in my script unity says that "The type or namespace "FlyingEnemy" could not be found"
@@guillermostaudt6452 Did you name your script "FlyingEnemy"?If you you different script/class name then it goes "YourName[]" enemyArray;
@@RootGames Yes it was exactly that, thanks!!!
Great tutorial! I'm having issues with adding animations to the script if possible can you help? 🥲
Good!
how can i find this asset pack?
This was made for some old game that never saw the light of day, so there is no site to download it from.
i have tag player not defined?
Create a custom tag or use the tag player. Minute 1:30 in the video.
The enemies just freeze after respawning my player clone, how do i fix that?
This is because the variable GameObject player is null for enemies(since it's destroyed). In the enemy script, we find the "player" in the Start() function and that happens only once, after you destroy it, reference to the player is lost. This approach is for restarting the whole level.
In your case, you need somehow tell enemies about the new respawned player. My suggestion is to do it in the ChaseControl since you already have the array of enemies. In addition to setting the chase variable to true, you need to pass them an object(player) that has entered the collider.
@@RootGames i am kind of new to coding, how to do it?
In the Flying Enemy script make this function.
public void FindPlayer(GameObject newPlayer)
{
player = newPlayer;
}
In the Chase Control script, call that function in the foreach loop like this:
foreach (FlyingEnemy enemy in enemyArray)
{
enemy.chase = true;
enemy.FindPlayer(collision.gameObject);
}
@@RootGames thank you so much, this works on prefabs right?
nevermind, it's working. thanks again!!!
goood!!!
How would I set up the chase control if I am doing an infinite runner and enemies are getting cloned?
Trigger colliders need to be a part of the enemy prefab that you will instantiate, or just make them follow the player by default when instantiated, you can find the player with some tag and pass its position
can you please share the codes so we don't make any mistake when we copy it?
I don't believe that copy-paste is a good way of learning to code. You will learn much more if you do it yourself step by step and understand the process and logic behind the code. Try it, this is a fairly easy challenge :)
@@RootGames thank you for replying
bro can u help me with this problem 😅
ChaseControler.cs(24,17): error CS0103: The name 'enemy' does not exist in the current context
For me the flip method wasnt working so i asked chatgpt and did it this way and it worked
private void Flip()
{
if (transform.position.x > player.transform.position.x)
{
transform.localScale = new Vector3(-1, 1, 1); // Flip horizontally
}
else
{
transform.localScale = new Vector3(1, 1, 1); // Reset scale
}
}