Тёмный

Unity - Enemy Follows Player [Beginner Tutorial] 

Press Start
Подписаться 24 тыс.
Просмотров 184 тыс.
50% 1

In this video I show you how to create enemy AI that follows your player around the screen.
SUBSCRIBE: bit.ly/2Js78lE
============
SUPPORT: / pressstartclub
In this video, we cover:
0:12 - Downloading and importing assets from the asset store
0:30 - Animating and creating our characters
1:10 - Creating a C# script for our Follow Enemy AI script
1:40 - Calculating the direction our player is in real time
2:20 - Adding a RigidBody2D to rotate our enemy to face our player
3:20 - Calculating the rotation in degrees to rotate our enemy
4:20 - Moving our enemy to chase the player object
6:00 - Final Product
6:15 - Tutorial Challenge
Armored Soldiers Asset Pack:
assetstore.unity.com/packages...
COPY & PASTE CODE FROM THIS TUTORIAL:
pressstart.vip/tutorials/2019...
MORE TUTORIALS:
pressstart.vip/unity-tutorials

Опубликовано:

 

10 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 339   
@antsinpantsninja6619
@antsinpantsninja6619 5 лет назад
Awesome video. Need more of these short simple tutorials out there. Thank you for not making this into a 20 min video.
@mr.jeffjeffun1212
@mr.jeffjeffun1212 3 года назад
you don't know how much I appreciate the fact that you gave the code from the tutorial possible to copy (great tutorial).
@karwannouri8266
@karwannouri8266 4 года назад
Thank you for this tutorial. Your voice, the speed and the way you speak is great. The information was very valuable but you present everything really well. Thank you!
@chuk_udi
@chuk_udi 4 года назад
Your tutorials are well made and explain unity's mechanics well. Thank you and keep making more videos.
@j.a.m.s6340
@j.a.m.s6340 3 года назад
tysm i have been looking for this and watched numerous videos but nothing worked but yours did!
@avisekch
@avisekch 4 года назад
Lovely Mr. Waldo. Thank you very much for such short and to the point tutorial.
@ErtBaran
@ErtBaran 4 года назад
Thanks soo much. I understand in the end. 💯 Drawing angles and positions was so helpful.
@tsivils1987
@tsivils1987 Год назад
You're a life saver I don't know coding much at all but i am making a game using youtube tutorials lol. I had gotten my enemies following my player but they were always looking the wrong way now I've got my answer and can go fix it now!
@thejamesgreensill
@thejamesgreensill 4 года назад
Thank you, been trying to figure out a way to do this using physics rather then Transform.
@ApexArtistX
@ApexArtistX 2 года назад
Your tutorial makes sense. Now I learn vector to degrees conversion. And I understand
@AbdulRahman-vt4bw
@AbdulRahman-vt4bw 5 лет назад
Very cool, thank you for making this tutorial
@savecandy6881
@savecandy6881 5 лет назад
Thanks bro I love how you write codes
@TortoRacoon
@TortoRacoon 4 года назад
Thank you! I did it completely different but your logic helped me figure it out
@wojciechbecela5550
@wojciechbecela5550 3 года назад
That's exactly what I was looking for! Thanks a lot!
@meg1a498
@meg1a498 4 года назад
i cant belive there is people in this world that are trying to help,i couldnt belive that discription man keep it up you just got a subscriber. btw do you have any tutorials about enemy spawning on random locations?
@CodesnippetBiz
@CodesnippetBiz 4 года назад
very good tutorial, nice and simple! I love it!
@-Dolphus
@-Dolphus 4 года назад
Yo dude, thanks for the help. I changed your script to fit min tho, so it's not completely the same. But thanks to you, I got inspiration for a way to fix my issue, which was rotation of the enemy towards player.position.
@xTheYeTiChannel
@xTheYeTiChannel 4 года назад
Your tutorials are very nice & understandable, i like you!
@pressstart6864
@pressstart6864 4 года назад
I like you!
@quimerico
@quimerico 3 года назад
Awesome video, very clear and simple :}
@danlapersy5099
@danlapersy5099 4 года назад
Thank you so much Press, you are awesome with this video you help me alot .
@hhashh2327
@hhashh2327 4 года назад
Awesome video! Saved my life!
@eekturk4258
@eekturk4258 3 года назад
Great tutorial! I am making a game about a circle with a gun fighting against other shapes with guns for no reason and the follow worked perfectly, it really fits to my game.
@brianrobson8864
@brianrobson8864 2 года назад
Muito obrigado meu passeiro, estava procurando esse tipo de rotação no inimigo quando ele estivesse seguindo o player e olhasse para o alvo... busquei busquei e apareceu seu vídeo, muito obrigado msm, me ajudou muito...
@brianrobson8864
@brianrobson8864 2 года назад
BRASIL🇧🇷
@Glori4n
@Glori4n 4 года назад
Thank you for the very informative video!
@akuzanugraha4339
@akuzanugraha4339 4 года назад
This is very good channel for Unity Tutorial... Thanks for the tutorial dude.. you awesome!!!!
@pressstart6864
@pressstart6864 4 года назад
Nah you’re awesome for taking the time out to give feedback. And positive feedback at that. Cheers my friend, it’s people like you that keep me going
@bluestripes6037
@bluestripes6037 4 года назад
You're a great guy. Thank you.
@pressstart6864
@pressstart6864 4 года назад
Geee thanks :)
@legion5548
@legion5548 3 года назад
Thank you, your video helped a lot.
@foreducation408
@foreducation408 2 года назад
this video is awesome, helped me soo much.
@trailerkeller6760
@trailerkeller6760 2 года назад
I needed this!!!
@bluepie4983
@bluepie4983 4 года назад
That is a very helpful tutroial, thanks
@gametechy702
@gametechy702 3 года назад
Great Tutorial Man! Your channel is the best out there! You got my Like and Sub too!!
@-workout4118
@-workout4118 4 года назад
Hi, I am from Russia, thanks for the understandable and useful video tutorials, very informative!
@michaelshollaj6428
@michaelshollaj6428 4 года назад
thanks a lot man , it helped me a lot :D
@Ben-qn3py
@Ben-qn3py 5 лет назад
You're amazing man!
@pressstart6864
@pressstart6864 5 лет назад
Thank you!
@binarium4376
@binarium4376 2 года назад
thanks a lot! it helped me so much
@mahanfarahani698
@mahanfarahani698 4 года назад
well done and nice job,thanks
@Tim_Voronkin
@Tim_Voronkin 4 года назад
thank you very mutch, cool tutorial
@AfterBirth
@AfterBirth 3 года назад
subscribed bcox u provided script easily
@pablito4762
@pablito4762 3 года назад
Great tutorial
@rrlosabio7190
@rrlosabio7190 3 года назад
BRACKEYS QUIT SO UR MY SECOND BEST TUTORIAL FOR ME UR EZ TO UNDERSTAND COMPARE TO OTHER EXISISTING,, KEEP BRO HOPE YOUR CHANNEL GROW FAST
@Plapp_
@Plapp_ 2 года назад
calm down captain caps lock
@FatherPhi
@FatherPhi 5 лет назад
I love your videos man, it would be great to interview you one day haha
@pressstart6864
@pressstart6864 5 лет назад
Shoot me an email lets talk
@JxD01
@JxD01 5 лет назад
Your codes are Sooo EZ for everyone Thanks for this powerful tutorial 😋 please make a video on multiplayer system
@klarnorbert
@klarnorbert 4 года назад
Add some obstacles between the player and the enemy, and this method breaks.
@EnderElectrics
@EnderElectrics 4 года назад
@@klarnorbert i guess
@lunamnauta5844
@lunamnauta5844 4 года назад
@@klarnorbert A* is fairly easy to learn, and works great if you have obstacles
@gone192
@gone192 4 года назад
@@klarnorbert What I've found in a lot of the tutorials on RU-vid is the skeleton of the mechanic a developer wants to create. Like I used this and it didn't work exactly like I needed it to, so I iterated on it. So yes, this isn't the best, but it is a damn good starting place for those who don't know how to do it. - And to anyone reading this, don't forget to stress critical thinking and creative problem solving. Don't take this tutorial as the end result but as the beginning theory for the end result. If it doesn't behave like you want to, iterate on it or find work arounds. You. Are. The. Developer. - And don't think I'm some great programmer looking down, I know it's hard, I've been programming for a collective two weeks so far. Just have the confidence to iterate on these skeletons of mechanics.
@danielholth
@danielholth 4 года назад
@@gone192 Spot on :)
@josephturgoose7470
@josephturgoose7470 2 года назад
This video is simple and really shows you how to quickly set up the enemy movement, which is great. However, I'm still really new to Unity, so I would like to learn about what each function specifically is and does to know how it all fits together. Otherwise I'd probably be watching these same basic tutorials over and over again throughout my entire game dev career, which isn't ideal.
@brunodesyeux
@brunodesyeux Год назад
just search up tutorials for the things you find hard
@sharkspread
@sharkspread 3 года назад
works great
@kuma4an
@kuma4an 4 года назад
Thanks for script!
@abyanmahya5184
@abyanmahya5184 2 года назад
thanks alot man
@youtubemierda
@youtubemierda Год назад
Very Good
@PEDRO4075
@PEDRO4075 4 года назад
Great job brother. Hope you can do more and more videos and have lots of subs. Thank you ✌️
@crio9604
@crio9604 3 года назад
You could also just define a lookDir Vector ( transform.right ) and do transform.right = player.transform.position - transform.position, so you don't need to calculate angles for rotation.
@ImYaxon
@ImYaxon 3 года назад
thanks dude i love u
@hamzabelarbi6169
@hamzabelarbi6169 2 года назад
Muchísimas gracias hombre
@Yiu-wq1yo
@Yiu-wq1yo 5 лет назад
My best channel Good Work man :D Can u make tutorial about Enemies (ranged and Melee) ?
@pressstart6864
@pressstart6864 4 года назад
Will do!
@ahmaddehaini1158
@ahmaddehaini1158 4 года назад
Thanks very helpfull video
@ZiRR0
@ZiRR0 4 года назад
Thank you!!!
@noormuhammad5923
@noormuhammad5923 4 года назад
Yooo thanks so much
@kawerishirke5568
@kawerishirke5568 3 года назад
Thank You So Much...
@halukddddd
@halukddddd 4 года назад
thank you so much
@kamilorado6979
@kamilorado6979 4 года назад
Thanks!
@sebastianurciuolo6249
@sebastianurciuolo6249 4 года назад
You should use navmesh agent unless your game level is just an empty desert. And teach a way to implement this in a way where we don't calculate distance every frame on each enemy
@GamingWarlord64
@GamingWarlord64 4 года назад
Never mind i got it Thank you i have been trying to do this all day if your trying to do this on a 3D place do Vector 3 not 2 that's all.
@astrabajtyt
@astrabajtyt 4 года назад
You don't need to use Vector2 at all really, you might use this code, its a bit easier I think:) using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewFollower : MonoBehaviour { [SerializeField] Transform target; float moveSpeed = 4f; Rigidbody2D rb; Vector3 direction; void Start() { rb = GetComponent(); } void Update() { direction = target.position - transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; rb.rotation = angle; } private void FixedUpdate() { Move(); } void Move(){ rb.MovePosition(transform.position + (direction.normalized * moveSpeed * Time.deltaTime)); } }
@dasigkatama029
@dasigkatama029 4 года назад
Thank You
@Gabriel-ti2rl
@Gabriel-ti2rl 4 года назад
Nice vid
@adityaiyengar152
@adityaiyengar152 3 года назад
3:55 this solution worked for me: using 'Rb.rotation = -angle + 180f' instead of 'Rb.rotation = angle'
@Historyan
@Historyan 2 года назад
thanks!!
@tsivils1987
@tsivils1987 Год назад
You helped me a year later far more than you can know. I thank you. I don't know coding really but im trying to release a game by next week and you are part of the reason I'll be able to do that.
@andrewimmanuel2553
@andrewimmanuel2553 Год назад
thanks :D
@creationJeuxVideoETtuto
@creationJeuxVideoETtuto 9 месяцев назад
hello from french, dear friend dev. thank you so much for tuto, it helped me a lot ^^ End credits of my game, j will mentionne you and you youtube channel. thank you again.
@denn501
@denn501 2 года назад
taaaank you :3
@michelknorr4281
@michelknorr4281 Год назад
Thanks thank thanks! thatsa is the movement methode which works for my project i need, i tried 3-4 movement methods, all of them wasnt the right vor my vision for my game. Thanks!!!!! haha i cry actually xD i dont believe
@nikolaslegault3641
@nikolaslegault3641 4 года назад
Thanks
@3oriigingaming
@3oriigingaming 4 года назад
I haven't tried this yet but if I do, I think I'll eventually face an issue where the enemy will collide with other static objects instead of simply moving around them. Maybe using a NavMesh would be preferrable.
@bobgamernoobq4776
@bobgamernoobq4776 3 года назад
thx you
@silentminecrafter6234
@silentminecrafter6234 4 года назад
Thank you thank you thank u
@yograjjawle3294
@yograjjawle3294 4 года назад
Very good video. helped me a lot. Can you make a tutorial of damage in unity2D(topdown)?
@UdulaRatnayake
@UdulaRatnayake 3 года назад
Nice Explanation! Will this script help the enemies to avoid obstacles and follow the player?
@nikitapolskiy
@nikitapolskiy 3 года назад
The best
@blockido2151
@blockido2151 2 года назад
This is amazing! I only have square tho and it wont look at me correctly.
@chuk_udi
@chuk_udi 4 года назад
You should make a video on the particle system.
@bioman1hazard607
@bioman1hazard607 3 года назад
any way to apply this to a 3d game and can you use x and z rather then x and y?
@user-uz3hr6gf6d
@user-uz3hr6gf6d 4 месяца назад
Try this code at:3:47 Quaternion angle = Quaternion.Euler(direction.y, direction.x,0); rb.rotation = angle;
@sametayaz4891
@sametayaz4891 2 дня назад
2 minutes ago, an AI helper tool suggested me this video. Dude...
@dehnmagnus
@dehnmagnus 2 года назад
A tip. If you are using unity 3d u can just say transform.LookAt(player); You are welcome!
@latazz4908
@latazz4908 Год назад
i love you
@EricEwers
@EricEwers 5 лет назад
Excellent tutorial! How would I go about making this for a 3D object? I added a Rigidbody to my enemy object and changed Rigidbody2D to Rigidbody in the code. I am getting the error: " Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion' "
@pressstart6864
@pressstart6864 5 лет назад
A RigidBody2D.rotation uses a float value, but a RigidBody.rotation (3D) needs a Quaternion value. This might help: www.cuelogic.com/blog/how-to-rotate-object-in-unity-using-quaternion
@mandalapratama7907
@mandalapratama7907 4 года назад
Nice info thanks im from indonesia
@user-qd9jn5od7v
@user-qd9jn5od7v 4 года назад
simply use this code rb.rotation = Quaternion.Euler(0f,angle,0f);
@MrChocofreak
@MrChocofreak 3 года назад
@@user-qd9jn5od7v trying this in vr on a regular capsule, it disappears before its ever visible in headset. any explanation or help?
@camelusvolantes3326
@camelusvolantes3326 3 года назад
@@user-qd9jn5od7v Thx bro.
@sierz.stradtord7883
@sierz.stradtord7883 4 года назад
Hey, how would you make multiple enemies chasing the player, but the enemies not clumping up/overlapping ?
@AlphaLul
@AlphaLul 4 года назад
How do I make it so the speed isn't relative to the distance from the player?
@alejandrozamora3468
@alejandrozamora3468 4 года назад
How would I go about and get the enemy to shoot in the direction that it's facing?
@stephenhealy6367
@stephenhealy6367 3 года назад
How would you alter this code to work with sprites in a 2d side scrolling situation
@damirshabayev344
@damirshabayev344 3 года назад
can u make a vid with an enemy following the player 3d please.
@minatic1148
@minatic1148 Год назад
Alternitive code: in Class write this: public Transform target; in Update write following: Vector3 relative = transform.InverseTransformPoint(target.position); float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg; transform.Rotate(0, 0 , angle); Drag ur player into the target field of ur inspector and it should work!
@TechWrathTV
@TechWrathTV 3 года назад
In case of 3D top down shooter? what should be the change?
@MerrStudio
@MerrStudio 3 года назад
Why does it slow down when coming closer to the player?
@mrkensho3770
@mrkensho3770 Год назад
Hey guys I need help, when I apply the code it works perfectly fine with the enemy, but as soon as I make my enemy a prefab so I can spawn it as a wave they stop following my player. I tried to add the player to the transform player option, but for some reason it wont let me unless it is another prefab object any chance you guys can help me out.
@kpv8
@kpv8 4 года назад
Is it possible to make enemy to start move towards the player when the player is just in enemy let me name it "seeing range"?
@yuqzii
@yuqzii 4 года назад
I know im answering late but you can use Vector3.Distance(position a, position b). that will give you the distance between two objects
@felipethepro467
@felipethepro467 4 года назад
​@@yuqzii I tried this but it isnt working(sorry if its a obvious mistake im a beginner) edit: so the more than symbols where backwards..... anyways i found this video that does basically the same thing ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-xppompv1DBg.html (5:20)time
@alanb4098
@alanb4098 4 года назад
OnTriggerEnter2D
@creeperfreaker3910
@creeperfreaker3910 3 года назад
I didn't know you could go quicker than brackeys
@arc1607
@arc1607 4 года назад
Does anyone know how he made the spawner for the enemies? I’m trying to replicate it but the the enemy viewing the player then gets messed up
@aprimic
@aprimic 2 года назад
Why did you need to do physics 2d for rotation when you have it available in the transform?
@brangtoggez6363
@brangtoggez6363 3 года назад
can i ask what font is that ? it looks pretty cool
@AlejandroLetxu
@AlejandroLetxu 2 года назад
How can I make the enemy chase the main character but avoiding walls? Currently I can do it but he doesn't detect the shorter way to reach the main character, he stays blocked at the other side of the wall. Thanks in advance
@MajesticMindGames
@MajesticMindGames Год назад
So effing cool bro! Thanks for this awesome tutorial. I hope you go back to making videos. Let us support you, got any games out for us to check out?
@user-td7np3st4m
@user-td7np3st4m Год назад
There is a easy way to fix the enemy rotation just add + 90f; "float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90f;"
@justbrycefr
@justbrycefr Год назад
I don't really know if I have not copy and pasted something or done something wrong but when I press the start button in unity it says "all complier errors have to be fixed before you can enter playmode"
@rushawngillespie31
@rushawngillespie31 4 года назад
Is there anyway this can work in 2d that's not top down??
Далее
How to make a Video Game - Godot Beginner Tutorial
1:17:12
Cat Plays with Window Washer
00:22
Просмотров 1,7 млн
How to code SMARTER A.I. enemies | Unity Tutorial
32:49
2D PATHFINDING - Enemy AI in Unity
23:13
Просмотров 805 тыс.
A Perfect Jump in Unity - A Complete Guide
17:05
Просмотров 75 тыс.
Spawning Objects in Unity [Using Instantiate]
10:23
Просмотров 239 тыс.
Creating SMART enemies from scratch! | Devlog
5:40
Просмотров 283 тыс.
Cat Plays with Window Washer
00:22
Просмотров 1,7 млн