Unity 2D Tutorial How To Make Enemy Walk And Follow Along The Path Using Simple C# Script In a Game

  Рет қаралды 59,889

Alexander Zotov

Alexander Zotov

Күн бұрын

Пікірлер: 116
@otrocanalmas8096
@otrocanalmas8096 6 жыл бұрын
This is exactly what i want for a long time.... quick and usefull tutorials.. i've seen this in 24 minutes and even 45 minutes to explain this :) thank you
@AlexanderZotov
@AlexanderZotov 6 жыл бұрын
Happy to help. Thanks for your feedback!
@Searwitch
@Searwitch 2 жыл бұрын
Others would've milked this for 10-20 minutes. Thank you very much!
@random_precision_software
@random_precision_software 6 жыл бұрын
I love these quick and lots of tutorials, it's about time someone done a video everyday, and the content is what everybody wants. 👌😀👍
@Worlnof
@Worlnof 2 жыл бұрын
I really do LOVE you! You are my hero, I had to search soooo long to find someone explaining this to me
@bren.n.z.
@bren.n.z. Жыл бұрын
I have been searching for a tutorial like this for days and this is exactly I need. Awesome video
@01dragonix
@01dragonix 4 жыл бұрын
in case that is not working for you check that the Z position is the same on the Enemy and the Point
@leonholli7281
@leonholli7281 3 жыл бұрын
Saved my day
@anonymy834
@anonymy834 3 жыл бұрын
thank you! you saved my day! had been confused for ages why my sprite did not work. It came to my mind that someone in the comment area could comment on this. So I reviewed the comments one by one. You saved my day!
@7amdanae898
@7amdanae898 3 жыл бұрын
To add rotation if you use car as a gameobject: if (waypointIndex != waypoints.Length) { Vector3 targetInVec3 = new Vector3(waypoints[waypointIndex].transform.position.x, waypoints[waypointIndex].transform.position.y, 0f); Vector3 direction2 = targetInVec3 - transform.position; float angle = Mathf.Atan2(direction2.y, direction2.x) * Mathf.Rad2Deg - 90; Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation = rotation; transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, movespeed * Time.deltaTime);
@astrolabtechnologies
@astrolabtechnologies 2 жыл бұрын
thanks :)
@racmanov
@racmanov 3 жыл бұрын
works like a charm. had to work around some stuff coz i do 3d and use rigidbodies. But now it works flawlessly. ty
@michalstanislawbun5443
@michalstanislawbun5443 6 жыл бұрын
everyday a new and useful lesson! thanks a lot
@rjsiii3
@rjsiii3 4 жыл бұрын
One of those videos that takes something that seemed impossibly confusing and makes it feel like "duh well of course". Great job and i appreciate the shortness of the video, as much as the games of the idea. Liked and subscribed
@ilovecode3727
@ilovecode3727 2 жыл бұрын
so fast and clear. THX!!
@phpickles
@phpickles 4 жыл бұрын
Ty!!! (if is not working for you make sure your z coordinates on every waypoint is set to 0).
@beaverjoe9171
@beaverjoe9171 6 жыл бұрын
So Effective and convenient way for me! THANKS!
@lazzzzz_exe
@lazzzzz_exe 2 жыл бұрын
UnassignedReferenceException: The variable waypoints of FollowThePath has not been assigned. You probably need to assign the waypoints variable of the FollowThePath script in the inspector. help pls!!
@Parisphotogram96
@Parisphotogram96 3 жыл бұрын
thanks bro , i love what you sharing with us
@AlexanderZotov
@AlexanderZotov 3 жыл бұрын
Happy to help
@MichaelRyanNewell
@MichaelRyanNewell Күн бұрын
That’s how they do Sonic. You enter the loop, it enters a path, and you exit the loop.
@liudaxun
@liudaxun 3 жыл бұрын
Any idea why there's an error Index was outside the bounds of the array? when I tried running
@apoleyta9760
@apoleyta9760 4 жыл бұрын
I don't know why but ıt doesn't work for my game. I have done whatever you said. But my enemy doesn't walk. Just pop ups in the first point because of the codeline in the Start method
@geeayarrwhy
@geeayarrwhy 3 жыл бұрын
Did you find out why? I'm having the same issue. :/
@AlexanderZotov
@AlexanderZotov 3 жыл бұрын
Check out Z axis value. It should be zero.
@griseldapozo1405
@griseldapozo1405 3 жыл бұрын
@@geeayarrwhy I had the same issue, had all my Z's on 0, but I had my waypoints in a container and that had the Z on a different value, changed that and works now.
@vakarisgasiunas1867
@vakarisgasiunas1867 3 жыл бұрын
Hello, new to unity, is the any way to make it, that the enemy would start over after reaching the last checkpoint (for example: Waypoint 1 -> Waypoint 2 -> Waypoint 3 -> Waypoint 1 -> Waypoint 2 -> Waypoint 3...
@AlexanderZotov
@AlexanderZotov 3 жыл бұрын
I have video on that
@vakarisgasiunas1867
@vakarisgasiunas1867 3 жыл бұрын
@@AlexanderZotov would you be so kind to link it please?
@AlexanderZotov
@AlexanderZotov 3 жыл бұрын
@@vakarisgasiunas1867 Here kzbin.info/www/bejne/e6m1gnR7opmdhsk
@snowm561
@snowm561 Жыл бұрын
I only have one waypoint, because the enemy is simply walking straight across the screen. How do I make it so that they aren't instantly going to the waypoint but instead more slowly going to it?
@itsdonix
@itsdonix Жыл бұрын
how can i make it loop the movement path?
@marvicbautista4583
@marvicbautista4583 3 жыл бұрын
Hi again, when i make my enemy as a prefab a cant put the way points on the inspector i dont know what happen.
@Mamaco915
@Mamaco915 Жыл бұрын
Nice tutorial but for some reason the square doesnt move what is the problem ?
@SMGtk
@SMGtk 3 жыл бұрын
what if there are two options at a point? How will the object make the decision randomly
@mobilemon5032
@mobilemon5032 5 жыл бұрын
The object is moving for me but it moves really slow. I followed the tutorial exactly, what's wrong?
@blackstarstudios920
@blackstarstudios920 3 жыл бұрын
Its not letting me add my waypoints into the slot. Help!
@jamesesa501
@jamesesa501 4 жыл бұрын
why my Enemy is not moving anymore when I change the enemy to another place?
@gavinckw97
@gavinckw97 5 жыл бұрын
Hello there! May i know how do i make the object to rotate based on the path?
@IROC___Jeff
@IROC___Jeff 4 жыл бұрын
This only worked once for me. I remade my enemy and followed the video again and my new enemy just starts at the first waypoint and just sits there. No movement regardless the speed i set. The script was copied and pasted so there are no errors from my end. Any ideas?
@AlexanderZotov
@AlexanderZotov 4 жыл бұрын
Check Z coordinate. It should be zero.
@cristianquiroz4200
@cristianquiroz4200 4 жыл бұрын
For some reason the comparison between Vector3 fails. try this: private float distancia; private void Move() { if (waypointIndex
@howtogamedev_
@howtogamedev_ 3 жыл бұрын
it was really helpful, thank you so much
@marvicbautista4583
@marvicbautista4583 3 жыл бұрын
my sprite only goes to the first point and don't go to the next one I don't know whats the problem. I hope I can find some ways to fix this. Thankyou
@marvicbautista4583
@marvicbautista4583 3 жыл бұрын
I Changed the z to zero and it works, My error is my path is parented to an object which is positioned to -3 Thankyou!!
@janyiren8463
@janyiren8463 Жыл бұрын
@@marvicbautista4583 could you explain it to me please? Did you change smth in the code? Because every object is placed on z 0 but still not working(((
@anonymy834
@anonymy834 3 жыл бұрын
hi I edited the c# script and dragged it to the avatar. it gave me an error of NullReferenceException: Object reference not set to an instance of an object. Do you know what caused this error?
@songediter9101
@songediter9101 3 жыл бұрын
can you please help? i want my enemy to face the point when he follows it
@gamerel5028
@gamerel5028 11 ай бұрын
Thank you so much
@RatoCavernaBR
@RatoCavernaBR 6 жыл бұрын
There is any way to do it using bezier?
@XenosDGamer
@XenosDGamer 9 ай бұрын
Please make a video about how the enemy will change it's rotation according to the direction it's moving in
@Ghoqable
@Ghoqable 6 жыл бұрын
in Unity 2d can you make a tutorial where a character moves randomly but doesn't go through certain objects like walls? Without navmesh?
@sife-i9n
@sife-i9n 3 жыл бұрын
it's just follow one waypoint than stops
@Chief-wx1fj
@Chief-wx1fj 6 жыл бұрын
Enemy tutorials make me happy
@lordtoby7454
@lordtoby7454 5 жыл бұрын
This helped me very much A quick question: I want to make an enemy repeat the path he follows over and over. Do I just wrap the code into a loop?
@petipois28
@petipois28 4 жыл бұрын
If (waypoint == waypoint. Length - 1) Waypoint index = 0 This way it will start over again
@g1apefruit
@g1apefruit 4 жыл бұрын
@@petipois28 Where do i put this in the code
@petipois28
@petipois28 4 жыл бұрын
@@g1apefruit 1:03 inside the move function after the first if() statement
@beaverjoe9171
@beaverjoe9171 6 жыл бұрын
Long time not see you man~~~~
@AlexanderZotov
@AlexanderZotov 6 жыл бұрын
Been on vacation. Back again ;-)
@beaverjoe9171
@beaverjoe9171 6 жыл бұрын
sounds good ! you must very enjoyable travling. Looking forward your tutorials if you have enough time~
@ThatLazyGameDev
@ThatLazyGameDev 2 жыл бұрын
Nice Tutorial, but how can I make it loop? Like go back to point 1 and just repeat
@beaverjoe9171
@beaverjoe9171 6 жыл бұрын
!!!!!My pleasure! Hopes I can learn more from you!
@AlexanderZotov
@AlexanderZotov 6 жыл бұрын
I do my best for that :-)
@random_precision_software
@random_precision_software 4 жыл бұрын
Can you Change this so the player draws as line then an object follows along that line?
@dragontailsb1971
@dragontailsb1971 2 жыл бұрын
if it isn't working for you, change "transform.position = Vector2.MoveTowards..." to "transform.position = Vector3.MoveTowards..."
@sumkid9263
@sumkid9263 Жыл бұрын
clutch, this is the only thing that worked
@Madheim777
@Madheim777 Жыл бұрын
IT WORKED! thank you!!
@karislewis5192
@karislewis5192 5 ай бұрын
Thank you so much. Couldn’t figure out why z axis was changing even tho it was set to zero 😭
@dimitrishow_D
@dimitrishow_D 3 жыл бұрын
I tried this and tried to spawn a new enemy after it dies.. But the enemy prefab then doesnt have the waypoints anymore.. I tried adding the wavepoints to the prefab in prefab folder but that doesnt work i can only add them when is in hiërarchie.. Do you know how i can solve this... Have a enemy have waypoints and being able to spawn it?
@suryakumara3360
@suryakumara3360 4 жыл бұрын
Dear Alexander, how to make the dynosaur go back to the first waypoint ? Hope for your reply thanks
@AlexanderZotov
@AlexanderZotov 4 жыл бұрын
Here is the answer kzbin.info/www/bejne/e6m1gnR7opmdhsk
@ambarmajumdar4933
@ambarmajumdar4933 6 жыл бұрын
Sir when I try to make this enemy as a prefabs the the element in way points are not there why that happened
@JongHyunSM
@JongHyunSM 4 жыл бұрын
can you explain how to face idle with 8 directions after follow the path?
@siscoye9454
@siscoye9454 6 жыл бұрын
Had it working once or twice, went back to use the script, and again it wont pass waypoint one , no errors :(:(:(
@davidlandenkarlsson9983
@davidlandenkarlsson9983 5 жыл бұрын
@SuperNinja Had the same issue as the guy above. Tried putting the if statement above the other but it yielded the same result. Thing is that it works fin for one of my enemies, but if I change waypoint (as is the case for my second enemy) it just stops working. Both enemies and paths are put up the same way, but one enemy works, not the other.
@unbounceexpert5487
@unbounceexpert5487 Жыл бұрын
How to make waypoint in loop ?
@prevratnik
@prevratnik 6 жыл бұрын
It's shortest and greatest tut
@Bes2TheFort
@Bes2TheFort 6 жыл бұрын
What do I change, if I want it to continue without stopping?
@ZackRing
@ZackRing 6 жыл бұрын
Its in another one of his videos. kzbin.info/www/bejne/e6m1gnR7opmdhsk
@gabrielalbu9134
@gabrielalbu9134 6 жыл бұрын
will this work for 3d if i change vector2 to vector 3
@jonsmith2832
@jonsmith2832 4 жыл бұрын
I know this is late but, yes it does.
@Osoclever
@Osoclever 8 ай бұрын
Thank you
@alexandertuzovskiy485
@alexandertuzovskiy485 6 жыл бұрын
Thank you very much! Very good and usefull :) I have one question. You have a very interesting tutorial with dice. And I'm tring to use it. For example, you drop the dice, got a number, and how to make your character go that number waypoints?
@siscoye9454
@siscoye9454 6 жыл бұрын
Some reason mine never goes past waypoint one Followed the tut to a TEEE , no errors in code, waypoints in inspector set up :(
@siscoye9454
@siscoye9454 6 жыл бұрын
didnt put capital W on waypoints object :( spent 5 hours doing it over n over lol
@sharif47
@sharif47 6 жыл бұрын
I need some help with adding some features to this. 1. I'm working on a 3D environment and I want the y-axis value to be determined by gravity. 2. I'm working on a car AI. So, whenever it faces a turn, it should at least rotate in order to make the turning look believable. Because the path is created by finding the shortest path based on a grid system, the adjacent points are the center of two adjacent squares in the grid. So, the distance between two adjacent points are either 1(for horizontal/vertical) or √2 (for diagonal). See if this info can help in making easier code for rotating.
@turoreal
@turoreal 4 жыл бұрын
Hi! thank you, i combined it with a scriptable object in order to make it portable and more flexible.
@danielfernandobaquerorojas2141
@danielfernandobaquerorojas2141 4 жыл бұрын
ThankYou!
@AlexanderZotov
@AlexanderZotov 4 жыл бұрын
Happy to help
@miguelmonreal.8874
@miguelmonreal.8874 4 жыл бұрын
This is great. Can you show how you would set this up for 2 different paths and have it randomly choose which path to take?
@suryanshrana838
@suryanshrana838 3 жыл бұрын
you could give it two lists and make randomly choose which list of waypoints its going to follow
@monika-nowakowska
@monika-nowakowska 6 жыл бұрын
Thanks! I love your lessons - short form is really cool! If you'll have some time, can you do tutorial about player bouncing from the wall (tower platformer)? You can check out example of what I have in mind in Icy Tower. Thank you in advance!
@8978martin
@8978martin 6 жыл бұрын
good tutorial
@erinhaingagatun7062
@erinhaingagatun7062 4 жыл бұрын
Добрый день, а как сделать что бы наш Enemy рандом выбирал путь, когда их больше одного?
@abdurrehmankhan1699
@abdurrehmankhan1699 6 жыл бұрын
How to add pong effect in this script
@gamoe
@gamoe 2 жыл бұрын
thanks you
@aonmuhammad4305
@aonmuhammad4305 6 жыл бұрын
thanx!!!!
@kyleplair4929
@kyleplair4929 2 жыл бұрын
Could someone paste the script?
@mulagraphics
@mulagraphics 6 жыл бұрын
Please make a video on how to give other players - Coin, Items, Weapons
@bashirmanafikhi
@bashirmanafikhi 6 жыл бұрын
THAAANK YOOU
@personnotfound5007
@personnotfound5007 4 жыл бұрын
I can't figure out how add the script to the enemy, could someone help?
@nmb86
@nmb86 4 жыл бұрын
Hello :) click on the enemy in the scene window, then under inspector you will see all the components. now, just find your script file in the project directory and drag it to the inspector :)
@KoolakStudio
@KoolakStudio 6 жыл бұрын
really thanks man really thanks . how one object can play different sound with one audio source
@AlexanderZotov
@AlexanderZotov 6 жыл бұрын
I have such tutorial already. Please check it out kzbin.info/www/bejne/bqGpnaGvm7eZiMU
@dharmeshkumar2763
@dharmeshkumar2763 6 жыл бұрын
Waoo nice method
@wearwolf4202
@wearwolf4202 3 жыл бұрын
Maybe if you did a code tutorial for this maybe
@AlexanderZotov
@AlexanderZotov 3 жыл бұрын
What?
@skywoker123456
@skywoker123456 6 жыл бұрын
Очень хорошие у тебя уроки, сделай пожалуйста видео, как делать автосохранение и загрузку в игре.
@cristianquiroz4200
@cristianquiroz4200 4 жыл бұрын
For some reason the comparison between Vector3 fails. try this: private float distancia; private void Move() { if (waypointIndex
@MahmoudTammamJake
@MahmoudTammamJake Ай бұрын
thank you so so much
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
2D PATHFINDING - Enemy AI in Unity
23:13
Brackeys
Рет қаралды 827 М.
PATROL AI WITH UNITY AND C# - EASY TUTORIAL
9:29
Blackthornprod
Рет қаралды 156 М.
TOUCH CONTROLS in Unity!
16:09
Brackeys
Рет қаралды 1,3 МЛН
2D PLATFORMER PATROL AI WITH UNITY AND C# - EASY TUTORIAL
7:31
Blackthornprod
Рет қаралды 158 М.
SHOOTING/FOLLOW/RETREAT ENEMY AI WITH UNITY AND C# - EASY TUTORIAL
12:55
ADVANCED AI IN UNITY (Made EASY) - STATE MACHINE BEHAVIORS
8:15
Blackthornprod
Рет қаралды 238 М.
SETTINGS MENU in Unity
17:22
Brackeys
Рет қаралды 958 М.
TOP DOWN MOVEMENT in Unity!
22:30
Brackeys
Рет қаралды 1,2 МЛН
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН