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

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 154

  • @SweetVibesOfficialMusic
    @SweetVibesOfficialMusic 3 года назад +7

    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 2 года назад

    Man I have been watching different videos for two hours. Your code has been the only one that has worked. Thank you so much

  • @Bdarkan
    @Bdarkan 3 года назад +7

    Somehow you said everything I needed in 10 minutes when my teacher gave me nothing over a half hour

    • @tam_69420
      @tam_69420 Год назад

      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

  • @ArmorCroc
    @ArmorCroc 4 года назад +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!

  • @violala8279
    @violala8279 4 года назад +4

    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 :)

  • @HakiYikio
    @HakiYikio 2 года назад

    Thank you GameDevTV to bring this tutorial to you :D

  • @NathanOnline
    @NathanOnline 2 года назад +1

    Thank you so much!! This is the only tutorial I've seen that actually works and isn't 2 fps :DDD

  • @Smoky_da_cat
    @Smoky_da_cat 2 года назад

    I saw many videos but all of them were so complicated but yours so simple loved it

  • @puntalic
    @puntalic 3 года назад +1

    This tutorial earned you at least one new subscriber.
    Keep it up & thanks!

    • @kapkoder4009
      @kapkoder4009  3 года назад

      Glad it helped you! Thanks for the sub

  • @devmah
    @devmah 2 года назад

    A deep Thank you from Vietnam to you ;D

  • @spectrorobot7069
    @spectrorobot7069 Год назад +1

    great tutorial, very simple and to the point

  • @aizuki9261
    @aizuki9261 2 года назад

    Thank you very much this code was helpful for my universal project.❤❤❤

  • @Drana3308
    @Drana3308 Год назад +3

    How does the neemy detect they've hit a wall ?

  • @tam_69420
    @tam_69420 Год назад +3

    nice tutorial but im having trouble understanding why this works with walls and edges and not JUST edges?

  • @abdulkadir7c328
    @abdulkadir7c328 3 года назад +1

    This tutorial is so helpful that i will give you a sub, thx bro

  • @asasin8129
    @asasin8129 3 года назад +1

    better guide than Brackeys!

  • @mohammadnuriskiakbar7803
    @mohammadnuriskiakbar7803 4 года назад +1

    great and effective code. Thanks a lot

  • @patapizza3382
    @patapizza3382 4 года назад +7

    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 года назад +11

      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 4 года назад

      @@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  4 года назад +2

      @@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 2 года назад

      @@kapkoder4009 Thank you very much, I wish you luck

    • @CodingKricket
      @CodingKricket 2 года назад +1

      @@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.

  • @ciechanek8886
    @ciechanek8886 4 года назад +3

    Great tutorial! But how can i make him ignore collision with player controlled object?

    • @zedtix4971
      @zedtix4971 3 года назад +1

      void OnTriggerExit2D(Collider2D other)
      {
      if (other.tag == "ground" || other.tag == "wall")
      {
      transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y);
      }
      }

  • @Baize-ro4vz
    @Baize-ro4vz Год назад

    Thanks, it's really easy and useful.

  • @64_Tesseract
    @64_Tesseract 3 года назад +1

    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

  • @xqes2037
    @xqes2037 3 года назад

    it worked, thanks for that.

  • @felixn822
    @felixn822 4 года назад +2

    That's great, I just don't understand why it also turns when it hit the wall if it's an OnTriggerExit.

    • @kapkoder4009
      @kapkoder4009  4 года назад +1

      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

  • @atreyuslilcottage4053
    @atreyuslilcottage4053 4 года назад +1

    I'd love a tutorial on bullets/objects bouncing off of walls and destroying once they hit a certain point cx

  • @odalmer8110
    @odalmer8110 4 года назад +3

    how can I use another collider for detecting

  • @scriptsthefamous2250
    @scriptsthefamous2250 4 года назад +1

    nice awesome great epic fantastic

  • @ShahidZia-of1mk
    @ShahidZia-of1mk 2 года назад

    Which compiler are you using?

  • @Jeea84
    @Jeea84 2 года назад

    Thank you thank you thank you!!!!🙇‍♀

  • @imviciously
    @imviciously 3 года назад

    great tutorial :)

  • @dubcan4213
    @dubcan4213 4 года назад +3

    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 года назад +1

      @@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 4 года назад

      @@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 :)

  • @aaronmacasaet2284
    @aaronmacasaet2284 2 года назад

    thank you very much!

  • @Xelfist
    @Xelfist 2 года назад

    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?

  • @roberttorres3009
    @roberttorres3009 4 года назад +1

    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 2 года назад

      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.

  • @CodenTheSynth
    @CodenTheSynth Год назад

    the enemy is not only squished but just walks endlessly

  • @husseinrizk8933
    @husseinrizk8933 4 года назад +2

    That is GameDev 2D Game Dev Course on Udemy

    • @kapkoder4009
      @kapkoder4009  4 года назад +1

      Yup I just wanted to share it because the information isn't really accesable on youtube

    • @husseinrizk8933
      @husseinrizk8933 4 года назад

      @@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  4 года назад

      @@husseinrizk8933 I should put it in the description

    • @husseinrizk8933
      @husseinrizk8933 4 года назад

      @@kapkoder4009 I think you should

  • @noem4162
    @noem4162 4 года назад +6

    This Tutorial is Great! Just one thing, whenever my charactor turns his renderer disables.

    • @zedtix4971
      @zedtix4971 3 года назад +1

      void OnTriggerExit2D(Collider2D other)
      {
      if (other.tag == "ground" || other.tag == "wall")
      {
      transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y);
      }
      }

  • @nicedoodly6392
    @nicedoodly6392 2 года назад

    very very thanks😀

  • @linedoltrainerchannelgammi2247

    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

  • @kamalfaiz7286
    @kamalfaiz7286 2 года назад

    why i cannot use serializefield? is there some kind of extension that I need?

  • @gabrielacisleanu6192
    @gabrielacisleanu6192 Год назад

    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.

  • @isimsizanimator1291
    @isimsizanimator1291 2 года назад

    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?

  • @neozoid7009
    @neozoid7009 3 года назад

    How can i make an object wandering within the screen please help.🙏

  • @soyaboya6212
    @soyaboya6212 2 года назад

    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

    • @OzoneInteractiveMegan
      @OzoneInteractiveMegan Год назад

      are you using a composite collider on the tilemap? that should fix the issue

  • @simeqoff
    @simeqoff 4 года назад

    dude you climbed up youtube

  • @Justme-zq6bf
    @Justme-zq6bf 3 года назад

    really good tutorial but changing my move speed value dosn`t do anything. can someone help me ?

  • @gmodify9568
    @gmodify9568 2 года назад

    OnTriggerExit2D is making problems for some reason does anyone know why?

  • @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

  • @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 года назад +1

      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 года назад +8

      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 года назад +3

      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

  • @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 😊

  • @kuyabonbondevlog1044
    @kuyabonbondevlog1044 4 года назад

    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  4 года назад

      Make sure your ground check collider is set up properly, this does all your ground and wall detection

  • @holyrocketbunny2801
    @holyrocketbunny2801 3 года назад

    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  3 года назад +1

      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 3 года назад

      @@kapkoder4009 Thank you I will probably use pixels per unit method !

  • @uh3622
    @uh3622 2 года назад

    how do you do it so it has an animation when it moves?

    • @sansguerra2265
      @sansguerra2265 2 года назад

      Search for "Brackeys how to make animations 2d game in unity"

  • @lynai8273
    @lynai8273 3 года назад

    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  3 года назад +1

      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 3 года назад

      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 3 года назад

      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  3 года назад

      @@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

  • @Bdoll1993
    @Bdoll1993 10 месяцев назад

    couldn't you just flip the sprite instead of negating the localScale?

  • @chief9994
    @chief9994 3 года назад

    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  3 года назад

      Hmm... I wouldn't know what to do unless I saw the code

  • @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?

  • @brunoderkamramann7671
    @brunoderkamramann7671 2 года назад

    My trigger hitbox is not wroking, hes just walking straight in the air any ideas why?

  • @maurenaja
    @maurenaja 4 года назад

    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  4 года назад +1

      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

  • @SamiBeyondBorders
    @SamiBeyondBorders 3 года назад +1

    Hey my enemy makes that what he is supossed to do but he slowly sink in to the ground 😅

    • @kapkoder4009
      @kapkoder4009  3 года назад +1

      Maybe its rigidbody has gravity enabled? Or in your code your not adding 0 force on the y vector

    • @SamiBeyondBorders
      @SamiBeyondBorders 3 года назад

      @@kapkoder4009 i gonna try it

    • @SamiBeyondBorders
      @SamiBeyondBorders 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

    • @SamiBeyondBorders
      @SamiBeyondBorders 3 года назад

      @@kapkoder4009 but my rigidbody should be ok

    • @kapkoder4009
      @kapkoder4009  3 года назад

      @@SamiBeyondBorders 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 😉😉

  • @parker3739
    @parker3739 4 месяца назад

    I LOVE YOU

  • @mraxle9028
    @mraxle9028 3 года назад

    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  3 года назад +1

      Your code looks completely fine... my you need to configure your colliders??

  • @anirudhganesh7714
    @anirudhganesh7714 4 года назад

    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 8 месяцев назад

      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)

  • @maartenvanhove7965
    @maartenvanhove7965 Год назад

    I prefer this way of flipping a sprite:
    Vector3 localScale = transform.localScale;
    localScale.x *= -1f;
    transform.localScale = localScale;

  • @holyrocketbunny2801
    @holyrocketbunny2801 3 года назад

    for some reason, my enemy does not rotate and keeps walking through the tiles can somebody help ?

    • @holyrocketbunny2801
      @holyrocketbunny2801 3 года назад

      ok i fixed it i was missing the 2d on the OnTriggerExit2D

  • @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 года назад +1

      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 😂

  • @zedtix4971
    @zedtix4971 3 года назад +1

    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 2 года назад

      Thanks for your help! It fixed my problem.

  • @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?

  • @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

  • @songediter9101
    @songediter9101 3 года назад

    please help my enemy still not turn when he hits the wall :/

    • @kapkoder4009
      @kapkoder4009  3 года назад

      Try adjusting your wall detection collider, and make sure yours walls have colliders swell

  • @EMJAYYTK
    @EMJAYYTK Год назад

    This distorts my enemies resolution

  • @عموعمو-خ8ت
    @عموعمو-خ8ت 2 года назад

    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 года назад +2

      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 года назад +1

      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 года назад +1

      Haha thanks 😂

  • @jesseramon5013
    @jesseramon5013 3 года назад

    4:58

  • @RealCoachMustafa
    @RealCoachMustafa 4 года назад

    My guy never turns around, just keeps going straight. I even copied your code completely. What could I be doing wrong?

    • @RealCoachMustafa
      @RealCoachMustafa 4 года назад

      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  4 года назад

      Make sure your ground and walls have a collider and you set up your enemies collider correctly

  • @xingorro8605
    @xingorro8605 4 года назад

    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