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.
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.
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 :)
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?
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.
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?
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).
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 😊
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 ?
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
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
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.
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 😊
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)
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 😊
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
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 😊
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.
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 😊
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?
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
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
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.
You are a life saver, thankyou.
Lol I had the same problem, thanks
unfortunately changing these values didnt help me. I wonder what the issue is?
saved my life!!
Oh, Thanks ! I searched a long time to fix this bzg 😮😂
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!
😊😊
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.
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 :)
Yes, but you only need to change Y value and Z value, you can go with X where you want (10,0,0).
Thanks! This helped!
Great video! Helped me so much. Thank you.
Glad you found it useful 😊
Awesome video, keep it up!
Thank you 😊
My enemy is not chasing my player, I followed all the steps but still not working.
me too
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?
Awesome video
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.
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?
idk if anyone else is having this problem but the enemy goes up
Nice!
👍😊
I am always on time
😊😊
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).
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 😊
@@KetraGames okie thank you!
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 ?
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
I keep getting the error CS0201: Only assignment, call, decrement, await, and new object expressions can be used as a statement
rigidbody.SetRotation(rotation); dosnt work, where did "SetRotation" come from?
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
Do we need to pass the delta time to `RotateTowards()` even if we run it from the fixed update?
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.
i'm actually not sure when to replace the start method with awake(). what's the difference between the two?
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 😊
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)
Hi! I keep getting error CS0246 - "playermovement' could not be found.
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 😊
what do i do about "operator "-" cannot be applied to operands of "Transform" and "Vector3" " ?
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
Hi thanks for the tutorial, what if the sprite is facing the right ??
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 😊
Hi Guys!
😊😊
its saying that SetRotation is not defined by Rigidbody2D.
How do i fix this?
Hi, did you manage to fix this? This video may help th-cam.com/video/DsDpwb6SJYI/w-d-xo.html
Hello I'm having a warning about CS0618 pls help me
it keeps saying the script could not be loaded
Hi, are there any errors in the console panel?
help me my enemies did not see in my map
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
the zombie is spinning around me
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.
@@KetraGames This happened to me and I tried that but it still isn't working
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 😊
@@KetraGames i m too having this issue
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?
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
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
Hi, could you check that EnemigoAtento.DireccionHaciaPersonaje a public property?