Creating Simple Enemy AI to Chase the Player (Unity Tutorial | 2D Top Down Shooter)
In this Unity 2D tutorial we're going to look at how to create an enemy with some basic AI, that chases the player if they get too close.
This is the fourth video in our series showing how to make a full top down 2D shooter game in Unity.
The playlist for this series can be found here
► • Unity 2D Top Down Shoo...
The project files are available to our patrons here:
► / 75818131
Want to follow along? This project is a good starting point
► / 74343725
The final script can be found here
► dotnetfiddle.net/fwnJ6E
The assets used in this video can be found here
► tokka.itch.io/top-down-basic-set
Help support our work:
► Patreon: / ketragames
Follow us:
► Ketra Games: www.ketra-games.com
► Patreon: / ketragames
► Twitter: / ketragames
► Facebook: / ketragames
Introduction - 0:00
Creating the Enemy Game Object - 00:30
Creating the Player Awareness Script - 04:12
Creating the Enemy Movement Script - 07:14
Trying it out - 11:55
Summary - 12:24
#KetraGames #LearnUnity #UnityTutorials #UnityTips
Пікірлер: 71
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.
@Mannerson04
5 ай бұрын
You are a life saver, thankyou.
@Brandon12983
5 ай бұрын
Lol I had the same problem, thanks
@uzi7462
4 ай бұрын
unfortunately changing these values didnt help me. I wonder what the issue is?
@robertcurley5120
4 ай бұрын
saved my life!!
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
yay! I've been waiting for this one!
@KetraGames
Жыл бұрын
😊😊
Great video! Helped me so much. Thank you.
@KetraGames
Жыл бұрын
Glad you found it useful 😊
Awesome video, keep it up!
@KetraGames
Жыл бұрын
Thank you 😊
Awesome video
Nice!
@KetraGames
Жыл бұрын
👍😊
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
10 ай бұрын
Yes, but you only need to change Y value and Z value, you can go with X where you want (10,0,0).
@darkestknight84
5 ай бұрын
Thanks! This helped!
idk if anyone else is having this problem but the enemy goes up
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.
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?
I keep getting the error CS0201: Only assignment, call, decrement, await, and new object expressions can be used as a statement
Hi thanks for the tutorial, what if the sprite is facing the right ??
@KetraGames
Жыл бұрын
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 - kzread.info/dash/bejne/hpqerpdvpKenf8Y.html. Hope that helps 😊
Do we need to pass the delta time to `RotateTowards()` even if we run it from the fixed update?
@KetraGames
Жыл бұрын
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.
rigidbody.SetRotation(rotation); dosnt work, where did "SetRotation" come from?
My enemy is not chasing my player, I followed all the steps but still not working.
@montaarf1236
Ай бұрын
me too
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
Жыл бұрын
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
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.
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
9 ай бұрын
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
8 ай бұрын
@@KetraGames okie thank you!
what do i do about "operator "-" cannot be applied to operands of "Transform" and "Vector3" " ?
@glebiank
7 ай бұрын
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
Hello I'm having a warning about CS0618 pls help me
Hi! I keep getting error CS0246 - "playermovement' could not be found.
@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 😊
I am always on time
@KetraGames
Жыл бұрын
😊😊
i'm actually not sure when to replace the start method with awake(). what's the difference between the two?
@KetraGames
7 ай бұрын
Hi, we have a video dedicated to this - kzread.info/dash/bejne/a5uo29CYkc3gg5s.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
7 ай бұрын
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)
its saying that SetRotation is not defined by Rigidbody2D. How do i fix this?
@KetraGames
Жыл бұрын
Hi, did you manage to fix this? This video may help kzread.info/dash/bejne/dqd40tmbZrawiaw.html
it keeps saying the script could not be loaded
@KetraGames
Жыл бұрын
Hi, are there any errors in the console panel?
help me my enemies did not see in my map
@KetraGames
Жыл бұрын
Hi, it may be to do with the order things are being drawn. This video may help kzread.info/dash/bejne/equlj8GCqbnShqw.html
Hi Guys!
@KetraGames
Жыл бұрын
😊😊
the zombie is spinning around me
@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
Жыл бұрын
@@KetraGames This happened to me and I tried that but it still isn't working
@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
Жыл бұрын
@@KetraGames i m too having this issue
@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
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
Жыл бұрын
Hi, make sure you've added the PlayerAwarenessController as a component of the Enemy. Hope that helps 😊
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
@liammoodley9291
Жыл бұрын
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
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
Жыл бұрын
Hi, could you check that EnemigoAtento.DireccionHaciaPersonaje a public property?