Enemy Patrol/Wander AI Unity SIMPLE & EFFECTIVE CODE! Tutorial

  Рет қаралды 34,658

Kap Koder

Kap Koder

4 жыл бұрын

A very precise and performant Patrol Script in Unity Engine! code in C#, a long with some cool and handy syntax! simple for beginners too! some wander/patrol script explained in well detail
Credits to GameDev.tv with Ben Tristam and Rick Davidson for their 2D course on Udemy for showing this awesome code

Пікірлер: 154
@violala8279
@violala8279 3 жыл бұрын
This tutorial was extremely helpful. I've been looking everywhere for a code that I can actually understand and that doesn't seem unnessesarily cluttered. This was just what I've been hoping for. Thanks :)
@ArmorCroc
@ArmorCroc 3 жыл бұрын
Your voice is soothing and your pace is on point. After scrapping three different AI implementations, this video finally had it dawn upon me how I should've been doing it all along! Great tutorial. THANK YOU!
@SweetVibesOfficialMusic
@SweetVibesOfficialMusic 3 жыл бұрын
u know , after a week of searching , luckily i found you , this script is really simple without that bs that other youtube does , 10 minutes perfect timing explained everything in it (i watched ur other vids too, love em), you made my day so here is a new subscriber ❤👌
@Amonguus
@Amonguus Жыл бұрын
Man I have been watching different videos for two hours. Your code has been the only one that has worked. Thank you so much
@Smoky_da_cat
@Smoky_da_cat Жыл бұрын
I saw many videos but all of them were so complicated but yours so simple loved it
@HakiYikio
@HakiYikio 2 жыл бұрын
Thank you GameDevTV to bring this tutorial to you :D
@puntalic
@puntalic 3 жыл бұрын
This tutorial earned you at least one new subscriber. Keep it up & thanks!
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Glad it helped you! Thanks for the sub
@NathanOnline
@NathanOnline 2 жыл бұрын
Thank you so much!! This is the only tutorial I've seen that actually works and isn't 2 fps :DDD
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Hehe I think it's 30fps 😼
@spectrorobot7069
@spectrorobot7069 Жыл бұрын
great tutorial, very simple and to the point
@aizuki9261
@aizuki9261 Жыл бұрын
Thank you very much this code was helpful for my universal project.❤❤❤
@Bdarkan
@Bdarkan 3 жыл бұрын
Somehow you said everything I needed in 10 minutes when my teacher gave me nothing over a half hour
@tam4655
@tam4655 Жыл бұрын
it shouldve only taken 30 minutes of teaching to understand everything, AT MOST! which means ur teacher sux so gl with him, many helpful yt tutorials out there
@abdulkadir7c328
@abdulkadir7c328 3 жыл бұрын
This tutorial is so helpful that i will give you a sub, thx bro
@devmah
@devmah Жыл бұрын
A deep Thank you from Vietnam to you ;D
@mohammadnuriskiakbar7803
@mohammadnuriskiakbar7803 3 жыл бұрын
great and effective code. Thanks a lot
@Baize-ro4vz
@Baize-ro4vz Жыл бұрын
Thanks, it's really easy and useful.
@Xelfist
@Xelfist Жыл бұрын
How would you do this with an overlapbox? I want to use layers to not allow the player to redirect the enemy. A platform effector's collider mask works for now but is there a better way to do this?
@gabrielacisleanu6192
@gabrielacisleanu6192 11 ай бұрын
I do not have an animation for my enemy, but the sprite that I am using is rotating on the z axis when moving and it won't come back to the original position. Any ideas how I can fix this? I tried to freeze the rotation from the RigidBody component but it did not work.
@xqes2037
@xqes2037 3 жыл бұрын
it worked, thanks for that.
@atreyuslilcottage4053
@atreyuslilcottage4053 3 жыл бұрын
I'd love a tutorial on bullets/objects bouncing off of walls and destroying once they hit a certain point cx
@isimsizanimator1291
@isimsizanimator1291 Жыл бұрын
My character's scale needs to change on the y-axis. I did the opposite by putting y where you put x, x where you put y, but the code doesn't work, can you help?
@odalmer8110
@odalmer8110 3 жыл бұрын
how can I use another collider for detecting
@ShahidZia-of1mk
@ShahidZia-of1mk Жыл бұрын
Which compiler are you using?
@kamalfaiz7286
@kamalfaiz7286 2 жыл бұрын
why i cannot use serializefield? is there some kind of extension that I need?
@Drana3308
@Drana3308 10 ай бұрын
How does the neemy detect they've hit a wall ?
@imviciously
@imviciously 2 жыл бұрын
great tutorial :)
@aaronmacasaet2284
@aaronmacasaet2284 Жыл бұрын
thank you very much!
@patapizza3382
@patapizza3382 4 жыл бұрын
Hey, so I have a couple of questions. So when the enemy turns, it gets really distorted for some reason. And is there a way I can use two different colliders for ground and wall detection? Thanks in advance
@kapkoder4009
@kapkoder4009 4 жыл бұрын
I understand the problem, When the enemy turns the problem is the code doesn't simply ROTATE the enemy by 180 deg. It changes the actual SCALE... so for example, if your enemy size is 5x, 5y, 5z, than when the code tells him to turn it takes the SCALE and ASSUMES it is 1x 1y 1z, and to rotate the enemy it takes the 5x and makes it -1x, so after he turns he probably would look like a stick Hope this helps 👇 Here's the fix: Your scale rotating code line probably looks like this private void OnTriggerExit2D(Collider2D collision) { transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y) } 1. First easy fix would be if it is a SPRITE and not a UI element than simply press on the sprite and in the inspector you'll see there is a "Pixels Per Unit" (default at 100) lower that number to make him bigger and go back to your enemy and set his scale back to (1x, 1y, 1z) 2. Second fix requires some code but here what you need to do - private void OnTriggerExit2D(Collider2D collision) { //transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y) < CHANGE FROM THIS //TO THIS 👇 transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y) //All this does is flip our CURRENT SIZE, which the old line doesn't do, so now you can have any size //All was done was I switched -(Mathf.Sign(myRigidbody.velocity.x)) with -transform.localScale.x }
@violala8279
@violala8279 3 жыл бұрын
@@kapkoder4009 So the inital code only works when the character's scale is 1x,1y,1z ? because the Mathf.Sign structure returns the number (x value) to 1? Therefore if my character has a different size than 1 it will get deformed. Is that correct?
@kapkoder4009
@kapkoder4009 3 жыл бұрын
@@violala8279 That is correct! And if your sprite is not your desired scale when it is scale 1 than all you have to do is go to your sprite in your assets and decrease the "Pixels per unit" from 100 to a lower number to make is bigger and vice versa, or maybe if your purposely stretching your sprite (say 1.5x and 1y) than you would have to use a different formula
@IM-ws5if
@IM-ws5if Жыл бұрын
@@kapkoder4009 Thank you very much, I wish you luck
@CodingKricket
@CodingKricket Жыл бұрын
@@IM-ws5if I know I am pretty late but I also found using transform.localScale*= new Vector2(-Mathf.Sign(myRigidbody.velocity.x),1); works too. It takes the sprites current scale and multiplies it by 1 or -1 to get the new scale. Oh wait I think I found a problem with it, when you multiply the current scale by 1, it doesn't change so you would have to multiply it by -1 each time I did find that transform.localScale*= new Vector2(-1,1); woks for now, but I may find an issue with it later on.
@roberttorres3009
@roberttorres3009 3 жыл бұрын
I have 2D sprites but the environment is 3d. Right now the sprite is going through the 3d objects and not treating them as barriers. How could I make this work? Also, does it matter if the terrain is not completely straight. I have some diagonal slopes in the scene and I would like the patrol to continue up along the path until it hits a wall or edge like in your video. Thanks!
@CodingKricket
@CodingKricket Жыл бұрын
Even though I am 2 years late, if you or anyone else are still having this problem it's probably because BoxCollider 2Ds and Box Colliders (3d Box colliders) don't interact with each other.
@Jeea84
@Jeea84 Жыл бұрын
Thank you thank you thank you!!!!🙇‍♀
@gmodify9568
@gmodify9568 Жыл бұрын
OnTriggerExit2D is making problems for some reason does anyone know why?
@scriptsthefamous2250
@scriptsthefamous2250 3 жыл бұрын
nice awesome great epic fantastic
@nicedoodly6392
@nicedoodly6392 Жыл бұрын
very very thanks😀
@abdulkadir7c328
@abdulkadir7c328 3 жыл бұрын
hey can you help me with my enemy because the enemy didnt flip after hitting the wall and i dont know why
@64_Tesseract
@64_Tesseract 2 жыл бұрын
There's a few correction I think you could make... For one, on line 33, I don't see why you'd use a mathematical constant where precise maths isn't needed; just use 0.001f, or even better, 0f. Another shortcut you could take is skipping the whole `if(IsFacingRight())` step anyway and just set the velocity to `new Vector2(moveSpeed * Mathf.Sign(myRigidBody.velocity.x), 0f)`, which is way more efficient. ...The edge detection is a smart idea tho
@kapkoder4009
@kapkoder4009 2 жыл бұрын
I made this video a year ago lol
@marvinbarboza1045
@marvinbarboza1045 7 ай бұрын
@@kapkoder4009 then correct it
@linedoltrainerchannelgammi2247
@linedoltrainerchannelgammi2247 10 ай бұрын
it is helpful, but I got one problem, when the enemy walks away to a different direction from the no tile part, it just keep on bumping and switching between frames right to a wall,, even it it doesn't do that anymore, it goes right through the wall
@felixn822
@felixn822 3 жыл бұрын
That's great, I just don't understand why it also turns when it hit the wall if it's an OnTriggerExit.
@kapkoder4009
@kapkoder4009 3 жыл бұрын
When he walks to a ledge his collider exits the ledge's collider and turns, and for walls it works because the collider sticks out of the ground a little bit
@Justme-zq6bf
@Justme-zq6bf 2 жыл бұрын
really good tutorial but changing my move speed value dosn`t do anything. can someone help me ?
@tam4655
@tam4655 Жыл бұрын
nice tutorial but im having trouble understanding why this works with walls and edges and not JUST edges?
@neozoid7009
@neozoid7009 2 жыл бұрын
How can i make an object wandering within the screen please help.🙏
@ciechanek8886
@ciechanek8886 3 жыл бұрын
Great tutorial! But how can i make him ignore collision with player controlled object?
@zedtix4971
@zedtix4971 3 жыл бұрын
void OnTriggerExit2D(Collider2D other) { if (other.tag == "ground" || other.tag == "wall") { transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y); } }
@asasin8129
@asasin8129 2 жыл бұрын
better guide than Brackeys!
@kuyabonbondevlog1044
@kuyabonbondevlog1044 3 жыл бұрын
Hello I have a question, why is it sticking to my pillar/tower? Its flipping and moving but its stuck between it. Need help.
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Make sure your ground check collider is set up properly, this does all your ground and wall detection
@alopmemes1964
@alopmemes1964 3 жыл бұрын
Hey there! So the velocity of my rigidbody is always 0 and doesn’t change when the enemy is moving. So it always rotates to the right. I tried fixing this since yesterday morning but i just don’t get it. I tried so many ways to flip the enemy but nothing works :( Maybe you could help me? Would be really great 😊
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Show me da code
@maurenaja
@maurenaja 3 жыл бұрын
transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y); "Mathf.sign" make my character change size into 1 or -1 scale, i use 0.1.... for my character scale. what should i do?
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Well you need to cache your start scale so here's (A) way to do it float xScale = 0f; void Start() { xScale = transform.localScale.x //Note that this assumes that the start scale is positive, if not than flip the xScale like this xScale = -xScale; } And do this for the flip code float x = myRigidbody.velocity.x > 0f ? xScale : -xScale; Thats a pretty complicated line but its basically a cleaner if statement like this, if x > 0 (? Question mark) firstValMeansTrue : secondValMeansFalse; transform.localScale = new Vector2(x, transform.localScale.y) Hope this helps! 😁 have an amazing day
@lynai8273
@lynai8273 2 жыл бұрын
my problem is that the enemy doesn't really stop at the end of a platform. it goes some steps more as it should and flips too late, resulting him being in the air for some seconds. How can i fix this? I tried changing the box colliders and edge colliders on the ground but it still doesn't work :( My terrain has colliders too and my BoxCollider is marked as a trigger so I really don't see what i'm doing wrong
@kapkoder4009
@kapkoder4009 2 жыл бұрын
The only thing I can think of is your collider may be exiting your ground collider too late based on its size and shape or you tweaked your code in some way to delay it :/
@lynai8273
@lynai8273 2 жыл бұрын
Edit: Wow i didnt even see you replied! thank you so much!! I just fixed it by putting the collider in the middle of the enemy instead of outside of it. My next problem ist that with my current script if I kill the enemy it keeps moving... the death animation takes places, so dying actually works. but the enemy well.. still moves. do you maybe have a "Die" and "Damage" method for the enemy?
@lynai8273
@lynai8273 2 жыл бұрын
I actually figuered out how to stop him from moving. I have another question. do you know how to disable multiple colliders of one GameObject? My Enemy contains of 2 box colliders, one that's a trigger like in your video and the other for detecting collision with the ground and other objects. do you know how to disable BOTH of them in a script? What I've done only works for the "trigger" collider... the other one is still enabled when he's dead
@kapkoder4009
@kapkoder4009 2 жыл бұрын
@@lynai8273 Get a reference to both of your colliders and disable them both at once, or you can also make a game object childed to the enemy and just get a reference to that object that has the collider components and just disable that game object, but I think the first option is easier
@chief9994
@chief9994 2 жыл бұрын
My enemy just stops randomly and i don't see any problem with the code that i have. I tried adjusting the size of the collider but still does not work please help me how to fix this problem.
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Hmm... I wouldn't know what to do unless I saw the code
@PegacornGuppy
@PegacornGuppy 4 жыл бұрын
Nice video. I'm new to Unity/C#. Do you have any ideas for how to set up a patrol where an enemy will follow the player if they see them, before returning to a patrol?
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Do you need this for a 2D platform type of enemy?
@PegacornGuppy
@PegacornGuppy 4 жыл бұрын
@@kapkoder4009 it's for a top down 2d
@kapkoder4009
@kapkoder4009 4 жыл бұрын
I put together this little script for you, put it on your enemy and it should work :) make sure your player has a player script too cause that's how it's found, i know how hard it is to start out coding i hope this helps. { [SerializeField] float enemySpeed = 1f; //HERE are the presets for your enemy! feel free to change any of the values here [SerializeField] float startFollowRange = 5f; //only in the inspector though because the inspectors values override these values [SerializeField] float damageRange = 0.5f; [SerializeField] float damageAgainDelay = 1f; float distance = 0f; //These check don't need to be changed anywhere cause its set by code bool damagePlayerCalled = false; bool damagedPlayer = false; GameObject player; void Start() { player = FindObjectOfType().gameObject; //Player MUST have a "Player.cs" script on it in order to find Player //player = GameObject.FindGameObjectWithTag("TagName"); //If you want to find by Tag this is what you do } void Update() { CheckForPlayer(); //We check if player is in range every frame } private void CheckForPlayer() { distance = Vector2.Distance(transform.position, player.transform.position); //calculate the distance if (distance < startFollowRange) //Check if player is in following range { MoveToPlayer(); } } private void MoveToPlayer() { transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemySpeed * Time.deltaTime); //simply moves from our position to player position if (distance < damageRange && !damagePlayerCalled) //We say "&&" to say "and" in an if statement, and i check for if damagePlayerCalled == true { //so we don't call the coroutine multiple times StartCoroutine(DamagePlayer()); } } private IEnumerator DamagePlayer() { damagePlayerCalled = true; if (damagedPlayer) { yield return new WaitForSeconds(damageAgainDelay); damagedPlayer = false; } //If first time getting in range damage immediately Debug.Log("Damage player!"); //You can put whatever you want here damagedPlayer = true; damagePlayerCalled = false; } }
@PegacornGuppy
@PegacornGuppy 4 жыл бұрын
Thank you so much! I've been struggling so hard. Haha. Will subscribe for more content!
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Np 😂 when I first started off I spent hours and hours trying to make a 2D wandering object.. i could never find the answer on Google anywhere either lol I adventually figured it out
@holyrocketbunny2801
@holyrocketbunny2801 2 жыл бұрын
hi if I scale my enemy up when it rotates the x becomes -1 instead of -3 is there a fix for that?
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Because your rotate code uses "Mathf.Sign", and mathf.sign only rounds up to the nearest value, so it can only be 1, or -1, to fix that you would need to replace it with your own variable You could also fix this problem by just lowering the pixels per unit of your enemys sprite to make him bigger IF he is going to be a fixed size for your game
@holyrocketbunny2801
@holyrocketbunny2801 2 жыл бұрын
@@kapkoder4009 Thank you I will probably use pixels per unit method !
@mraxle9028
@mraxle9028 2 жыл бұрын
did i do it corectly bc my sprite doesnt stop from walls and when he changes direction he kinda goes thin using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemybehavior : MonoBehaviour { [SerializeField] float moveSpeed = 1f; Rigidbody2D myRigidbody; void Start() { myRigidbody = GetComponent(); } void Update() { if(IsFacingRight()) { myRigidbody.velocity = new Vector2(moveSpeed, 0f); }else { myRigidbody.velocity = new Vector2(-moveSpeed, 0f); } } private bool IsFacingRight() { return transform.localScale.x > Mathf.Epsilon; } private void OnTriggerExit2D(Collider2D collision) { transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y); } }
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Your code looks completely fine... my you need to configure your colliders??
@dubcan4213
@dubcan4213 4 жыл бұрын
Hi nice video very well made and simple code. I'm just having a problem, it detects the edge of a platform and turns around but its not turning back when it runs into a wall, what am I missing or where did my code go wrong??
@dubcan4213
@dubcan4213 4 жыл бұрын
I now changed the collider trigger box a bit in scale now it turns at edge of platform but when it runs into a wall its stationary and just spazms back and forth at the wall
@kapkoder4009
@kapkoder4009 4 жыл бұрын
@@dubcan4213 this is caused by the position and size of the collider, in the video I show how to set up the collider, You can also make a secondary collider to detect walls, also make sure your walls and floors are used by composite collider to decrease the amount of edges on painted tilemaps collider
@kapkoder4009
@kapkoder4009 4 жыл бұрын
@@dubcan4213 BTW if you ended up trying to use a second collider this is what you would do: ColliderType2D collider; //Whatever type of collider you chose void Start() { collider = GetComponent(); } void OnTriggerEnter2D(Collider2D collision) { if(collider.IsTouchingLayers(LayerMas. k.GetMask("Layer name"))) { //Do stuff } }
@dubcan4213
@dubcan4213 4 жыл бұрын
@@kapkoder4009 Thank you so much for the reply! the detection collider is getting messed up by my wall and floor colliders. Thank you so much for your help!!
@violala8279
@violala8279 3 жыл бұрын
@@kapkoder4009 Can I do the same thing with if(collision.gameObject.CompareTag("Wall"))? I have the problem that If I create a void OnTriggerEnter(Collider2D collision), it won't work unless I get rid of the OnTriggerExit function. What exactly do I have to write into the OnTriggerEnter so that it harmonizes with OnTriggerExit? Thanks in advance for all your efforts :)
@noem4162
@noem4162 3 жыл бұрын
This Tutorial is Great! Just one thing, whenever my charactor turns his renderer disables.
@zedtix4971
@zedtix4971 3 жыл бұрын
void OnTriggerExit2D(Collider2D other) { if (other.tag == "ground" || other.tag == "wall") { transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y); } }
@soyaboya6212
@soyaboya6212 Жыл бұрын
its been a while since you posted this, but it all works fine. the only problem i have is when i place it on something that isnt a tilemap, it goes crazy between the two colliders. gotta fix?
@Narddz
@Narddz Жыл бұрын
hey man, did u happen to fix this issue?
@soyaboya6212
@soyaboya6212 Жыл бұрын
nope, i went to something else after i couldnt fix this lmao
@soyaboya6212
@soyaboya6212 Жыл бұрын
@@Narddz vur if you figured out a fix ide be happy to go back to my project
@Megan-ii6vj
@Megan-ii6vj Жыл бұрын
are you using a composite collider on the tilemap? that should fix the issue
@simeqoff
@simeqoff 3 жыл бұрын
dude you climbed up youtube
@brunoderkamramann7671
@brunoderkamramann7671 2 жыл бұрын
My trigger hitbox is not wroking, hes just walking straight in the air any ideas why?
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Check all your colliders
@uh3622
@uh3622 Жыл бұрын
how do you do it so it has an animation when it moves?
@sansguerra2265
@sansguerra2265 Жыл бұрын
Search for "Brackeys how to make animations 2d game in unity"
@CodenTheSynth
@CodenTheSynth 10 ай бұрын
the enemy is not only squished but just walks endlessly
@Bdoll1993
@Bdoll1993 3 ай бұрын
couldn't you just flip the sprite instead of negating the localScale?
@ultrabikeboy
@ultrabikeboy 4 жыл бұрын
Please help me, my character don't turn back it just stay going forward, and the scale is 1x 1y 1z and also I copy/pasted the script you put in the chat with all the requirements. PLEASE HELP MEEEEE.
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Someone had this problem in the comment section, I copy and pasted my answer: I understand the problem, When the enemy turns the problem is the code doesn't simply ROTATE the enemy by 180 deg. It changes the actual SCALE... so for example, if your enemy size is 5x, 5y, 5z, than when the code tells him to turn it takes the SCALE and ASSUMES it is 1x 1y 1z, and to rotate the enemy it takes the 5x and makes it -1x, so after he turns he probably would look like a stick Hope this helps 👇 Here's the fix: Your scale rotating code line probably looks like this private void OnTriggerExit2D(Collider2D collision) { transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y) } 1. First easy fix would be if it is a SPRITE and not a UI element than simply press on the sprite and in the inspector you'll see there is a "Pixels Per Unit" (default at 100) lower that number to make him bigger and go back to your enemy and set his scale back to (1x, 1y, 1z) 2. Second fix requires some code but here what you need to do - private void OnTriggerExit2D(Collider2D collision) { //transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y) < CHANGE FROM THIS //TO THIS 👇 transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y) //All this does is flip our CURRENT SIZE, which the old line doesn't do, so now you can have any size //All was done was I switched -(Mathf.Sign(myRigidbody.velocity.x)) with -transform.localScale.x }
@ultrabikeboy
@ultrabikeboy 4 жыл бұрын
forget I fixed it
@ultrabikeboy
@ultrabikeboy 4 жыл бұрын
I just put a box collider in the floor XD
@kapkoder4009
@kapkoder4009 4 жыл бұрын
@@ultrabikeboy I was asleep, but glad you fixed it! And yeah I reply as much as I can 😂
@husseinrizk8933
@husseinrizk8933 3 жыл бұрын
That is GameDev 2D Game Dev Course on Udemy
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Yup I just wanted to share it because the information isn't really accesable on youtube
@husseinrizk8933
@husseinrizk8933 3 жыл бұрын
@@kapkoder4009 ya, you are right . but you could have give them credits . i am sure Rick wouldn't mind but they deserve it xD
@kapkoder4009
@kapkoder4009 3 жыл бұрын
@@husseinrizk8933 I should put it in the description
@husseinrizk8933
@husseinrizk8933 3 жыл бұрын
@@kapkoder4009 I think you should
@holyrocketbunny2801
@holyrocketbunny2801 2 жыл бұрын
for some reason, my enemy does not rotate and keeps walking through the tiles can somebody help ?
@holyrocketbunny2801
@holyrocketbunny2801 2 жыл бұрын
ok i fixed it i was missing the 2d on the OnTriggerExit2D
@anirudhganesh7714
@anirudhganesh7714 3 жыл бұрын
My Sprite is not moving and when my player hits the enemy object, the flip happens but it is weird, the sprite shrinks when the flip happens. keep doing vids like this and you'll be raking subs in no time. Although, you might wanna add the code in git or somewhere else and leave a link along with the assets so that it'd be useful for those who are just starting with something.
@yugnatata
@yugnatata Ай бұрын
If the sprite shrinks or grows, it means it's scale was set to more or less than 1, for the sprite not moving, no idea, follow step by step his guide, maybe you missed something? (three years ago but hey, who knows)
@jesseramon5013
@jesseramon5013 3 жыл бұрын
4:58
@jaykztvn
@jaykztvn 4 жыл бұрын
hello my enemy doesn turn around he just goes forward what should i do ?
@jaykztvn
@jaykztvn 4 жыл бұрын
Never mind i fixed that but next problem my enemy changes scale when it first hits the edge
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Someone also had a similar problem in the comment section... Check out MichealTheBot's comment, I replied to him the solution to the problem and also some code that you can copy/paste 👍 its very detailed
@jaykztvn
@jaykztvn 4 жыл бұрын
@@kapkoder4009 Thank you
@jaykztvn
@jaykztvn 4 жыл бұрын
@@kapkoder4009 and btw do you know a way to make enemy attack player ??
@kapkoder4009
@kapkoder4009 4 жыл бұрын
@@jaykztvn for a 2D platformer or top down?
@jimmyjab190
@jimmyjab190 3 жыл бұрын
How would I change this code to make the enemy go up and down along the y axis instead? i tried changing the transform local scale to y but it did nothing
@kapkoder4009
@kapkoder4009 3 жыл бұрын
On the line "myRigidbody.velocity = new Vector2(moveSpeed, 0f);" this line sets the velocity (movement speed or direction) of the gameObject, the vector has an x and y axis "Vector2(x, y)" replace the x with 0f and put the moveSpeed in the y instead Now of course after this you'll need to make sure your collider will be changed to find vertical walls instead so you may have to tweak that meanwhile making sure the walls have colliders aswell Hope this helps sorry I'm tired rn lol
@jimmyjab190
@jimmyjab190 3 жыл бұрын
@@kapkoder4009 That's such a simple fix, thank you! As for the flipping, I got it to go up and flip and go back down, but it stops after that and doesn't flip again, any thoughts?
@songediter9101
@songediter9101 2 жыл бұрын
please help my enemy still not turn when he hits the wall :/
@kapkoder4009
@kapkoder4009 2 жыл бұрын
Try adjusting your wall detection collider, and make sure yours walls have colliders swell
@maniacplayzgamez4283
@maniacplayzgamez4283 3 жыл бұрын
yeah my enemy is just going through everything. even with the box colliders......
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Make sure the terrain has colliders too and the enemy collider is setup properly Also make sure your enemy collider is set to trigger so it can call the OnTriggerEnter2D function
@zedtix4971
@zedtix4971 3 жыл бұрын
this is code and make him ignore collision with player and Other objects float speed = 1f; Rigidbody2D rb; BoxCollider2D bc; // Start is called before the first frame update void Start() { rb = GetComponent(); bc = GetComponent(); } // Update is called once per frame void Update() { if (isfacingright()) { rb.velocity = new Vector2(-speed, 0f); } else { rb.velocity = new Vector2(speed, 0f); } } bool isfacingright() { return transform.localScale.x > Mathf.Epsilon; } void OnTriggerExit2D(Collider2D other) { if (other.tag == "tilemap" ) { transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y); } }
@gokuization
@gokuization Жыл бұрын
Thanks for your help! It fixed my problem.
@moori8643
@moori8643 3 жыл бұрын
Hey my enemy makes that what he is supossed to do but he slowly sink in to the ground 😅
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Maybe its rigidbody has gravity enabled? Or in your code your not adding 0 force on the y vector
@moori8643
@moori8643 3 жыл бұрын
@@kapkoder4009 i gonna try it
@moori8643
@moori8643 3 жыл бұрын
@@kapkoder4009 i dont check it cause its my first game can i have your insta or something like that so you could look at the code
@moori8643
@moori8643 3 жыл бұрын
@@kapkoder4009 but my rigidbody should be ok
@kapkoder4009
@kapkoder4009 3 жыл бұрын
@@moori8643 my insta is paulkapitula if you want to message me there or you can just ask following questions here if you'd like... more comments 😉😉
@EMJAYYTK
@EMJAYYTK Жыл бұрын
This distorts my enemies resolution
@EnterName12312
@EnterName12312 2 жыл бұрын
can anyone help me for the code for attacking the player?
@kapkoder4009
@kapkoder4009 2 жыл бұрын
You can adjust the movement speed of the enemy based on the distance of the enemy from the player, so that if the player is on the left than move and move left, otherwise if on the right than move and face right Let me try to seudo code this [SerializeField] float targetDist; [SerializeField] float attackMoveSpeed; [SerializeField] float attackDist; [SerializeField] RigidBody rb; Player player; void Start() { //Finding by name or tag would be better player = FindObjectOfType(); } void Update() { Vector2 playerPos = player.transform.position; float playerDist = Vector2.Distance(transform.position, playerPos) if (playerDist < targetDist) { AttackPlayer(playerPos); } } //We can pass playerPos as a parameter to recycle the variable to save some computing power void AttackPlayer(Vector2 playerPos) { //If our X position value is less than the players X position value that means the player is on our right bool playerIsOnRight = transform.position.x < playerPos.x; //Now we can choose what to do now that we know which side the player is on if (playerIsOnRight) { //Move right (move however you want, transform translate, Vector.MoveTowards or with rigidbody.velocity but that’s probably what your going to use because that’s what we used in the video rb.velocity = new Vector2(attackMoveSpeed, 0) //Now we check if we’re in attacking distance CheckToAttack(); } else { //Move left, here the difference is we move in minus attackMoveSpeed making us go left instead rb.velocity = new Vector2(-attackMoveSpeed, 0) CheckToAttack(); } } void CheckToAttack() { float dist = Vector2.Distance(transform.position, player.transform.position) if (dist < attackDist) { //Take life’s from player, you might need to use coroutines to set up you attack rate (how often you hurt the player while your touching the player } } Edit: I didn’t realize how long my reply is 🤣
@EnterName12312
@EnterName12312 2 жыл бұрын
@@kapkoder4009 BRO THANK YOU SO MUCH... I've been stuck for ages now thanks again dude... I am already subscribed xD but you deserve way more! Haha its fine :D
@kapkoder4009
@kapkoder4009 2 жыл бұрын
@@EnterName12312 Also where it says if (dist < attackDist) { } heres how you could attack the player //Add this to the top of your class for coroutines using System.Collections; //Or this one i dont remember which one it was using.System.Collections.Generic; //Add some more variables [SerializeField] float attackRate = 1f; //Set your desired attackRate float attackCooldown = 0f; bool isAttacking = false; //Set to false by default if (dist < attackDist) { if (!isAttacking) //Check if we already started this coroutine, if not, than call it { StartCoroutine(AttackPlayerWithDelay()); } } IEnumerator AttackPlayerWithDelay() { Transform playerTrans = player.transform; //Cache the players transform float dist = Vector2.Distance(transform.position, playerTrans.position); isAttacking = true; while (dist < attackDist || attackCooldown > 0f) //Says we still do all this if in attack range or cooldown timer is not at 0f, this way every time the enemy gets back into attack range it doesn’t just hit immediately attack again causing an unnaturally faster attackRate { dist = Vector2.Distance(transform.position, playerTrans.position); attackCooldown -= Time.deltaTime; //This counts down the cooldown timer with real seconds if (attackCooldown 0f) exception { player.health -= 1f; or a certain amount, you can make this a variable attackCooldown = attackRate; } } yield return null; //This means wait 1 frame. DO NOT FORGET THIS LINE } //If we get to this point that means the enemy is no longer in attack range so we do this: isAttacking = false; attackCooldown = 0f; } This all should work but theres probably bugs because i cant test anything im just coding a comment xD i hope it works for you though lol If theres errors just try to use logic to look at the code from a more objective stand point rather than a confusing what even is code syntax cry baby face stand point, but this can be hard for new programmers anyways but you'll learn logic as you code. I remember having trouble trying to flip a sprite xDDD
@user-pz7rk5ty2k
@user-pz7rk5ty2k Жыл бұрын
we need downloadd scrept
@patapizza3382
@patapizza3382 4 жыл бұрын
Could you give me the code to put into my project? sorry, kinda lazy coder .-.
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Sure thing xD gimme a sec
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Heres the code for "Enemy Platformer patrol" hope it's what you need { //This isn't just cut and dry copy and paste code! here's the requirements: //TODO - make sure you have a Rigidbody2D component on your enemy //TODO - make sure you have a BoxCollider2D setup on the edge of your enemy (as // - wall & ground detection, setup instruction in video) [SerializeField] float moveSpeed = 1f; //This movement speed is tweakable in the inspector Rigidbody2D myRigidbody; private void Start() { myRigidbody = GetComponent(); } private void Update() { if(IsFacingRight()) { myRigidbody.velocity = new Vector2(moveSpeed, 0f); } else { myRigidbody.velocity = new Vector2(-moveSpeed, 0f); } } private bool IsFacingRight() //Automatically set direction { return transform.localScale.x > Mathf.Epsilon; } private void OnTriggerExit2D(Collider2D collision) //Checks for wall/ground detection { transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), transform.localScale.y); } }
@richardmitchell2500
@richardmitchell2500 4 жыл бұрын
Kap Koder Hey this is me from my second account, THANKS SO MUCH!! i’ll sub on both accounts when i get the chance!!
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Haha thanks 😂
@RealCoachMustafa
@RealCoachMustafa 3 жыл бұрын
My guy never turns around, just keeps going straight. I even copied your code completely. What could I be doing wrong?
@RealCoachMustafa
@RealCoachMustafa 3 жыл бұрын
I realized the issue. I had an animation, changing the scale of the enemy, which also kept moving the collider. I moved the animation/sprite to a child game object
@kapkoder4009
@kapkoder4009 3 жыл бұрын
Make sure your ground and walls have a collider and you set up your enemies collider correctly
@xingorro8605
@xingorro8605 3 жыл бұрын
a lot simpler than code i use (from Blathornprod guide) where i need to set 2 or more patrolpoints, and to make my enemy go back, i need to set rotation on my 2nd patrolpoint to 180 on y
@maartenvanhove7965
@maartenvanhove7965 11 ай бұрын
I prefer this way of flipping a sprite: Vector3 localScale = transform.localScale; localScale.x *= -1f; transform.localScale = localScale;
Adaptive Enemy AI Patrol System - Complete Unity Game Dev Tutorial
27:19
Lost Relic Games
Рет қаралды 17 М.
How to make a BOSS in Unity!
21:54
Brackeys
Рет қаралды 657 М.
Super sport🤯
00:15
Lexa_Merin
Рет қаралды 20 МЛН
How I prepare to meet the brothers Mbappé.. 🙈 @KylianMbappe
00:17
Celine Dept
Рет қаралды 52 МЛН
¡Puaj! No comas piruleta sucia, usa un gadget 😱 #herramienta
00:30
JOON Spanish
Рет қаралды 22 МЛН
2D PLATFORMER PATROL AI WITH UNITY AND C# - EASY TUTORIAL
7:31
Blackthornprod
Рет қаралды 156 М.
Simple 2D Enemy Patrolling Unity tutorial
9:34
MoreBBlakeyyy
Рет қаралды 48 М.
How to make a good platforming character (Developing 6)
14:50
Game Maker's Toolkit
Рет қаралды 410 М.
High precision speed reducer using rope
20:19
Aaed Musa
Рет қаралды 63 М.
2D PATHFINDING - Enemy AI in Unity
23:13
Brackeys
Рет қаралды 799 М.
Enemy AGRO AI System in Unity For Beginners ( 2D Game Dev Tutorial )
23:28
The Ultimate beginner's guide to AI with Unity & C#
19:31
Blackthornprod
Рет қаралды 102 М.