Creating Simple Enemy AI to Chase the Player (Unity Tutorial | 2D Top Down Shooter)

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ต.ค. 2024

ความคิดเห็น • 75

  • @sawyerdewitt964
    @sawyerdewitt964 ปีที่แล้ว +17

    If you have the zombie spins in a circle around the character issue, you accidently changed the position of the graphics under Enemy. Just set the Enemy Graphics position to 0,0,0 and change the Enemy position to whatever you want.

    • @TheHockeyCentre1
      @TheHockeyCentre1 8 หลายเดือนก่อน +1

      You are a life saver, thankyou.

    • @Brandon12983
      @Brandon12983 8 หลายเดือนก่อน

      Lol I had the same problem, thanks

    • @uzi7462
      @uzi7462 7 หลายเดือนก่อน

      unfortunately changing these values didnt help me. I wonder what the issue is?

    • @robertcurley5120
      @robertcurley5120 7 หลายเดือนก่อน

      saved my life!!

    • @CodenBen
      @CodenBen หลายเดือนก่อน

      Oh, Thanks ! I searched a long time to fix this bzg 😮😂

  • @NERURAP
    @NERURAP 11 หลายเดือนก่อน

    I don't really know how to speak English or understand it very well but I still understood the tutorial perfectly, this channel deserves more support

  • @tranquil3727
    @tranquil3727 ปีที่แล้ว +5

    yay! I've been waiting for this one!

  • @uzi7462
    @uzi7462 7 หลายเดือนก่อน +1

    So if you're having the enemy run in circles issue like me here;s how i fixed it:
    -Increasing speed and rotation speed (My values are at: 7 speed, 200 rotation)
    -Setting transform values in Enemy and Graphics all to 0,0,0
    It possibly had something tot do with my characters movement speed. Still not 100% sure but this worked.

  • @ItsDailyDigest
    @ItsDailyDigest ปีที่แล้ว +12

    Hey guys if anyone has an issue where the zombie spins around the player in a circle to fix it set the child graphic transform to 0 , 0 , 0 and the enemy transform to 0 , 0 , 0 aswell and that should fix it :)

    • @CrawdadSoftware
      @CrawdadSoftware ปีที่แล้ว

      Yes, but you only need to change Y value and Z value, you can go with X where you want (10,0,0).

    • @darkestknight84
      @darkestknight84 8 หลายเดือนก่อน

      Thanks! This helped!

  • @bohnenstangenjim5293
    @bohnenstangenjim5293 ปีที่แล้ว +2

    Great video! Helped me so much. Thank you.

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Glad you found it useful 😊

  • @nogrilodoguede7515
    @nogrilodoguede7515 ปีที่แล้ว +2

    Awesome video, keep it up!

  • @kraessa7950
    @kraessa7950 9 หลายเดือนก่อน +2

    My enemy is not chasing my player, I followed all the steps but still not working.

  • @Huy-G-Le
    @Huy-G-Le หลายเดือนก่อน

    I'm using a different assets, and when ever the Player model go to one direction, their shoulders point in that direction and keep movie, but not their face, any suggestion?

  • @aiadtariq9964
    @aiadtariq9964 ปีที่แล้ว

    Awesome video

  • @neocoretec
    @neocoretec 8 หลายเดือนก่อน

    Maybe this question will be answered later in this series, or becomes obvious, but why use RigidBody2D's Body Type "Dynamic" instead of "Kinematic"? Isn't the former more expensive in terms of performance cost? At least the docs say so.
    Btw Thank you very much for this great series.

  • @darkestknight84
    @darkestknight84 8 หลายเดือนก่อน

    This tutorial series is great so far. I am learning so much. I finally came into a bit of a snag though. The code seems to be good and no errors show up. I even double checked it with the finished code in the description. But when I go into play mode the enemy doesn't chase me. The enemy does a weird circling pattern around me and freezes up. Any suggestions?

  • @justpie179
    @justpie179 11 หลายเดือนก่อน +3

    idk if anyone else is having this problem but the enemy goes up

  • @jasonwilliams8730
    @jasonwilliams8730 ปีที่แล้ว +2

    Nice!

  • @pr4shantishere
    @pr4shantishere ปีที่แล้ว +3

    I am always on time

  • @blankspace4293
    @blankspace4293 ปีที่แล้ว +1

    Hello! I was wondering if it makes a difference whether I use [SerializeField] or FindObjectOfType to make a reference (in this case to the players transform).

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, in the context of this video it would actually be more performant to use SerializeField. That way it wouldn't need to find the Player when the game starts. The reason we're using FindObjectOfType is that in the later videos we spawn more enemies at runtime, and it's not possible to assign the serialized field of the Player on the Enemy prefab. If you have a set number of enemies in the scene then you'll be OK to use SerializeField. Hope that makes sense 😊

    • @blankspace4293
      @blankspace4293 11 หลายเดือนก่อน

      @@KetraGames okie thank you!

  • @lucasdumeyniou6164
    @lucasdumeyniou6164 ปีที่แล้ว +1

    hi, thanks for your tuto but i have (32,40) and (34,59) : error CS1061 playerAwarenessController and AwareOfPlayer ; playerAwarenessController and DirectionToPlayer. but i don't understand why it doesn't work ?

    • @rawgamefootage
      @rawgamefootage ปีที่แล้ว

      you may of renamed the script for the player causing the gameobject to have a different name than what it has been renamed to. click the player and read the name of the script added via the inspector tab

  • @robinkiryenko7276
    @robinkiryenko7276 ปีที่แล้ว

    I keep getting the error CS0201: Only assignment, call, decrement, await, and new object expressions can be used as a statement

  • @jimmmybacon9043
    @jimmmybacon9043 3 หลายเดือนก่อน

    rigidbody.SetRotation(rotation); dosnt work, where did "SetRotation" come from?

    • @Huy-G-Le
      @Huy-G-Le หลายเดือนก่อน

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class PlayerAwarenessController : MonoBehaviour
      {
      // Start is called before the first frame update
      public bool AwareOfPlayer { get; private set;}
      public Vector2 DirectionToPlayer { get; private set; }
      [SerializeField]
      private float _playerAwarenessDistance;
      private Transform _player;
      private void Awake()
      {
      _player = FindObjectOfType().transform;
      }
      // Update is called once per frame
      void Update()
      {
      Vector2 enemyToPlayerVector = _player.position - transform.position;
      DirectionToPlayer = enemyToPlayerVector.normalized;
      if (enemyToPlayerVector.magnitude

  • @PatrickNusbaum
    @PatrickNusbaum ปีที่แล้ว +2

    Do we need to pass the delta time to `RotateTowards()` even if we run it from the fixed update?

    • @KetraGames
      @KetraGames  ปีที่แล้ว +3

      Hi, strictly speaking you don't need to as the deltaTime will always be the same value in FixedUpdate. However, by multiplying by deltaTime, it allows you to set the rotation speed to the number of degrees per second in the Inspector. Also, if the physics update rate was changed in the settings, the rotation rate would be affected without the deltaTime part.

  • @stevethepirate
    @stevethepirate 10 หลายเดือนก่อน

    i'm actually not sure when to replace the start method with awake(). what's the difference between the two?

    • @KetraGames
      @KetraGames  10 หลายเดือนก่อน

      Hi, we have a video dedicated to this - th-cam.com/video/9gtyn_ajzS8/w-d-xo.html
      In most cases it's not going to make a big difference, but the Awake method is called on all Game Objects before the Start method is called. It's good practice to initialize all your fields in the Awake method. Then if you need to call out to another script you would do it in the Start method. That way you know the other script has already initialised itself in its Awake method before you try to use it in the Stat method. Hope that helps 😊

    • @glebiank
      @glebiank 10 หลายเดือนก่อน

      Awake is called for EACH object on the scene, even for inactive ones (while Start is called before the first frame rendered with an object you are calling the Start method for)
      You never know the order of Awake methods to be called, so it's a bad idea to initialize something in Awake method, if this 'something' is dependent on the Awake method of another object
      Still, all the initialization is recommended to be performed in Awake method, you just need to be carefull with dependencies)

  • @miketadych9462
    @miketadych9462 ปีที่แล้ว

    Hi! I keep getting error CS0246 - "playermovement' could not be found.

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, check the spelling and casing of 'PlayerMovement' is the same everywhere. The PlayerMovement script itself and when we try to get the PlayerMovement component using FindObjectOfType() need to be exactly the same. In the series we've gone with 'PlayerMovement' with a capital 'P' and 'M'. Hope that helps 😊

  • @sociopathicfriends1150
    @sociopathicfriends1150 10 หลายเดือนก่อน

    what do i do about "operator "-" cannot be applied to operands of "Transform" and "Vector3" " ?

    • @glebiank
      @glebiank 10 หลายเดือนก่อน

      Transform type includes 3 fields of Vector3 type: position, rotation, scale. So you can't just perform '-' operation between three vectors in one variable and one-vector variable
      You have to choose a field in transform variable (transform.position, transform.rotation or transform.scale) depending on your current needs

  • @IsayBanj
    @IsayBanj ปีที่แล้ว +2

    Hi thanks for the tutorial, what if the sprite is facing the right ??

    • @KetraGames
      @KetraGames  ปีที่แล้ว +1

      Hi, if the sprite is facing to the right you can rotate the child object with the sprite renderer by 90 degrees. This video shows how to do this - th-cam.com/video/TfjL56tDAOc/w-d-xo.html. Hope that helps 😊

  • @evil_beast_z
    @evil_beast_z ปีที่แล้ว +2

    Hi Guys!

  • @Alexshmalexx
    @Alexshmalexx ปีที่แล้ว

    its saying that SetRotation is not defined by Rigidbody2D.
    How do i fix this?

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, did you manage to fix this? This video may help th-cam.com/video/DsDpwb6SJYI/w-d-xo.html

  • @rj360megaplayer9
    @rj360megaplayer9 ปีที่แล้ว

    Hello I'm having a warning about CS0618 pls help me

  • @Bergeet31
    @Bergeet31 ปีที่แล้ว

    it keeps saying the script could not be loaded

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, are there any errors in the console panel?

  • @classysoda
    @classysoda ปีที่แล้ว

    help me my enemies did not see in my map

    • @KetraGames
      @KetraGames  ปีที่แล้ว +1

      Hi, it may be to do with the order things are being drawn. This video may help th-cam.com/video/Hwq-_IyVlVI/w-d-xo.html

  • @johannes4510
    @johannes4510 ปีที่แล้ว

    the zombie is spinning around me

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, do you mean the zombie is circling you but never actually facing you? Maybe try increasing the rotation speed of the zombie so they can turn towards you faster.

    • @fortnitefor_life5868
      @fortnitefor_life5868 ปีที่แล้ว +1

      @@KetraGames This happened to me and I tried that but it still isn't working

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      I think the issue you're seeing is because we haven't added collisions yet, so the enemy passes through the player and then loops back round. If you do the next video in the series, it should all be fine 😊

    • @mythoredofficial
      @mythoredofficial ปีที่แล้ว

      @@KetraGames i m too having this issue

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, lots of people seem to be having this issue but we've not been able to reproduce. Is anyone able to share their project so we can see what's wrong?
      Also could you double check the rotation speed of the enemy prefab. It should be set to 100

  • @attrick4433
    @attrick4433 ปีที่แล้ว +1

    Its ok until I start to run it, then I get this error:
    NullReferenceException: Object reference not set to an instance of an object
    EmenyMovement.UpdateTargetDirection () (at Assets/spripts/EmenyMovement.cs:32)
    EmenyMovement.FixedUpdate () (at Assets/spripts/EmenyMovement.cs:25)
    How would I fix it?

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, make sure you've added the PlayerAwarenessController as a component of the Enemy. Hope that helps 😊

  • @sdp9956
    @sdp9956 ปีที่แล้ว

    Hi My enemy is moving round and round the player .In the PlayerAwarnessController the line _player = FindObjectOfType().transform; has been deprecated it seems Please help me solve my issue.
    Warning CS0618 'Object.FindObjectOfType()' is obsolete: 'Object.FindObjectOfType has been deprecated. Use Object.FindFirstObjectByType instead or if finding any instance is acceptable the faster Object.FindAnyObjectByType' Assembly-CSharp Assets\Scripts\Game\Enemy\PlayerAwarnessController.cs

    • @ItsDailyDigest
      @ItsDailyDigest ปีที่แล้ว

      Hey man if you have not managed to fix it set the child graphic to 0 , 0 , 0 and the enemy transform to 0 , 0 , 0 aswell that should fix it

  • @elsheshar999
    @elsheshar999 ปีที่แล้ว

    I have a problem when I try to compile the code, I get this message
    The property or index "EnemigoAtento.DireccionHaciaPersonaje" cannot be used in this context because the get accessor is inaccessible

    • @KetraGames
      @KetraGames  ปีที่แล้ว

      Hi, could you check that EnemigoAtento.DireccionHaciaPersonaje a public property?