Enemy AI in Unity2D #3 A enemy that moves and shoots towards the player

  Рет қаралды 48,251

ChronoABI

ChronoABI

Күн бұрын

In this video you will know how to make these three enemies from hollow knight.
1 The first one will just follow when you are near it.
2 The second one will follow you and when it is close enough start shooting you.
3 The third one is also like the second one but its bullet will start following you.
The main code that I will be using :
Vector2.MoveTowards - docs.unity3d.com/ScriptRefere...
Vector2.Distance - docs.unity3d.com/ScriptRefere...
This is the third enemy Ai in Unity if you want to watch the first and second then there is a playlist called Enemy AI .
• Enemy AI
Time stamps
0:00 = Intro
0:33 = 1st enemy
4:16 = 2nd enemy
8:23 = 3rd enemy
9:46 = Outro
You can also follow me on
Instagram : / chronoabi
KZbin: bit.ly/2CReUlQ
Twitter : / chronoabi
Discord : / discord
You can Support this channel on patron to get the latest video as quickly as Possible
Paytron: / chronoabi
For the bullet script just copy and paste the code in the screen ok.
How to make Enemy AI in Unity,Unity - Enemy follow Player,2D Follow AI With Unity and C#,Enemy ai Unity,Enemy follow player unity 2D,Enemy ai Unity 2D,Unity enemy tutorial,Unity enemy ai follow and shoot player,Unity enemy ai 2D,artificial intelligence,advanced AI,unity bot,ai, programming,EnemyAI series,Enemy AI in Unity #3 A enemy that moves and shoots towards the player

Пікірлер: 114
@mar7348
@mar7348 3 жыл бұрын
thank you so much, such an easy and smart way to have enemy ai following you, helpful for fast prototypes.
@mikesrenders356
@mikesrenders356 2 жыл бұрын
You explain so well. I subscribed 💯🔥
@marga7a997
@marga7a997 11 ай бұрын
I was struggling with shooting for 2 days straight i tried several methods and only your worked right for me
@WallDotCom
@WallDotCom Жыл бұрын
If your having a problem with the .transform just make sure it is Find Game OBJECT with tag not OBJECTS
@kamalchan9756
@kamalchan9756 3 жыл бұрын
i love this kind of videos stright to the point
@saurabhsharma-qw1io
@saurabhsharma-qw1io 3 жыл бұрын
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.
@chronoabi
@chronoabi 4 жыл бұрын
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.
@Jayle_YT
@Jayle_YT 4 жыл бұрын
Can you make more like this?? Especially in the Hollow Knight enemies and bosses. Love it
@christenkasemann1992
@christenkasemann1992 4 жыл бұрын
Maybe a Tetris I think this is nice solution to lern unity
@phi4108
@phi4108 4 жыл бұрын
can you make a tutorial about enemy jump toward player from Hollow Knight.
@palipanther8210
@palipanther8210 3 жыл бұрын
nice man. tbh this is good, and awesome
@notplayer5624
@notplayer5624 3 жыл бұрын
great job man
@christenkasemann1992
@christenkasemann1992 4 жыл бұрын
I subscribed i love you video I understood everything
@chickendilla
@chickendilla 4 жыл бұрын
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?
@thejaycem
@thejaycem 8 ай бұрын
you can use force or lerp
@KeithK469
@KeithK469 2 жыл бұрын
Thank you for this tutorial!
@khushalsinghsaini6272
@khushalsinghsaini6272 3 жыл бұрын
thanks, bro I search this content for 6months but didn't get that but u help me a lot with this.
@zakr2084
@zakr2084 3 жыл бұрын
at 5:30 do why have to copy that script????????????
@khushalsinghsaini6272
@khushalsinghsaini6272 3 жыл бұрын
@@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
@user-ec3bv1pl8o
@user-ec3bv1pl8o 3 жыл бұрын
Thanks, very nice and clear.
@Wrizzlerdude
@Wrizzlerdude Жыл бұрын
is your vpn for free??
@isseyed
@isseyed 3 жыл бұрын
This is a Legendary Video... So simple and efficient :)))))))))))))))) like*1000000
@seaophiel
@seaophiel 3 жыл бұрын
Thanks! 😃
@AK-gt4zc
@AK-gt4zc 2 жыл бұрын
nice vid dude
@jacklagrosepiquette-qy3hq
@jacklagrosepiquette-qy3hq 10 ай бұрын
Hey, thank's for your video, that;s will be a good challenge for implementing it inside my own 2d web engine :)
@christenkasemann1992
@christenkasemann1992 4 жыл бұрын
Very good tutorial
@opt4623
@opt4623 3 жыл бұрын
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!
@givmmemoney
@givmmemoney 2 жыл бұрын
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
@guylazarovich1079
@guylazarovich1079 2 жыл бұрын
how i can make my enemy navigate around obstacles?
@Zahard_ph
@Zahard_ph 2 жыл бұрын
nice video bro. but can you create a video about flying bird dash to player to attack?
@snakeeyes7165
@snakeeyes7165 10 ай бұрын
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
@gabrielfrohlichhernandez3175
@gabrielfrohlichhernandez3175 Жыл бұрын
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
@rectangularprism3630
@rectangularprism3630 11 ай бұрын
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)
@GuitarSchizo
@GuitarSchizo 2 жыл бұрын
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);
@egorro5442
@egorro5442 6 ай бұрын
Спасибо!)
@dragonballz3686
@dragonballz3686 2 жыл бұрын
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.
@dr4g0n53
@dr4g0n53 2 жыл бұрын
thanks :)
@haides98
@haides98 3 жыл бұрын
For the melee attack how do I change the code?
@deacoplo1693
@deacoplo1693 6 ай бұрын
The bullets aren't follower the play but I've followed everything I'm not sure whats wrong
@JoaoFerro
@JoaoFerro 3 жыл бұрын
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.
@syfenw6586
@syfenw6586 8 ай бұрын
how can i make it so it face the enemy if it go right or left
@joshlantry1979
@joshlantry1979 2 жыл бұрын
The tutorial is great but how does the bullet kill the player?can you plz help with code
@DavidAdochiti
@DavidAdochiti 3 жыл бұрын
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!
@arcanep
@arcanep 3 жыл бұрын
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
@j6mes
@j6mes 3 жыл бұрын
wow you are better at editing than coding... jk, keep up the great work :)
@chronoabi
@chronoabi 3 жыл бұрын
Thanks 😅
@danbryandalisay8547
@danbryandalisay8547 4 жыл бұрын
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.
@foreearmspower
@foreearmspower 3 жыл бұрын
yes me too
@chronoabi
@chronoabi 3 жыл бұрын
Guys follow me on discord it will be easy to show you the code
@GuitarSchizo
@GuitarSchizo 2 жыл бұрын
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.
@ibrahemrafee2397
@ibrahemrafee2397 3 жыл бұрын
hey man can you make husk warrior tutorial ???
@mehmeh8883
@mehmeh8883 3 жыл бұрын
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 :)
@chronoabi
@chronoabi 3 жыл бұрын
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
@jayeshpatil4354
@jayeshpatil4354 3 жыл бұрын
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
@senate_66
@senate_66 3 жыл бұрын
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;)
@jonahcarr7347
@jonahcarr7347 2 жыл бұрын
@@senate_66 Thank you, this fixed my problem
@senate_66
@senate_66 2 жыл бұрын
@@jonahcarr7347 Im glad that i could help someone:)
@VerzatileDev
@VerzatileDev 10 ай бұрын
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
@VerzatileDev
@VerzatileDev 10 ай бұрын
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
@zakr2084
@zakr2084 3 жыл бұрын
at 5:30 do why have to copy that script????????????
@jessicaherrera502
@jessicaherrera502 Жыл бұрын
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
@chronoabi
@chronoabi Жыл бұрын
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
@tobyrobson6188
@tobyrobson6188 3 жыл бұрын
anyone know how to update object after respawn at the moment it can not find the cloned player after the first transform is destroyed
@chronoabi
@chronoabi 3 жыл бұрын
Probably make a function that will find the player every time it gets instantiated
@christenkasemann1992
@christenkasemann1992 4 жыл бұрын
Can you explain the funtions more in you nexr Video 😅
@main888
@main888 3 жыл бұрын
Whats up please send me the second CODE please the hole thing
@vladgheorghe7197
@vladgheorghe7197 3 жыл бұрын
how can I flip the enemy? btw make more AI videos.
@chronoabi
@chronoabi 3 жыл бұрын
I have an jumping enemy ai and gruz mother ai . In those I have thought how to make enemy flip player kzbin.info/www/bejne/pGGkf4mrjZysma8 In this video I have thought this
@toxicbird.official
@toxicbird.official Жыл бұрын
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
@chronoabi
@chronoabi Жыл бұрын
What kind of attack does the player do. If it shoot' s then have a If condition
@ridzy7997
@ridzy7997 2 жыл бұрын
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!
@lpsmonster999
@lpsmonster999 2 жыл бұрын
Did you figure it out? I have the same problem
@xus9380
@xus9380 Жыл бұрын
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 ?
@chronoabi
@chronoabi Жыл бұрын
You mean a bulllet that when you shoot the wall, the wall gets destroyed and also the player gets damaged?
@xus9380
@xus9380 Жыл бұрын
@@chronoabi i mean , bullet that come from enemy shooting the player, btw tq for the responses bro 😄
@chronoabi
@chronoabi Жыл бұрын
I guess give the player health and code something that decreases player health value when bullet collides with the player
@salimmohsneh1990
@salimmohsneh1990 Жыл бұрын
Ty I have a qoustion when the enemy shoot the bullet it falls its not going fworord its going down. Somen Can answer PLZZZZZ
@chronoabi
@chronoabi Жыл бұрын
In rigid body of bullet put gravity as 0
@salimmohsneh1990
@salimmohsneh1990 Жыл бұрын
@@chronoabi The bullet is not moving it stop where it came from i did use the code in 2d game
@chronoabi
@chronoabi Жыл бұрын
@@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
@VkStudio_
@VkStudio_ 10 ай бұрын
@@chronoabi have you managed to fix it? f yes then please help me out
@ibrahemrafee2397
@ibrahemrafee2397 3 жыл бұрын
nice video but how can you make the enemy (Flip the enemy to face the player) ???
@chronoabi
@chronoabi 3 жыл бұрын
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
@chronoabi
@chronoabi 3 жыл бұрын
Here is the link for the video kzbin.info/www/bejne/pGGkf4mrjZysma8
@ibrahemrafee2397
@ibrahemrafee2397 3 жыл бұрын
@@chronoabi got it thanks man SUB
@secretartofficial7732
@secretartofficial7732 3 жыл бұрын
@foreearmspower
@foreearmspower 3 жыл бұрын
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
@rogerwinright2290
@rogerwinright2290 3 жыл бұрын
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.
@Lajlaj
@Lajlaj 3 жыл бұрын
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 :-)
@buithanh1444
@buithanh1444 Жыл бұрын
hi @Lajlaj how did you handle it. I'm having problems like you. Many thanks
@VerzatileDev
@VerzatileDev 10 ай бұрын
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); } }
@VerzatileDev
@VerzatileDev 10 ай бұрын
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
@Yagvor
@Yagvor 2 жыл бұрын
it does not work i literaly copied line by line
@foreearmspower
@foreearmspower 3 жыл бұрын
can some body show me how to flip the player
@chronoabi
@chronoabi 3 жыл бұрын
You want to flip tye player or flip enemy towards the player
@foreearmspower
@foreearmspower 3 жыл бұрын
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
@foreearmspower
@foreearmspower 3 жыл бұрын
​@@chronoabi no i fixed it
@foreearmspower
@foreearmspower 3 жыл бұрын
@@chronoabi but can you reply on my other comment
@chronoabi
@chronoabi 3 жыл бұрын
@@foreearmspower which one
@motorparadise
@motorparadise 2 жыл бұрын
Nepali ho bro?
@chronoabi
@chronoabi 2 жыл бұрын
Ho yar
@motorparadise
@motorparadise 2 жыл бұрын
@@chronoabi LinkedIn ma connect garna milxa bro. ma mail garxu!
@2.5dboy20
@2.5dboy20 4 жыл бұрын
Will it work in a 3d game
@chronoabi
@chronoabi 4 жыл бұрын
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
@2.5dboy20
@2.5dboy20 4 жыл бұрын
@@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!!!!
@chronoabi
@chronoabi 4 жыл бұрын
@@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.
@kondrix3291
@kondrix3291 4 жыл бұрын
idiota...
@2.5dboy20
@2.5dboy20 4 жыл бұрын
@@kondrix3291 hehe XD
@bogatyrstudios1272
@bogatyrstudios1272 2 жыл бұрын
why are you using white unity?
@chronoabi
@chronoabi 2 жыл бұрын
because its a super old video XD
@arcanep
@arcanep 3 жыл бұрын
2:50 Line Of Sight dont make the viewers dumbers in english
@chronoabi
@chronoabi 3 жыл бұрын
😂😂
@anishghimire8989
@anishghimire8989 2 жыл бұрын
Kratos is GAE
@emanalialmograbi
@emanalialmograbi 4 ай бұрын
In the first part, the enemy does not move. What is the reason? Programming is no problem @chronoABI
Jump Attacking Enemy AI in Unity 2D   Part 2
12:11
ChronoABI
Рет қаралды 11 М.
Creating SMART enemies from scratch! | Devlog
5:40
Challacade
Рет қаралды 265 М.
TRY NOT TO LAUGH 😂
00:56
Feinxy
Рет қаралды 9 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
2D PATHFINDING - Enemy AI in Unity
23:13
Brackeys
Рет қаралды 801 М.
SHOOTING/FOLLOW/RETREAT ENEMY AI WITH UNITY AND C# - EASY TUTORIAL
12:55
Idle, Run and Jump Animations - Platformer Unity 2D
10:13
Game Code Library
Рет қаралды 29 М.
How Do Games Render So Much Grass?
15:52
Acerola
Рет қаралды 316 М.
Unity - Enemy Follows Player [Beginner Tutorial]
6:51
Press Start
Рет қаралды 183 М.
4D Games | 4D Graphics | What 4D games look like, 2D 3D 4D 5D
15:04
Cyberstars - Как создать игру
Рет қаралды 1,9 МЛН
ADVANCED AI IN UNITY (Made EASY) - STATE MACHINE BEHAVIORS
8:15
Blackthornprod
Рет қаралды 234 М.
2D Enemy Shooting Unity Tutorial
12:52
MoreBBlakeyyy
Рет қаралды 51 М.
TRY NOT TO LAUGH 😂
00:56
Feinxy
Рет қаралды 9 МЛН