Hey follow me on Discord. Link is here discord.gg/78q3HFnb69 You can ask for help , post what your working on and get motivated , show your funny bugs and memes and more.
Awesome video, it helped me alot with making the enemy move, the one thing that would be cool is if someone could tell me how to restrict the enemy movement to the x axis so I can use it for my 2D platformer
you wouldnt use .MoveTowards you would have a vector to represent its current position and then += a vector 2 where you can make either x or y 0 and then change the other based on your speed*Time.deltaTime (im slightly new to coding so let me know if it works)
@@zakr2084 it's your choice if u doesn't want to copy than don't worry just understand the login and than u can make your own script but generally there are same logic I found every single time for bullet script as shown in video
Good video. You are good at explaining things. I am working on a game that uses similar enemies I used the Math functions to calculate the enemy shoot distance but I think your way is easier and efficient.
this vid really helped me make the enemy ai and gave me an idea for an enemy that i never thought about adding before enemy is called pusher pushers have a bigger line of sight, but dont do damage as the name implies, the pushers will use collision to push you which could cause you to get pushed into other enemies or deathzones
Hey! Really enjoy your videos, they are simple to follow and straight to the point. I was wondering, is there a way to make the enemies’ movements “smoother” instead of it following in a straight line?
Second part of the code is here : using UnityEngine; public class EnemyAi : MonoBehaviour { public float speed; public float lineOfSight; public float shootingRange; public float fireRate; private float nextFireTime; public GameObject bullet; public GameObject bulletParent; private Transform player; void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; // FIND THE PLAYER } void Update() { float distanceFromPlayer = Vector2.Distance(player.position, transform.position); // When player goes near enemy // Will move when player in line of sight, but not move when in shooting range if(distanceFromPlayer < lineOfSight && distanceFromPlayer>shootingRange) { transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime); } else if (distanceFromPlayer
Hey for enemy 1 I get an error of 'GameObject[]' does not contain a definition for 'transform' and no accessible extension 'transform' accepting a first argument In short :I need help
Hey, I got the same issue as you. In my case the issue was that i wrote Player= GameObject.FindGameObjects... the issue was ,, FindGameObjects", but it has to be only one GameObject. Sorry for my bad English, i hope this comment helped you;)
The First Part of the Code is here : using UnityEngine; public class EnemyAi : MonoBehaviour { public float speed; public float lineOfSight; private Transform player; void Start() { player = GameObject.FindGameObjectWithTag("Player").transform; // FIND THE PLAYER } void Update() { float distanceFromPlayer = Vector2.Distance(player.position, transform.position); // When player goes near enemy if(distanceFromPlayer < lineOfSight) { transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime); }
I have an jumping enemy ai and gruz mother ai . In those I have thought how to make enemy flip player th-cam.com/video/n0ANVuX7ycM/w-d-xo.html In this video I have thought this
someone can you please explain to me how can i add that AI enemies is facing left or right? sorry for that im also beginner in unity and programming, am just want to learn more mechanics in unity platformer, specially in AI enemies.
private void FlipEnemyX() { if (transform.position.x < LastXValue - 0.1) { //Debug.Log("Decreased!"); LastXValue= transform.position.x; this.spriteRenderer.flipX = true; } else if (transform.position.x > LastXValue + 0.1) { //Debug.Log("Increased"); LastXValue = transform.position.x; this.spriteRenderer.flipX = false; } this code will flip your enemy according to its movement. At first I had 2 scripts that flip the sprite.. when enemy is following the player and when its just patrolling around but it did not work as well .. so I made this new one and it works both situations. (-0.1 or +0.1) will change make it less flipping around when near walls or something when value change is too small... play around with this.
why my enemy is jittering when he is in shooting range. Used exactly the same code you did, but I can't see this problem in this video. transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
Great toturial, but how can i determine when player attack enemy, then enemy keep attacking to the player, otherwise enemy go its way regardless of player
Hi Nice video , i followed it , even i got some problem i done fixed it tq for the tutorial but , Can we make the bullet can collide and destroy with environment and Player too so the Player will get damaged ?
Bro, I am making a 2d top down space shooter. There is a problem in shooting system, that is , when enemy is shooting and I shoot, then the player's bullet is colliding with enemy bullet and the player's bullet is destroyed but the enemies bullet is not destroyed. Can you tell me what to do to make the both bullets passing away without any collision.
@@salimmohsneh1990 does your bullet have rigidbody2D component? If yes and it's still happening then paste the code you wrote ok and we will figure something out
I did this on my enemy and its working perfect. Meanwhile i made a script animation ( using animation 8 static directions and 8 walking directions ) how do i call this animation script when enemy enters in range to follow? All the best tkhs in advance.
how could i make it so that the enemy turns around when the player is either left or right? please respond fast, i'm making this game that i have to finish at the end of this year and i really dont want to extend this deadline. thanks!
what do you mean how? if(enemy.transform.pos.x > player.transform.pos.x) { enemy.getcomponent().flipX or enemy.localscale.x *-1 } and do the same if the opposite is met
Hey, what if there are two targets? (In my game i have a player and a tree) i want the enemy to attack the one near it (player or tree) how can i do this :)
Take in the distance between the enemy and the player and the enemy and the tree and compare which is the smallest then make the enemy go to that object
any one knows how to set up an attack animation with the 2nd part with animation I keep stuck it keeps doing the animation over and over that is my code: private void Update() { float DistanceFromPlayer = Vector2.Distance(player.position, transform.position); if (DistanceFromPlayer < LineOfSite && DistanceFromPlayer > AttackRange) { anime.SetBool("IsRunning", true); anime.SetFloat("speed", Mathf.Abs(speed)); transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime); FlipEnemy(); } else if (DistanceFromPlayer
I know this is 11 months old, but if you haven't figured it out, try to use a .SetBool("EnemyAttack", true) on the animator when they're in range. Set it to false when those conditions aren't met. Redundant, but it might work.
For enemy 2: The projectiles doesnt follow my player, the enemy just drop it down and it disappears, I set the gravity to 0 on Rigidbody, but then it just get stuck in air. Basically it doesnt shoot towards my player, I copied the script but no idea whats wrong? EDIT: Totally my bad, I accidentally used a bullet prefab that was an ingame object with Rigidbody to it :-)
any one knows how to set up an attack animation with the 2nd part with animation I keep stuck it keeps doing the animation over and over that is my code: private void Update() { float DistanceFromPlayer = Vector2.Distance(player.position, transform.position); if (DistanceFromPlayer < LineOfSite && DistanceFromPlayer > AttackRange) { anime.SetBool("IsRunning", true); anime.SetFloat("speed", Mathf.Abs(speed)); transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime); FlipEnemy(); } else if (DistanceFromPlayer
Well the concept is same but you have to change many values to make it work on 3D . Like change the overlap circle to overlap sphere,make it rotate towards the player on the z axis as well so the enemy faces toward the player and many more but yeah it will work
@@chronoabi can you please tell me what things I have to change in detail..I have been searching for a working tutorials for days... please please please!!!!
@@2.5dboy20 I have not worked with 3D that much so sorry if this doesn't workout but I think if you replace every Vector2 with vector3 should work . Do it on all of them and even in the bullet code and I think it will work.
If your having a problem with the .transform just make sure it is Find Game OBJECT with tag not OBJECTS
I was struggling with shooting for 2 days straight i tried several methods and only your worked right for me
Hey follow me on Discord. Link is here
discord.gg/78q3HFnb69
You can ask for help , post what your working on and get motivated , show your funny bugs and memes and more.
Can you make more like this?? Especially in the Hollow Knight enemies and bosses. Love it
Maybe a Tetris I think this is nice solution to lern unity
can you make a tutorial about enemy jump toward player from Hollow Knight.
thank you so much, such an easy and smart way to have enemy ai following you, helpful for fast prototypes.
Awesome video, it helped me alot with making the enemy move, the one thing that would be cool is if someone could tell me how to restrict the enemy movement to the x axis so I can use it for my 2D platformer
you wouldnt use .MoveTowards you would have a vector to represent its current position and then += a vector 2 where you can make either x or y 0 and then change the other based on your speed*Time.deltaTime (im slightly new to coding so let me know if it works)
thanks, bro I search this content for 6months but didn't get that but u help me a lot with this.
at 5:30 do why have to copy that script????????????
@@zakr2084 it's your choice if u doesn't want to copy than don't worry just understand the login and than u can make your own script but generally there are same logic I found every single time for bullet script as shown in video
Good video. You are good at explaining things. I am working on a game that uses similar enemies I used the Math functions to calculate the enemy shoot distance but I think your way is easier and efficient.
You explain so well. I subscribed 💯🔥
this vid really helped me make the enemy ai and gave me an idea for an enemy that i never thought about adding before
enemy is called pusher
pushers have a bigger line of sight, but dont do damage
as the name implies, the pushers will use collision to push you which could cause you to get pushed into other enemies or deathzones
i love this kind of videos stright to the point
Hey! Really enjoy your videos, they are simple to follow and straight to the point. I was wondering, is there a way to make the enemies’ movements “smoother” instead of it following in a straight line?
you can use force or lerp
how i can make my enemy navigate around obstacles?
Hey, thank's for your video, that;s will be a good challenge for implementing it inside my own 2d web engine :)
Second part of the code is here :
using UnityEngine;
public class EnemyAi : MonoBehaviour
{
public float speed;
public float lineOfSight;
public float shootingRange;
public float fireRate;
private float nextFireTime;
public GameObject bullet;
public GameObject bulletParent;
private Transform player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform; // FIND THE PLAYER
}
void Update()
{
float distanceFromPlayer = Vector2.Distance(player.position, transform.position); // When player goes near enemy
// Will move when player in line of sight, but not move when in shooting range
if(distanceFromPlayer < lineOfSight && distanceFromPlayer>shootingRange)
{
transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
}
else if (distanceFromPlayer
Bullet Script is here ;
using UnityEngine;
public class Bullet : MonoBehaviour
{
GameObject target;
public float speed;
Rigidbody bulletRB;
void Start()
{
bulletRB = GetComponent();
target = GameObject.FindGameObjectWithTag("Player");
Vector2 moveDir = (target.transform.position - transform.position).normalized * speed;
bulletRB.velocity = new Vector2(moveDir.x, moveDir.y);
Destroy(this.gameObject, 2);
}
}
// Change it to 3d sorry
nice man. tbh this is good, and awesome
Thanks! 😃
great job man
Hey for enemy 1 I get an error of 'GameObject[]' does not contain a definition for 'transform' and no accessible extension 'transform' accepting a first argument
In short :I need help
Hey,
I got the same issue as you. In my case the issue was that i wrote Player= GameObject.FindGameObjects... the issue was
,, FindGameObjects", but it has to be only one GameObject.
Sorry for my bad English, i hope this comment helped you;)
@@senate_66 Thank you, this fixed my problem
@@jonahcarr7347 Im glad that i could help someone:)
Thanks, very nice and clear.
is your vpn for free??
Thank you for this tutorial!
nice video bro. but can you create a video about flying bird dash to player to attack?
This is a Legendary Video... So simple and efficient :)))))))))))))))) like*1000000
Hello man, thank you for the video, it was really helpful. Everything works fine except for the bullet code, Vector2 is highlighted red idk why!
I subscribed i love you video I understood everything
nice vid dude
The First Part of the Code is here :
using UnityEngine;
public class EnemyAi : MonoBehaviour
{
public float speed;
public float lineOfSight;
private Transform player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform; // FIND THE PLAYER
}
void Update()
{
float distanceFromPlayer = Vector2.Distance(player.position, transform.position); // When player goes near enemy
if(distanceFromPlayer < lineOfSight) {
transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, lineOfSight);
}
}
Bullet Script is here ;
using UnityEngine;
public class Bullet : MonoBehaviour
{
GameObject target;
public float speed;
Rigidbody bulletRB;
void Start()
{
bulletRB = GetComponent();
target = GameObject.FindGameObjectWithTag("Player");
Vector2 moveDir = (target.transform.position - transform.position).normalized * speed;
bulletRB.velocity = new Vector2(moveDir.x, moveDir.y);
Destroy(this.gameObject, 2);
}
}
// Change it to 3d sorry
how can I flip the enemy? btw make more AI videos.
I have an jumping enemy ai and gruz mother ai . In those I have thought how to make enemy flip player
th-cam.com/video/n0ANVuX7ycM/w-d-xo.html
In this video I have thought this
Very good tutorial
someone can you please explain to me how can i add that AI enemies is facing left or right? sorry for that im also beginner in unity and programming, am just want to learn more mechanics in unity platformer, specially in AI enemies.
yes me too
Guys follow me on discord it will be easy to show you the code
private void FlipEnemyX()
{
if (transform.position.x < LastXValue - 0.1)
{
//Debug.Log("Decreased!");
LastXValue= transform.position.x;
this.spriteRenderer.flipX = true;
}
else if (transform.position.x > LastXValue + 0.1)
{
//Debug.Log("Increased");
LastXValue = transform.position.x;
this.spriteRenderer.flipX = false;
}
this code will flip your enemy according to its movement. At first I had 2 scripts that flip the sprite.. when enemy is following the player and when its just patrolling around but it did not work as well .. so I made this new one and it works both situations. (-0.1 or +0.1) will change make it less flipping around when near walls or something when value change is too small... play around with this.
wow you are better at editing than coding...
jk, keep up the great work :)
Thanks 😅
How do you reference an enemy that is a prefab, I can't seem to drag my user in as a reference or find a tag for it
thanks :)
The bullets aren't follower the play but I've followed everything I'm not sure whats wrong
Спасибо!)
why my enemy is jittering when he is in shooting range. Used exactly the same code you did, but I can't see this problem in this video.
transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
I followed the code and I am getting an error on transform I am so confused send help
how can i make it so it face the enemy if it go right or left
Great toturial, but how can i determine when player attack enemy, then enemy keep attacking to the player, otherwise enemy go its way regardless of player
What kind of attack does the player do. If it shoot' s then have a If condition
For the melee attack how do I change the code?
Hi Nice video , i followed it , even i got some problem i done fixed it tq for the tutorial but , Can we make the bullet can collide and destroy with environment and Player too so the Player will get damaged ?
You mean a bulllet that when you shoot the wall, the wall gets destroyed and also the player gets damaged?
@@chronoabi i mean , bullet that come from enemy shooting the player, btw tq for the responses bro 😄
I guess give the player health and code something that decreases player health value when bullet collides with the player
Bro, I am making a 2d top down space shooter. There is a problem in shooting system, that is , when enemy is shooting and I shoot, then the player's bullet is colliding with enemy bullet and the player's bullet is destroyed but the enemies bullet is not destroyed. Can you tell me what to do to make the both bullets passing away without any collision.
hey man can you make husk warrior tutorial ???
I have a problem with enemy 2, when the second gizmo detects it to attack and sticks to the player, it has a bounce, how can I solve that? :c
can you show me your code or maybe go to discord and show me your project. I will try to help. Really sorry for the late reply
The tutorial is great but how does the bullet kill the player?can you plz help with code
Ty I have a qoustion when the enemy shoot the bullet it falls its not going fworord its going down. Somen Can answer PLZZZZZ
In rigid body of bullet put gravity as 0
@@chronoabi The bullet is not moving it stop where it came from i did use the code in 2d game
@@salimmohsneh1990 does your bullet have rigidbody2D component? If yes and it's still happening then paste the code you wrote ok and we will figure something out
@@chronoabi have you managed to fix it? f yes then please help me out
anyone know how to update object after respawn at the moment it can not find the cloned player after the first transform is destroyed
Probably make a function that will find the player every time it gets instantiated
I did this on my enemy and its working perfect. Meanwhile i made a script animation ( using animation 8 static directions and 8 walking directions ) how do i call this animation script when enemy enters in range to follow? All the best tkhs in advance.
Whats up please send me the second CODE please the hole thing
Can you explain the funtions more in you nexr Video 😅
how could i make it so that the enemy turns around when the player is either left or right? please respond fast, i'm making this game that i have to finish at the end of this year and i really dont want to extend this deadline. thanks!
what do you mean how?
if(enemy.transform.pos.x > player.transform.pos.x)
{
enemy.getcomponent().flipX
or
enemy.localscale.x *-1
}
and do the same if the opposite is met
at 5:30 do why have to copy that script????????????
nice video but how can you make the enemy (Flip the enemy to face the player) ???
I have a video where i make Gruz mother enemy(from hollow knight) You can watch that . I have dont exactly what you need there
Here is the link for the video
th-cam.com/video/n0ANVuX7ycM/w-d-xo.html
@@chronoabi got it thanks man SUB
hey!
i would like to ask how do i put animation to the shooting fuction, this will helps me alot, i hope i get a response
a great video too!!
tqsm!
Did you figure it out? I have the same problem
Hey, what if there are two targets? (In my game i have a player and a tree) i want the enemy to attack the one near it (player or tree) how can i do this :)
Take in the distance between the enemy and the player and the enemy and the tree and compare which is the smallest then make the enemy go to that object
any one knows how to set up an attack animation with the 2nd part with animation
I keep stuck it keeps doing the animation over and over that is my code:
private void Update()
{
float DistanceFromPlayer = Vector2.Distance(player.position, transform.position);
if (DistanceFromPlayer < LineOfSite && DistanceFromPlayer > AttackRange)
{
anime.SetBool("IsRunning", true);
anime.SetFloat("speed", Mathf.Abs(speed));
transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
FlipEnemy();
}
else if (DistanceFromPlayer
I know this is 11 months old, but if you haven't figured it out, try to use a .SetBool("EnemyAttack", true) on the animator when they're in range.
Set it to false when those conditions aren't met. Redundant, but it might work.
For enemy 2: The projectiles doesnt follow my player, the enemy just drop it down and it disappears, I set the gravity to 0 on Rigidbody, but then it just get stuck in air. Basically it doesnt shoot towards my player, I copied the script but no idea whats wrong?
EDIT: Totally my bad, I accidentally used a bullet prefab that was an ingame object with Rigidbody to it :-)
hi @Lajlaj how did you handle it. I'm having problems like you. Many thanks
it does not work i literaly copied line by line
can some body show me how to flip the player
You want to flip tye player or flip enemy towards the player
any one knows how to set up an attack animation with the 2nd part with animation
I keep stuck it keeps doing the animation over and over that is my code:
private void Update()
{
float DistanceFromPlayer = Vector2.Distance(player.position, transform.position);
if (DistanceFromPlayer < LineOfSite && DistanceFromPlayer > AttackRange)
{
anime.SetBool("IsRunning", true);
anime.SetFloat("speed", Mathf.Abs(speed));
transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
FlipEnemy();
}
else if (DistanceFromPlayer
@@chronoabi no i fixed it
@@chronoabi but can you reply on my other comment
@@foreearmspower which one
Nepali ho bro?
Ho yar
@@chronoabi LinkedIn ma connect garna milxa bro. ma mail garxu!
Will it work in a 3d game
Well the concept is same but you have to change many values to make it work on 3D . Like change the overlap circle to overlap sphere,make it rotate towards the player on the z axis as well so the enemy faces toward the player and many more but yeah it will work
@@chronoabi can you please tell me what things I have to change in detail..I have been searching for a working tutorials for days... please please please!!!!
@@2.5dboy20 I have not worked with 3D that much so sorry if this doesn't workout but I think if you replace every Vector2 with vector3 should work . Do it on all of them and even in the bullet code and I think it will work.
idiota...
@@kondrix3291 hehe XD
2:50 Line Of Sight dont make the viewers dumbers in english
😂😂
why are you using white unity?
because its a super old video XD
Kratos is GAE
In the first part, the enemy does not move. What is the reason? Programming is no problem @chronoABI