Тёмный

How to Create an ENEMY In Godot 4 

DevWorm
Подписаться 28 тыс.
Просмотров 75 тыс.
50% 1

In this video we are going to go over how to create an enemy in godot 4, An enemy is one of the most important things in a game, if you dont have smooth good working enemys then the game is almost unplayable. So i made sure to go over exactly how to make the best enemy in godot.
Download Free art pack: game-endeavor.itch.io/mystic-...
make sure to download the free as that is all we will need for this series.
Link RPG Godot 4 series playlist: • How to Make an RPG in ...
----------------------------------------------------------------------------------------------------------------------------------------
Thank you so much for watching I really hope this video helped.
if you did enjoy then please go and click that subscribe button to help out the channel. I means so much and I love your feedback in the comments to let me know what it is that you enjoyed. Again thanks so much and I would love to see you again!
have a great rest of you day and of course be safe :)
- thanks DevWorm,
----------------------------------------------------------------------------------------------------------------------------------------

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

 

14 апр 2023

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 328   
@juansalvadordomandl5287
@juansalvadordomandl5287 Год назад
Excellent! The enemy wasn't colliding with the tiles, i added "move_and_collide" in: func _physics_process(delta): if player_chase: ... move_and_collide(Vector2(0,0)) ... And now it works :).
@kafiaahmed469
@kafiaahmed469 Год назад
thank you!
@polyspastos
@polyspastos 11 месяцев назад
great, now i outmaneuvre them only to forget that theyre behind a tree ^^
@carsongreen9838
@carsongreen9838 11 месяцев назад
thank you so much
@furkanaldemir9292
@furkanaldemir9292 11 месяцев назад
thanks
@SAMURAI_l58
@SAMURAI_l58 10 месяцев назад
thank you bro
@Akamo.
@Akamo. 5 месяцев назад
Nice tutorial. The movement is handled better this way: velocity = (player.get_global_position() - position).normalized() * movementSpeed * delta move_and_collide(velocity) with the velocity vector you can make the enemy slow down gradually after you leave the "aggro range". For that use lerp. velocity = lerp(velocity, Vector2.ZERO, 0.07) so in the end you'd have something like this: if player_chase: velocity = (player.get_global_position() - position).normalized() * movementSpeed * delta else: velocity = lerp(velocity, Vector2.ZERO, 0.07) move_and_collide(velocity) 0.07 being the rate of slowing down. Adjust to your needs. 1.0 is stopping instantly.
@Raspikabekk
@Raspikabekk 5 месяцев назад
on top of this. I was thinking to add a _ready() function to store the initial position of the enemy, so when the player gets out of range, to go back to their original position at a lower speed at some point. Maybe not immediately, but once out of range, you can add some additional animation to wait a few seconds, then go back.
@Akamo.
@Akamo. 5 месяцев назад
@@Raspikabekk that is a fire idea
@albert69Einstein
@albert69Einstein Месяц назад
@@Raspikabekk I did that. I called it the enemy's "rally_point". but yes good idea !!!
@NikaDolidze-rg2xy
@NikaDolidze-rg2xy Месяц назад
Can i use a move_toward instead of the lerp for decelaration?
@johnwicket888
@johnwicket888 Месяц назад
can anyone explain to me what is happening here
@dev-worm
@dev-worm Год назад
link to the full series: ru-vid.com/group/PL3cGrGHvkwn0zoGLoGorwvGj6dHCjLaGd
@Fork501
@Fork501 11 месяцев назад
The flip_h logic can be shortened: $AnimatedSprite2D.flip_h = (position > player.position)
@ayaanthetechnoguy
@ayaanthetechnoguy 9 дней назад
I am a child self learner of coding and this is been a great and easy kick start for me
@MasterNoda83
@MasterNoda83 5 месяцев назад
Possibly the most helpful tutorial on Godot I've come across. The way you work through it out loud as you're coding and setting things up made things click for me in a way that other tutorials have failed to do.
@keithestanol3771
@keithestanol3771 8 месяцев назад
This one was also difficult for me, but eventually got it all sorted out, collisions are working, y-sorting is working for both player and enemy, and enemy follow code is working perfectly. Phew! Heading to the combat system, which I know will be funsies.
@tomoyaokazaki2138
@tomoyaokazaki2138 2 месяца назад
These tutorials of yours have been working perfectly for me so far! I can't wait to follow along with the combat system next. Thank you so much for the amazing videos!
@dev-worm
@dev-worm 2 месяца назад
thank you so much!! I'm so happy to see you moving through the tutorials quickly!! hoping the rest prove to be helpful!
@lucius4hard
@lucius4hard 6 месяцев назад
To properly chase a player at a constant speed, I can suggest this option: if player_chase: position += (player.position - position).normalized() * speed * delta move_and_collide(Vector2(0,0)) Also, adding - move_and_collide(Vector2(0,0)) solves the problem of the enemy not colliding with objects.
@dev-worm
@dev-worm 6 месяцев назад
ah yes this is so much better, thank you!
@fog1
@fog1 6 месяцев назад
how do u make the enemy path around terrain
@WeirdBrainGoo
@WeirdBrainGoo 4 месяца назад
This was super helpful. You can also just use the move_and_slide() function which doesn't require a parameter.
@deadjuice1880
@deadjuice1880 2 месяца назад
​@@WeirdBrainGoomove and slide works nicer, but it causes issues with players and enemies getting stuck to each other, since the player uses it too
@olxm33
@olxm33 11 месяцев назад
fire series im grindin it all day
@richardwolfe2089
@richardwolfe2089 Год назад
since the enemy is a CharacterBody2d, instead of modifying the position directly, you can use velocity and the move_and_slide() function. This way, you wouldn't need to divide the position by the speed. (I think raising the speed value to make the enemy slower is a bit counterintuitive.) Also, I might be wrong, but I think if you don't multiply speed by delta, the enemy will move faster or slower based on framerate? Thank you for putting in the time to make this series. As someone new to Godot 4 I am really enjoying it!
@gasparquintana4622
@gasparquintana4622 Год назад
Agreed. I also hated the fact enemies were slower closer to the player and faster when further away. Anyone wondering how to do it just use this.- if player_chase: velocity = position.direction_to(player.position) * speed move_and_slide()
@voidling2632
@voidling2632 Год назад
@@gasparquintana4622 ​ you can't do that, velocity is a vector and position.direction_to returns a float, not a vector.
@nika_251
@nika_251 Год назад
@@voidling2632 I managed to get it working by setting the direction of the player as the normalized vector of the difference in their position, and then setting its velocity to that times the speed func _physics_process(delta): if chaseplayer: var direction = (player.position-position).normalized() velocity=direction*speed move_and_slide()
@moritzmuller4222
@moritzmuller4222 9 месяцев назад
@@voidling2632 it does not return a float? how could a float represent a direction
@rompevuevitos222
@rompevuevitos222 6 месяцев назад
@@moritzmuller4222They probably confused it with "distance_to"
@MetaDataAutomation
@MetaDataAutomation 4 месяца назад
Thanks for the guides mate, I think you do a great job! This game coming together looks great. If I could just give some positive feedback - You do a fairly good job at explaining some things, but I think it would be good to go into a little more detail within the code and why we are doing some things - for example moving our player and enemy to -15 Y axis. You say this is for the Ysorting, but we also turned on Y sorting within all the menus, so would be good to have that extra detail on what that page movement does. There's a few areas like this - One other bit would be the code iteslf. It seems like a lot of the time we aren't using the best practices or not using the best code. You can see even when you are testing the gameplay the the slime gets faster when you move further, and slows down when it gets close. In our gameplay controller we also use heaps of lines of code for a really simple controller. You seem to know what you're talking about though so it would be good to talk about your reasoning for these decisions. I'll also note that when you play the game with this code used, there are bugs like the slime being attached to the player if you collide correctly and if you run into the slime top down you will also get stuck and autowalk pushing the slime downwards and off the map. I GREATLY appreciate the fantastic guides, my only fears are things being missed and potentially not best practices used sometimes. Thanks again and look forward to the rest of the series.
@mihirparab2172
@mihirparab2172 Год назад
Thanks man.. excited for the next one!
@dev-worm
@dev-worm Год назад
coming in a couple hours!
@prismajane
@prismajane 8 месяцев назад
This is the funniest tutorial I've seen, but also insanely helpful.
@dev-worm
@dev-worm 7 месяцев назад
glad it was helpful at least lol
@supinboh4399
@supinboh4399 21 день назад
Hi! thank you so much for this tutorial, it saved my nooby butt and fortunately you explain everything instead of just telling us to copy and paste, so congrats man! The only thing that didn´t help me was the fact the enemy´s velocity changes depending on the difference between the position of the player and the enemy position, and for the game I´m trying to make it would feel odd. In case anyone needs the enemy to move with a constant velocity I found a way to do it. If anyone reads this and has a better solution or sees any error, please let me know! I started programming 2 weeks ago so I´m sure there are better ways to do this. I just mixed how I learned to move my player with the things I learned in this fantastic tutorial. The code would go like this: func _physics_process(_delta): if player_chase == true: var direction = Vector2(player.position - position) #gets the direction the enemy needs to move to reach the player var normal_direction = direction / direction.length() #normalizes the vector (makes the magnitude or length of the vector always the same while mantaining its direction) velocity = normal_direction * speed #multiplies the normalized vector or direction and the speed chosen move_and_slide() #method used to move a character depending on the velocity
@9sufian
@9sufian 10 дней назад
Hi there! Another person commented something similar, you could try checking that out. But just to point a couple of things out, instead of normalizing the vector manually you can use direction.normalized(). Also be sure to multiply the velocity by delta so it's independent of the game's frame rate (which could vary between different computers). I suggest looking up how delta works, it's an important concept! (Sorry for not providing links, I'm currently writing this on my phone so it's a bit tricky to look it up myself and paste it here)
@1997AlexB
@1997AlexB 5 месяцев назад
Thats a really nice song you have playing in the background.
@DrW1ne
@DrW1ne Год назад
I didnt expected such a good tutorial, thx dude, keep it up!
@dev-worm
@dev-worm Год назад
thanks bro, glad i could help
@MaddMennn
@MaddMennn Год назад
Love your Videos! They are super helpful, and you are incredibly chill. If anything, all I would recommend is perhaps a prepared script if you do not already do that. Thank you and keep up the good work.
@dev-worm
@dev-worm Год назад
will take the advice thank you.
@AutMouseLabs
@AutMouseLabs 11 месяцев назад
This is rad stuff man. Thank you for doing these.
@dev-worm
@dev-worm 11 месяцев назад
Glad I could help, anytime, if you need any help then let me know :D
@crocherapia_
@crocherapia_ 20 дней назад
Thank you so much, 😃I love how it's simple and logic to put the animated enemy sprite chasing the player and it's fun also. Though I'm still a little confused about offset, I will study about it in Godot docs.
@dev-worm
@dev-worm 17 дней назад
thank you!! so offset basically change the position of the node without changing the position!! so basically if we set offset x to 10 as our position is 0,0 then… our position will stay 0,0 but our sprite will appear at 0,10 within the scene!
@crocherapia_
@crocherapia_ 17 дней назад
@@dev-worm Thank you so much for taking the time to reply my comment and question😀😀😀🥰🥰 I get ot now🥰🥰
@protobeing3999
@protobeing3999 7 месяцев назад
hands down best godot 4 tutorials on youtube
@dev-worm
@dev-worm 7 месяцев назад
so glad to hear that!! thank you it means the world. Im so glad Im able to help you out.
@Ale_B_
@Ale_B_ 8 месяцев назад
thanks i saw 100 tutorial video and only you have the solution to my problem thanks thanks thanks!!!
@dev-worm
@dev-worm 7 месяцев назад
So glad I could help!
@magicalnoodles
@magicalnoodles 3 месяца назад
Thanks for the tutorial DevWorm! It's really easy to digest and follow along with. One thing I noticed with this movement script is that the enemy doesn't have a constant speed at all. It becomes a lot slower when it gets close to the player. In this case, such a behavior is helpful, but it may not work for other enemy types.
@dev-worm
@dev-worm 3 месяца назад
you are completely right! thanks for pointing that out.. i shouldve made that a little more clear and went over the quick pre-build functions to keep it consistent
@cbjaxx1413
@cbjaxx1413 Год назад
Yes! You make the best tutorials. They are easy to follow and understandable. Can you make a Main Menu/ Pause Menu tutorial next?
@dev-worm
@dev-worm Год назад
will work on it for a future tutorial
@cbjaxx1413
@cbjaxx1413 Год назад
@@dev-worm Much appreciated.
@girasgames
@girasgames 8 месяцев назад
Main menu is easy actually, just make a new scene, go into project settings and make it the main scene, and then add a button that changes to the scene you want next.
@AshifKhan-sn6jx
@AshifKhan-sn6jx Год назад
Very simple and clever way to make an enemy
@keltzy
@keltzy 2 месяца назад
Hello, I was using this video to create an npc companion for my player, and it's been incredibly helpful! I did have two questions though if you're able to help. The first: My npc is always in chase mode, which means her running animation is always playing, even if the player is standing still. Is there a good way to detect if the player is standing still, to trigger the idle animation, as well as make sure the npc is facing the player? The second: My player is pretty tall, but her hitbox is in her feet, so the npc ends up with the player walking on her head (at least, I think the hitbox is what's causing the problem). Is there a good way to move the npc up the y axis so she's walking next to the player instead?
@MB_Arssin
@MB_Arssin 11 месяцев назад
Great tutorial! :)
@DylanDev14
@DylanDev14 9 месяцев назад
Thank you for this tutorial! As a beginner to game dev itself, this tutorial series is the only one that has felt truly easy to understand, from you explaining the logic to the syntax, everything is very digestible. Definitely going to recommend this to any friends that want to try Godot. Keep up the amazing work! Also thanks to anyone posting comments, the extra solutions and explanations are really helpful.
@Clendy321
@Clendy321 Год назад
Nice job
@thegamechallenger9120
@thegamechallenger9120 2 месяца назад
Underrated tutorial
@InconnuO_O491
@InconnuO_O491 Год назад
This tutorial is really good 👍, it's nice for once to see a tuto's fonctionng easily
@dev-worm
@dev-worm Год назад
Glad it was helpful!
@InconnuO_O491
@InconnuO_O491 Год назад
@@dev-worm Thanks to you for making this tutorial, but am I the only one who have a problem with the delta, when I enter the enemy's zone, the game suddenly crash, do you have any solution for this. Thanx
@Arkelo_CG
@Arkelo_CG 7 месяцев назад
thankyou sir!
@AndILostAll
@AndILostAll 10 месяцев назад
Hi, it works great, but there is one problem, if you add the aggression function when hitting from far away, then the enemy quickly teleports to the player, when the speed changes, nothing much changes...
@LD-dt1sk
@LD-dt1sk Месяц назад
When he added the movement something just died in me. Not only did he divide by speed instead of multiplying, but he also made the enemy accelerate the further it gets from player
@timemachine5678
@timemachine5678 4 месяца назад
Very simple and effective way to do this! however I've been facing one issue for a while this worked completely as intended until i had to resize everything including both the player and the enemy, and ever since I've noticed 2 key things: 1. When the enemy is specifically larger than the player it will never stop chasing once entered its collision shape, No matter the direction its chasing the player 2. If the Player is to the right of the enemy, the enemy will chase the player but only to a certain amount. if the player goes towards the enemy, it will move away from the player(This is when the enemy is the same size or relatively similar in size to the player). While i don't believe this is an issue with the script, if anyone has had any experience similar to this or may have any sort of idea of what may be causing this please let me know!
@johnwicket888
@johnwicket888 Месяц назад
this is awesome
@dev-worm
@dev-worm Месяц назад
thanks!! glad it helped!
@van.nevar47
@van.nevar47 Год назад
This video works great and I can make the enemy move and chase the player. There's just one problem with my program, the enemy's collision does not stop when it hits my tilemap collision. So it can basically go through the walls like a ghost.(I have two collision nodes, 1 for chasing and one that's supposed to stop the enemy when it hits a wall)
@dev-worm
@dev-worm Год назад
make sure the enemys actullal collison shape is on the same layer as the tilemap in the collision shapes setting to make sure it can interacted with it
@itsaboy213
@itsaboy213 Год назад
add move_and_collide(Vector2.ZERO) to the enemy script in the _physics function
@legendaryredfox
@legendaryredfox Год назад
@@itsaboy213 Thanks!
@itsaboy213
@itsaboy213 Год назад
@@legendaryredfox fyi I changed enemies to be move_and_collide(velocity * delta), can't remember why but there was definitely a reason for it.
@protobeing3999
@protobeing3999 7 месяцев назад
this@@itsaboy213
@saintoz
@saintoz 7 месяцев назад
say that the video is good is an abuse, the video is excellent. thank you!!!
@rohaanpatel
@rohaanpatel 4 месяца назад
If the enemy is heading to the left when the game starts, it's because it's heading towards the TileMap node instead. On the player scene, under collision, set Layer to 2 On the enemy scene, on detection_area under collision, set Mask to 2 only (Also, on the world scene, click on Tilemap > TileSet button on the right > Physics Layers, make sure Layer is set to 1 only)
@deadjuice1880
@deadjuice1880 2 месяца назад
Appreciate it! I've gone through this project 3 times, current one on my laptop with Devworm's smoother movement controls. Damn enemy was flying across the scene until it's detection-range was triggered, and then acted normal. All the code was right, and was about to go back through all the layers of everything. Thanks for saving me time!
@user-if7fv7sp7f
@user-if7fv7sp7f Месяц назад
I did that but its stil going left
@shanedrawsart5631
@shanedrawsart5631 28 дней назад
I tried your extra tip but my enemy isnt moving at all...
@laserstarr
@laserstarr 5 месяцев назад
Im following along and I have the enemy coming to me as it should. but it always positions itself to the top of me instead of the direction it came from. how would I fix this?
@SoyGoncash
@SoyGoncash 5 месяцев назад
My enemy is detecting a tileset in "_on_detection_area_body_entered", does anyone know how to avoid this and only make it chase the player?
@tampinhaiv7083
@tampinhaiv7083 6 месяцев назад
Thanks! U turn my life easyer
@rhevoramirez7969
@rhevoramirez7969 5 месяцев назад
thank you
@dev-worm
@dev-worm 5 месяцев назад
of course anytime!
@ChasingYourCreams
@ChasingYourCreams Месяц назад
Hey I'm having trouble, at 4:25. I'm not sure if there's been an update since then but when I click Collision under CharacterBody 2D the only thing that drop down is "Safe Margin" setting. I don't have the layer and mask options you do.
@Vethras
@Vethras Месяц назад
hey, had the same problem, i think because of the new update, my solution was to go into player scene and on the right under CollisionObject2D set collision there for layers 1 and 2, it seemed to solve my issue with it
@sirmrman1
@sirmrman1 Год назад
Hi, great tutorial, but i do have a question: My enemies only seem to be attracted to the game level itself, they all just follow where the game level is, not the player
@bigal115
@bigal115 5 месяцев назад
I had the same issue. It has to do with the collision layers.
@jphilpsbox8686
@jphilpsbox8686 Месяц назад
why does ysort works on the enemy even tho we didnt tick the ysort checkbox in the enemy ordering ?
@Penguin_merchant
@Penguin_merchant Год назад
can you make a tutorial on how to do platformer controls in Godot 4 it would be really helpful
@boanediggadukabberst5380
@boanediggadukabberst5380 2 месяца назад
thank you without tutorials to the newest version many people including me have no chance to learn this in freetime
@dev-worm
@dev-worm 2 месяца назад
so happy to hear! if you ever need help with anything then please let me know!
@boanediggadukabberst5380
@boanediggadukabberst5380 2 месяца назад
​Thank you ​@@dev-worm
@boanediggadukabberst5380
@boanediggadukabberst5380 2 месяца назад
​@@dev-wormi have a little question how to make a death Animation we never used it the Project i know $animatedsprite2d.play(death) but you need a Timer for this
@chrisreid9614
@chrisreid9614 4 месяца назад
when i added 2 enemy's they would just go into each other when chasing the player any ideas on how to fix
@itsaboy213
@itsaboy213 Год назад
I have a problem where if I duplicate enemies the attack animations I set for them only run with the original enemy. Something I noticed is if the original enemy and a duplicate have both detected and are chasing the player, if the duplicate gets in range of where the attack animation should play but the original hasn't, the original enemy will play the attack animation. It's like the detection zone is linked between the enemies. Everything else doesn't seemed to be linked, IE health, hitboxes, etc.
@chrisreid9614
@chrisreid9614 4 месяца назад
i have the same problem
@Lestuvato
@Lestuvato 10 месяцев назад
Whenever I enter my enemy’s collision shape the detection connection thingy in the script makes my enemy get pushed backwards (away from me)
@joshelguapo5563
@joshelguapo5563 7 месяцев назад
It's been a couple months so I assume you figured something out, but if not, I'd use an area 2D instead of a character body 2D. Area 2d has a collision shape but does not interact with the physics engine.
@polyspastos
@polyspastos 11 месяцев назад
i shouldnt have laughed that hard when youve made the integer value of speed lower instead of higher, sorry. and thank you so much for these amazing tutorials!
@eggoSG123
@eggoSG123 5 месяцев назад
i don't get it, why didn't we put the enemy on detection layer 1 itself, since the enemy can detect only on layer 2 but the player is on layer 1 how is it being detected, and what is the difference between player collision and collisionshape_2D which didn't he change the layer in collisionshape_2D. and since the layer of the player is changed to 2 as well he should be able to walk over the cliffs as well.
@kylestyle2202
@kylestyle2202 Месяц назад
Much easier way to do the facing: I made a function called direction() if velocity.x < 0: animated_sprite.flip_h = true else: animated_sprite.flip_h = false he'll automatically flip his direction based on his velocity instead of having to calculate everything again.
@dasuper11
@dasuper11 4 месяца назад
If anyone has the issue an issue where the slime won't move at all, make sure you are using the "body entered/exited" nodes and not the "area entered/exited" ones, was confused for a while on that mistake lol
@dei8ht2570
@dei8ht2570 3 месяца назад
thaks bro you'te beast
@dev-worm
@dev-worm 3 месяца назад
thank brother means the world!
@alexthelizard6394
@alexthelizard6394 2 месяца назад
my player_chase variable always starts as true but I wanted to start as false I already trid calling it in a ready function but it didnt work recommendations will be greatly appreciated
@Historymadefun89
@Historymadefun89 Месяц назад
Brother my enemy lags a lot, it would freeze abruptly and stop following the player. please tell how to fix this (btw nice video)
@GEMASUDEVASU
@GEMASUDEVASU Год назад
Will You Make In Game Keymaps/Joystick/Skill Button Too?
@themageofspace5516
@themageofspace5516 3 месяца назад
I keep running into this problem its really strange. my slime when it decects the player will sometimes teleport to the top left of the character
@Figstuck
@Figstuck 2 месяца назад
for some reason everytime i run, an error pops saying that base string "position" doesn't exist and it keeps crashing
@user-qw4lh4dd9x
@user-qw4lh4dd9x 6 месяцев назад
How do we go about creating NPCs; not hostile but just walking around enjoying themselves?
@mathislalonde353
@mathislalonde353 4 месяца назад
i want to know too !
@user-kq4ph6un9x
@user-kq4ph6un9x 27 дней назад
It's possible to copy the enemy behavior shown in this, except: don't make them hostile to the player nor give damage when touched
@TheVIcToR9634
@TheVIcToR9634 7 месяцев назад
I wanted to also have an animation to both front and back walking for the enemy, tried with a " position.y - position.y" but did not work out, any suggestions?
@rompevuevitos222
@rompevuevitos222 6 месяцев назад
I'm not sure what "position.y - position.y" is supposed to do? You're not modifying anything with that.
@zealousend
@zealousend 6 месяцев назад
you ever figure this one out? I'm tryna do the same
@necrox7gaming186
@necrox7gaming186 6 месяцев назад
I don't know what's wrong with me because I copy it exactly, but the enemy jumps at me diagonally from the side and from about three times the distance. what's wrong??
@Milham-hg7hf
@Milham-hg7hf Месяц назад
if you have an issue about enemy dont move towards player, check again in player and slime offset
@royuchiha8297
@royuchiha8297 Месяц назад
i tried this and its working fine. however, my slime idk why seems to get attached towards the origin.
@miss.antidote
@miss.antidote 6 месяцев назад
You wont believe it, but I am ready for part 4
@dev-worm
@dev-worm 6 месяцев назад
Ive saw all your comments lol... I hope you soon comment "I made it to part 9"!!!
@miss.antidote
@miss.antidote 6 месяцев назад
@@dev-worm going to try! Thanks for sharing you knowledge.
@brandoncarbaugh7994
@brandoncarbaugh7994 9 месяцев назад
One of the things I found most confusing when coming to Godot from other engines, which a lot of tutorials don't seem to clarify, is a key point of how GDScript functions. Which is: linking a variable to a game object doesn't just assign your variable that object's values. It creates an active reference, and anything you do to that variable, gets done to that object. (As opposed to something like Unity, where you have to use special dedicated commands to access game objects from within another object's script.) So when you do like "var cheese = body", where body is an argument being passed into a function from a collider node, you have now linked "cheese" and that game object. And cheese now has all the properties of that game object under it. If you do "cheese.x += 1", it will pass that command through to the game object you linked. Majorly different way of thinking for me. Much easier, once I got my head around it, but a huge shift.
@deadjuice1880
@deadjuice1880 3 месяца назад
9:13 If your enemy isn't moving at all, makes sure it's instantiated on world, and not tile map. I spent hours trying to find out what wasn't working, quit for the day, came back this morning, deleted enemy node, went through the whole process again, for it to be this... T_T
@dev-worm
@dev-worm 3 месяца назад
aww I feel that pain.. Im sorry.. glad you got it working now though!
@giuseppe-jr7sz
@giuseppe-jr7sz 11 месяцев назад
when i start instead of staying idle it comes flying on me any idea how to resolve?
@CastedHearts
@CastedHearts 7 месяцев назад
This tutorial was very helpful but i ran into (1) issue. i followed the video and code exactly but for some reason when i enter the slime's detection range it goes straight to the left and runs away from the player every time the collision bubble is entered, idk how to fix plz help
@frikabg
@frikabg 7 месяцев назад
I had the same issue... he talked about the issue a little bit at 5:17 basically you have to go to AnimatedSprite2D-> Offset it until you got it right and move the detection and collision area properly(center of the slime) for me i had to offset it with x=8 px y=-28 px
@MijmerMopper
@MijmerMopper 5 месяцев назад
I don't understand the why or how, but I replaced the player_chase code with something I found on the Godot forum and now it works. :if player_chase: var velocity = global_position.direction_to(player.global_position) move_and_collide(velocity * speed * delta)
@BulletTime1
@BulletTime1 8 месяцев назад
idk why, but when i start the game, my enemy moves fast twords the top left cornre, and i can't figure out why
@sjwarialaw8155
@sjwarialaw8155 2 месяца назад
same here, not sure why.
@chutney1223
@chutney1223 7 месяцев назад
I'm running into an issue that when the player bumps into the enemy from below, the enemy seems to become "stuck" to the player and will move anywhere the player goes at the player speed. Any ideas what is causing the issue?
@tofuryu3567
@tofuryu3567 5 месяцев назад
fix the collision shape
@josephdutton4138
@josephdutton4138 4 месяца назад
did you find a solution to this?
@user-db7cq2cw6p
@user-db7cq2cw6p 4 месяца назад
I'm having an issue were the enemy runs form the player. any fixes?
@fenix1sabean326
@fenix1sabean326 9 дней назад
I’m also wondering
@WildeCart
@WildeCart 8 дней назад
@@fenix1sabean326 and to the person your responding to, go to your Player scene, and ensure that the cross hair/ + icon is over the player sprite and collision shape. In this video when he goes to edit that, he kind of got tripped up by the UI and glosses over this important note, but if you don't center the character on that scene it will have a large offset, meaning your character on screen and the true location may not really line up. So when you see the enemy "run away" from you it could be they are actually running to your true location.
@bednarczykp
@bednarczykp Год назад
I followed your video and somehow enemy is not following me. When I enter collision area enemy is not moving at all. Do you have any idea what is potential issue
@MrSevenEleven
@MrSevenEleven Год назад
make sure you go to the "node" tab (next to the inspector) of the Area2D node, right-click "body_entered" and "body_exited", and click "connect" Just typing it out isn't enough, you have to actually enable the connection as well. connect it to your enemy script and you should be good
@-starrysunrise-2908
@-starrysunrise-2908 11 месяцев назад
Help! My slime doesn't stop chasing me when I leave the "detection_area"! My code looks identical to yours. Does anyone here know how to help? Edit: turns out I accidentally _typed out the words_ "func _on_detection_area_body_exited(body):" instead of using the actual node! I don't know how that happened, but it's fixed =)
@saintdark1
@saintdark1 Год назад
Please put a number in the title so people can watch in order. Thanks!
@dev-worm
@dev-worm Год назад
I added a link to the play list in the description, and the number of he episode is in the thumbnail, i did this so its not just "How to Make an RPG in Godot 4 episode 4" but even if your not following the episoide and you need to learn to create an enemy this video is still helpful
@SrWalo
@SrWalo Год назад
Hii I have a problem when I have two enemies together they detect the player and chase him but there is no collision between them so the enemies can overlap :( can anyone help me :c
@seanm3933
@seanm3933 Год назад
not sure of your setup, but try putting the enemies on layer 3 under collision layer, and then make sure the mask layer3 is also selected
@gunnersmokey4851
@gunnersmokey4851 10 месяцев назад
so i can fix the issue of the character being pushed around the map
@Dooodle2949
@Dooodle2949 10 месяцев назад
9:00 I've done this code exactly the same, but for some reason my enemy goes towards the player as soon as I start the level, even though he's outside the detection range. as soon as it catches up, it starts acting normally, I don't know why
@Sheepilz
@Sheepilz 7 месяцев назад
same issue but previously didnt have it dont know why
@necrox7gaming186
@necrox7gaming186 6 месяцев назад
have you found a solution?
@rohaanpatel
@rohaanpatel 4 месяца назад
I had the same issue. For some reason the enemy heads towards the Tilemap node (even though it seems like it should be out of range anyway)? What worked for me: - On the player scene, under collision, set Layer to 2 - On the enemy scene, on detection_area under collision, set Mask to 2 only - On the world scene, click on Tilemap > TileSet button on the right > Physics Layers, make sure Layer is set to 1 only
@MijmerMopper
@MijmerMopper 5 месяцев назад
I have followed everything trough the letter up till 9:00 but my enemy does not move towards the player, but yeets off into the distance. I triple checked the code to make sure there was no + - shenanigangs going on, the colission layers are all as shown, I am really confused at this point as to what I and so many others seem to be doing wrong. Has anyone figured it out yet?
@MijmerMopper
@MijmerMopper 5 месяцев назад
OK, so I got to fixing parts of the problem. nr1, mess around with the speed var. Make it higherso the enemy walks at a more reasonable pace. (the equation is divides by speed, so a higher speed var means a slower enemy), nr 2 get finniky with the player, the positioning of the sprite and the collisionshape2d in the player scene REALLY matter to an absolutly counterintuitive degree. I got it to the point that the enemy does infact move towards the player, but keeps going in the same direction regardless of if I move the player around. Progress!.
@MijmerMopper
@MijmerMopper 5 месяцев назад
update, the enemy goes towards the player if the enemy is to the left of the player, it sprints off into the distance if the enemy is on the right side. I can feel the solution growing closer!
@MijmerMopper
@MijmerMopper 5 месяцев назад
Datapoint! Switching position and player.position in the between brackets on line 19 switches if the enemy yeets off to the right or to the left when the player enters the detection area/
@MijmerMopper
@MijmerMopper 5 месяцев назад
I spend most of the day messing around trying to understand and fix this problem, dragging in various other "make an enemy chase the player" tutorials and am giving up for the day. Best I can tell the problem is that for some reason the code isn't saying "when player enters detection field, move toward player" but "when player enters detection field, move so that the player is to the left of the detection field".
@MijmerMopper
@MijmerMopper 5 месяцев назад
for anyone running in the same problem. I still don't know what went wrong, but I found a different code that does the same thing and actually works. func _physics_process(delta): if player_chase: var velocity = global_position.direction_to(player.global_position) move_and_collide(velocity * speed * delta) $AnimatedSprite2D.play("walk")
@Frazer213
@Frazer213 6 месяцев назад
how do i get the direction an enemy is moving, without the player's input. Say my enemy has various direction animations, how do I get the enemy's direction to play those.
@rompevuevitos222
@rompevuevitos222 6 месяцев назад
If your enemy is a CharacterBody2D, the velocity property is a good way to determine it's direction. For example, "if velocity.x > 0 then look right" If not, you'll need to create your own way of defining what a "facing" is.
@imanxs6884
@imanxs6884 9 месяцев назад
Hola! perdón por comentar en español, pero quería saber como solucionar algo. Es que cuando el enemigo pasa frente a una colission shape de otro objeto, este solo lo atraviesa, y no colisiona con el objeto. Incluso puede mover al jugar a través de él. Alguna ayuda?
@CullixYT
@CullixYT 9 месяцев назад
Excellent! The enemy wasn't colliding with the tiles, i added "move_and_collide" in: func _physics_process(delta): if player_chase: ... move_and_collide(Vector2(0,0)) ... And now it works :). traducelo hermano
@imanxs6884
@imanxs6884 9 месяцев назад
@@CullixYTGracias
@CullixYT
@CullixYT 9 месяцев назад
claro@@imanxs6884
@badmusicproducer_offical
@badmusicproducer_offical Месяц назад
12:57 just keeping this time link for when i come back to this video
@dev-worm
@dev-worm Месяц назад
a little bookmark lol!! I bet you are doing amazing so far! if you have any questions then let me know!
@Sebastian-wk8cq
@Sebastian-wk8cq 7 месяцев назад
for some reason the enemy isn't detecting the player, I know this because I used print functions to detect what you've been spotted. I put both the layer and the mask of the detection range on layer 2, and put the player on both layer 1 and 2.
@Sebastian-wk8cq
@Sebastian-wk8cq 7 месяцев назад
after so tests I can conclude layer 2 is broken somehow in Godot's current build or somewhere in my project
@Sebastian-wk8cq
@Sebastian-wk8cq 7 месяцев назад
on further notice it seems I used the wrong function...
@tinyboog00
@tinyboog00 Год назад
Why does my enemy come flying onto the screen at my player if I have it off screen and the body isn't in the detection area?
@kosm460
@kosm460 Год назад
are you += the position. position += player.position - position. if you only set it to = then he will jump there instead of walk
@AnimateMii
@AnimateMii Год назад
Strange, it doesn't seem to be working for me. It makes the enemy run away from me when I go into the hit box.
@akon1189
@akon1189 8 месяцев назад
same problem here
@jaserwells6828
@jaserwells6828 7 месяцев назад
It may be that you need to go back to your player and make sure that the orange cross is lined up with both your character sprite and the collision shape.
@Multifandomguy
@Multifandomguy 7 месяцев назад
Hello First of all, English isn't my 1st language so excuse me for any mistype anyways, I got a question how to make the player have an area that activates with the enemy's one cuz the enemy doesn't response at all
@rompevuevitos222
@rompevuevitos222 6 месяцев назад
Use the "body_entered" signal to detect PhysicsBody nodes entering the area, like CharacterBody2D
@Majesty_V1
@Majesty_V1 Месяц назад
I don't know what i did and/or didn't do correctly, but when i get into the detection area the enemy moves away from me up the y-axis. any advice would be helpful.
@denbro1018
@denbro1018 Месяц назад
i have the same issue but he goes down on the y axis, did you find a fix?
@Majesty_V1
@Majesty_V1 Месяц назад
@@denbro1018 My issue was I didn't have the player character in the right spot in the player scene.
@user-rd3lk5kv7j
@user-rd3lk5kv7j Месяц назад
use the global_position instead of position. maybe it will gonna work.
@Dan-nz9oj
@Dan-nz9oj Месяц назад
the video is pretty old, but I hope someone can answer me. What to do if I have the problem "Cannot find the propety player on base Vector2"?
@fenix1sabean326
@fenix1sabean326 9 дней назад
Why is the slime running away?
@unknown-sm8cx
@unknown-sm8cx Год назад
Bro enemy move when player collide but it not came near the character
@kryzix72
@kryzix72 4 месяца назад
I did this exact thing, and when i run towards the slime, it just goes away from me, i followed the tutorial step by step, and it doesnt follow me, it just keeps on going away from me
@dev-worm
@dev-worm 4 месяца назад
you most likey have a + or - switched somewhere
@Lityx
@Lityx 3 месяца назад
I did everything in the tutorial on how to create the enemy but my enemy does not collide with the collisions that I placed on the TileMaps, it passes through them, please help
@WolfCodee
@WolfCodee 3 месяца назад
i think u must use the code move_and_collide(Vector2(0,0)) in physics process delta
@beansbeans96
@beansbeans96 6 месяцев назад
i keep having an issue where my enemy appears in front of the player when i walk in front of it, any clue what could be wrong here?
@beansbeans96
@beansbeans96 6 месяцев назад
welp i found the issue, i fixed it by turning ysort on in the world main node
@Shawn-cq7qy
@Shawn-cq7qy 7 месяцев назад
What if it’s said the player is not found
@LoIandfunny
@LoIandfunny 11 месяцев назад
My enemy starts with it’s running animation, how do I fix that
@furkanaldemir9292
@furkanaldemir9292 11 месяцев назад
me too
@art1cc
@art1cc 6 месяцев назад
Know I’m 4 months late but func _ready(): get_node(“AnimatedSprite2D”).play(“idle”)
@rangergreen1935
@rangergreen1935 6 месяцев назад
I could be wrong, but if it's what I dealt with, try making sure that the animation that you leave off on is always the idle or one you want to start with
@adrianvazquez8686
@adrianvazquez8686 5 месяцев назад
I've got a problem, my enemy has no collisions with the objects
@MijmerMopper
@MijmerMopper 5 месяцев назад
make sure that both the enemy's collisionshapes and the y sorting for the objects are on the same level.
@asome_x2165
@asome_x2165 14 дней назад
Well my enemy is just running from me😂😂
@dev-worm
@dev-worm 13 дней назад
you most likey have a + or - flip flopped somewhere!!!
@JetMango
@JetMango 11 месяцев назад
This tutorial has been great but I have run into one problem. When I try to play the game the enemy won’t chase the player but it sits in the same position relative to the player. For example it will be 2 tiles to the left and 1 tile up from wherever the player is. The slime will also move at any speed to get to that position. Does anyone know what I can do to fix this?
@dev-worm
@dev-worm 11 месяцев назад
make sure the center point of the player is in the middle of the player and also make sure the center point of the slime is in the middle of the slime
@BryathalonDev
@BryathalonDev 9 месяцев назад
@@dev-wormexplain?
@MijmerMopper
@MijmerMopper 5 месяцев назад
@@BryathalonDev I had the exact same problem and couldn't fix it. I found an alternative code on the godot forum that DID work as intended. if player_chase: var velocity = global_position.direction_to(player.global_position) move_and_collide(velocity * speed * delta)
@maertyrer420
@maertyrer420 4 месяца назад
@@MijmerMopper THANK YOU MAN, I FUCKING LOVE YOU! i was literally 2h in trying to fix the bug with like 1000 of different codes and was about to give up
Далее
Why Do Video Game Studios Avoid Blender?
6:49
Просмотров 272 тыс.
Asus  VivoBook Винда за 8 часов!
01:00
Просмотров 316 тыс.
ТИПИЧНЫЙ РЕСТОРАН
12:09
Просмотров 327 тыс.
Nintendo Did The Impossible...
14:25
Просмотров 42 тыс.
Creating SMART enemies from scratch! | Devlog
5:40
Просмотров 271 тыс.
Fog Overlay Shader in Godot 4 - 2 minutes tutorial
2:21
Optimizing my Game so it Runs on a Potato
19:02
Просмотров 414 тыс.
Advance Enemy AI in Godot
8:17
Просмотров 19 тыс.
I Made a Zero Player Game
12:30
Просмотров 10 млн
I Made the Same Game in 8 Engines
12:34
Просмотров 3,9 млн
How Games Make VFX (Demonstrated in Godot 4)
5:46
Просмотров 311 тыс.
How to Change Scenes Smoothly in Godot 4
22:39
Просмотров 34 тыс.
Asus  VivoBook Винда за 8 часов!
01:00
Просмотров 316 тыс.