Great series man. I have spent countless hours watching other videos, reading forums and such to get solutions to all the problems you've addressed so far. This is a seriously STRONG source for basic solutions to common 2D Unity questions. Perfect for the mid-late beginner!
Great videos. Thanks for the hard work. One idea regarding the Log.CheckDistance() method. instead of making 2 calls to Vector3.Distance, why not cache the result in a local float? I know the two calls won't hurt much on performance but makes the code easier to read in the conditional. IE: private void CheckDistance() { float targetDistance = Vector3.Distance(_target.position, transform.position); if (targetDistance _attackRadius) { transform.position = Vector3.MoveTowards(transform.position, _target.position, _enemyMoveSpeed * Time.deltaTime); } } Just a thought. Oh, forgive the fields with the _ prefix, Like you with swapping collider with other, I like to use private fields and the [SerializeField] attribute. :) Thanks again for the awesome work..
If the log doesn't seem to be chasing you, it have something to do with the Z-axis value. If you change both 'Vector3' from the [if] statement in the 'log.cs' script to 'Vector2' it should work. You're welcome. ( ͡º ͜ʖ ͡º)
Pro tip from a rookie: pay attention to the underlines in Visual Studio. I just spent ~40 minutes trying to figure out why the log was attracted to the player no matter the values of the radii or distance from player. Turns out, that underlined semicolon at the end of the last IF statement screwed it all up. Oh man I feel like a noob! And to noobs, be warned... It's easy to get lost in the stacks of text. You can also change the colors of various elements in the program to help things be more apparent.
These tutorials are an absolute delight. I'm so glad a professional educator is doing this. Learning so much it makes me want to get started on my own games sooner than later.
Instead of using "FindWithTag", you can simply add a trigger to each enemy with a specific radius and target the player if he crosses it. Then you can just define OnTriggerEnter2D/OnTriggerExit2D to check if someone entered the radius and save a lot of un-needed code.
I have to commend this series for providing direct solutions for things we're trying to figure out. A lot of other content out there provides solutions that are a lot less palatable to new coders.
Hope to see that in future videos, just started the series, but... if you move something like the enemy with transform.position, you are ignoring the colliders. That means when an obstacle is between player and enemy the enemy is going trough the obstacle.
I changed the code slightly so that the CheckDistance() function is being called from FixedUpdate() and not Update(). I also apply the movement to the log's RigidBody and not the log's GameObject itself.
Unless its already fixed in a future video, all I would say is not to repeat the calculation of the Distance function, but save it in a variable then check both conditions. Also, couldn't you also add extra circle colliders with triggers to substitute as chase and attack radius?
This is an awesome series! you really do awesome work. One thing I do find interesting is that you gloss over a lot of the inner workings of unity and why we set certain things but spend a few minutes talking about basic OOP/inheritance. And you have done this in a few other videos. I guess the question is, is it common for people to have a better understanding of unity than coding basics? Genuine curiosity as I work in the industry but have never done game dev, so the unity workings is what I'm more interested in but I'm not sure the community at this point. Also, it may just be that because I'm a novice at unity and even though it seems like you're glossing over it, it may not need more description. ¯\_(ツ)_/¯
Ive been here for 2 hours and I cant figure out on 14:37 , chaseRadius, attackRadius and target dont load in, Ive saved it, let it compile and checked the code and its still not working, please helppppp :(
question: why wouldn't we just put all those declarations and methods from the log script in the enemy script? especially since they seem to be useful vars for other enemies
17:00 now what I like to do in those kind of games, is to not make the Player and Enemy Layer collidable. (This is possible by assigning the corresponding Layers to Player and Enemies and going into the physics 2D section of the Project Settings. Down Bottom there is a Collision Matrix and you simply uncheck the Enemy/Player box) This makes sense because it reduces Physics Calculation your game has to do and since we have Enemies dealing Touch Damage and applying Knockback to the Player there is no reason to have the calculation be done at all. In the Death function you can do whatever, but I would make it so the enemy has no longer the Player Target and goes along it's way (maybe roaming or moving towards home position or just Idling, depending on their behavior) and then I fade the Screen to the Game Over Scene.
I have an issue and would appreciate if anyone could help me with. I have an issue with tearing in my tiles for my overworld and I have tried to find a lot of solutions online, tired a few but still didn't work. So is there anyway to solve this issue? It's been extremely annoying to deal with. I'm still using Unity's 2D tile palette.
Hello! I agree the issue is very frustrating. I fix the problem in this series in part 21. It's in the first few minutes of the video, so you can get away with just watching the beginning, then jumping back to where you are. This should fix the issue. Let me know if it doesn't.
To help visualize the distance you could add the OnDrawGizmosSelectedMethod private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, chaseRadius); Gizmos.DrawWireSphere(transform.position, attackRadius); }
I have problem for FindWithTag("Player"). The notification:NullReferenceExecption: Object reference not set to an instance of an object. Pls help me to solve it
I set Log's Chase Radius to 4 and the Attack Radius to 2 but for Log will come to my player no matter how far away they are and it moves the player, despite having the same code as the video. Any advice?
*I have done enemy like yours, but Larry the log have only 1 walking animation. I can't force my NPC to start walking animation with current direction (X Y). Can I make this with movement script like yours? How?*
How could I do the movement using Velocity rather than transform.position, for the enemy? I used the code below but it's not a good solution - the enemy never actually gets to the player, nor to home position, therefore the difference between the vectors is never 0. Also, the greater the distance, the greater the speed - even if multiplied by moveSpeed, the same issue occurs. goblinRigidbody.velocity = new Vector3((target.transform.position.x - transform.position.x), (target.transform.position.y - transform.position.y)); //goblinRigidbody is the Rigidbody2D component on the enemy I think it's a very easy solution which just escapes me, does anyone have any suggestions?
Hey there. Yes, later on in the series I change what I did here to make a better system. I initially wanted a kinematic enemy to make the player not push it, but I found that if the hit and hurt boxes are used properly then you don't have to set the enemy to be kinematic. I think I cover this in video 20
Very interesting. Im not sure if these things were available 2018, but its possible to get the same functionality with a NavMeshAgent! For anyone doing this today :)
I do, but I sometimes change the order. As it stands here's what I'd like to do: Ranged AI 2 Health System Health UI Indoor Environments Knockback Second Enemy Type Dungeon layout Boss Battle Like I said, this list isn't definitive and will certainly change over time.
No inventory ? I find that part quite hard :D Quests ? I've worked on my system but I'd be interested to see yours :) (also interested in environment effects like rain/fog if you plan to do it)
I just had the same issue. It's not colliding with objects as the Rigidbidy2D is not Dynamic. Also if it keeps chasing you check your script. It did that for me and I had an ";" after the bracket in chaseRadius.
hey, I know its been 2 years but lmao I have a huge problem my enemy is always just teleporting to me and makes that glitch I used time.delta time and vector2 but still the same results
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Log : Enemy { public Transform target; public float chaseRad; public float attackRad; public Transform homePos; // Start is called before the first frame update void Start() { target = GameObject.FindWithTag("Player").transform; } // Update is called once per frame void Update() { CheckDistance(); } void CheckDistance() { if(Vector2.Distance(target.position, transform.position)
ok I fixed it lol in the last void in void checkdis i wrote target.transfom and it was meant to be transform normally( no target) great vid helped me a lot
haha, Thanks for watching. The pace is going to slow down a little now that school has started. . . I'll try to keep putting out two videos a week, though.
Yeah, understandable and I kinda figured since it became apparent you were a teacher. Expected things to slow down. My needs just exceeded release, even in the short term. Your videos are great though. I just had to start doing Inventory and Resource management code
Hi man, I don't think Mr Taft plans to do inventoy right now, but if you have question I'd be happy to discuss it with you. I've been working on this for a while now, and if I'm certainly not a pro I've read and watched tutorials in some quantity so I might be able to help. Let me know :)
@@CyberGenesis1 Don't be afraid to incorporate other tutorial series elements also from other great content creators like Blackthornprod or inScope Studios to name a couple. inScope has a video series that addresses inventory management and equipment management ruclips.net/video/XhQ-hNbi-Lo/видео.html Though I hope Taft makes a series on these topics because his teaching style is more my taste than the other content creators.
you need to work on the distance. i'm working on a sidescroller as well and wasnt having this problem. by any chance do you know how to make the .movetowards to only work on the x axis?
I prefer Inheritance vs Interface bc I can just use Virtual and override instead of writing the same code every time bc of the CONTRACT issue with interfaces
Sounds like your CheckDistance method might be always firing true. I'd start with checking your chase radius code and making sure it matches whats below and also make sure you are only calling Vector3.MoveTowards within the if statement: if (Vector3.Distance(target.position, this.transform.position) = attackRadius) { this.transform.position = Vector3.MoveTowards(this.transform.position, target.position, enemyMoveSpeed * Time.deltaTime); } Good luck! Hope you can get it sorted out.
Great series man. I have spent countless hours watching other videos, reading forums and such to get solutions to all the problems you've addressed so far. This is a seriously STRONG source for basic solutions to common 2D Unity questions. Perfect for the mid-late beginner!
thanks so much!
I have learnt more from this tutorial than I did from 1 year of basic C# programming in university.
So far your channel has offered the most answers to common questions a beginner encounters while making a game. Absolutely love your work and channel!
Great videos. Thanks for the hard work. One idea regarding the Log.CheckDistance() method. instead of making 2 calls to Vector3.Distance, why not cache the result in a local float? I know the two calls won't hurt much on performance but makes the code easier to read in the conditional.
IE:
private void CheckDistance()
{
float targetDistance = Vector3.Distance(_target.position, transform.position);
if (targetDistance _attackRadius)
{
transform.position = Vector3.MoveTowards(transform.position,
_target.position,
_enemyMoveSpeed * Time.deltaTime);
}
}
Just a thought. Oh, forgive the fields with the _ prefix, Like you with swapping collider with other, I like to use private fields and the [SerializeField] attribute. :)
Thanks again for the awesome work..
I have to say, this has been an amazing series and has already helped me immensely with the project I am working on.
Glad to help!
If the log doesn't seem to be chasing you, it have something to do with the Z-axis value.
If you change both 'Vector3' from the [if] statement in the 'log.cs' script to 'Vector2' it should work. You're welcome. ( ͡º ͜ʖ ͡º)
You saved my brain
still not working
Pro tip from a rookie: pay attention to the underlines in Visual Studio. I just spent ~40 minutes trying to figure out why the log was attracted to the player no matter the values of the radii or distance from player. Turns out, that underlined semicolon at the end of the last IF statement screwed it all up. Oh man I feel like a noob! And to noobs, be warned...
It's easy to get lost in the stacks of text.
You can also change the colors of various elements in the program to help things be more apparent.
Thanks for commenting about that!
how dumb am i right now that it was there
@@Smoah not dumb! Just easy to miss. One year ago I had a harder time scanning code than I do now. Just keep at it :)
These tutorials are an absolute delight. I'm so glad a professional educator is doing this. Learning so much it makes me want to get started on my own games sooner than later.
I have a problem with Log not colliding with any of the Collision tiles or Pots. Any ideas how to fix?
Instead of using "FindWithTag", you can simply add a trigger to each enemy with a specific radius and target the player if he crosses it.
Then you can just define OnTriggerEnter2D/OnTriggerExit2D to check if someone entered the radius and save a lot of un-needed code.
you are so cool
thank u
Bruh that log wasn't playing any games x_x
I made the box collider like you but the log can go through objects please help me
I have to commend this series for providing direct solutions for things we're trying to figure out. A lot of other content out there provides solutions that are a lot less palatable to new coders.
Thanks so much!
Hope to see that in future videos, just started the series, but... if you move something like the enemy with transform.position, you are ignoring the colliders. That means when an obstacle is between player and enemy the enemy is going trough the obstacle.
I changed the code slightly so that the CheckDistance() function is being called from FixedUpdate() and not Update(). I also apply the movement to the log's RigidBody and not the log's GameObject itself.
Step 1: Abduct Hero.
Step 2: OBLVION AWAITS!
I love this tutorial it's calm to listen and easy to learn
Dude you are a hero or more like my hero :)
The log is not moving towards the player, I have tried everthing I could think of, I need some help
did you find the solution?
Unless its already fixed in a future video, all I would say is not to repeat the calculation of the Distance function, but save it in a variable then check both conditions. Also, couldn't you also add extra circle colliders with triggers to substitute as chase and attack radius?
hey my log sometimes ignoeres the collision from the tile map and when it does that the log does not stop at the attack radius
Your tutorial videos are very helpful! Thanks for your work :)
thanks a lot for the series. Just learned how to do inheritance!
This is an awesome series! you really do awesome work. One thing I do find interesting is that you gloss over a lot of the inner workings of unity and why we set certain things but spend a few minutes talking about basic OOP/inheritance. And you have done this in a few other videos. I guess the question is, is it common for people to have a better understanding of unity than coding basics?
Genuine curiosity as I work in the industry but have never done game dev, so the unity workings is what I'm more interested in but I'm not sure the community at this point. Also, it may just be that because I'm a novice at unity and even though it seems like you're glossing over it, it may not need more description. ¯\_(ツ)_/¯
Ive been here for 2 hours and I cant figure out on 14:37 , chaseRadius, attackRadius and target dont load in, Ive saved it, let it compile and checked the code and its still not working, please helppppp :(
Thank you so much for these videos. You have given me the motivation and confidence to dive into making my own games. I can't thank you enough.
Why?(((( error CS0117: 'GameObject' does not contain a definition for 'FindWithTag'
question: why wouldn't we just put all those declarations and methods from the log script in the enemy script? especially since they seem to be useful vars for other enemies
I've had so much trouble with Rigidbody, I just gave up and made my own script for collision
Understandable. Unity's rigidbody system can seem to make more problems than it solves.
17:00 now what I like to do in those kind of games, is to not make the Player and Enemy Layer collidable. (This is possible by assigning the corresponding Layers to Player and Enemies and going into the physics 2D section of the Project Settings. Down Bottom there is a Collision Matrix and you simply uncheck the Enemy/Player box)
This makes sense because it reduces Physics Calculation your game has to do and since we have Enemies dealing Touch Damage and applying Knockback to the Player there is no reason to have the calculation be done at all. In the Death function you can do whatever, but I would make it so the enemy has no longer the Player Target and goes along it's way (maybe roaming or moving towards home position or just Idling, depending on their behavior) and then I fade the Screen to the Game Over Scene.
I have an issue and would appreciate if anyone could help me with. I have an issue with tearing in my tiles for my overworld and I have tried to find a lot of solutions online, tired a few but still didn't work. So is there anyway to solve this issue? It's been extremely annoying to deal with. I'm still using Unity's 2D tile palette.
Hello! I agree the issue is very frustrating. I fix the problem in this series in part 21. It's in the first few minutes of the video, so you can get away with just watching the beginning, then jumping back to where you are. This should fix the issue. Let me know if it doesn't.
actually great man
Larry the Log? Is the better name oof the life
What if We dont want diagonal movement on AI?
If your enemy is kinematic it can move through colliders :(
Is there a fix for this yet?
The enemy is still able to push the player from a top diagnol position though?????/
To help visualize the distance you could add the OnDrawGizmosSelectedMethod
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, chaseRadius);
Gizmos.DrawWireSphere(transform.position, attackRadius);
}
Thats a good tip!
Hi! You haven't done part 2 for this episode. I want to let the enemy pathfinding to the player if the play in range. can you help ??
Great series!!!!
I have problem for FindWithTag("Player").
The notification:NullReferenceExecption: Object reference not set to an instance of an object.
Pls help me to solve it
I also have this problem , if you figured it out would you mind letting me know how you fixed it
@@greave6720 have to tag the player as player under untagged
I set Log's Chase Radius to 4 and the Attack Radius to 2 but for Log will come to my player no matter how far away they are and it moves the player, despite having the same code as the video. Any advice?
fixed it! I somehow but a semicolon at the end of the if statement so it was constantly calling for it to go to the player
how can we make the log collide with other collidable stuff in the map other than the player
Change it from Kinetic to Dynamic
*I have done enemy like yours, but Larry the log have only 1 walking animation. I can't force my NPC to start walking animation with current direction (X Y). Can I make this with movement script like yours? How?*
Hey there. I cover this in a later part, I think 20 or something. I think the video is called enemy animations
How could I do the movement using Velocity rather than transform.position, for the enemy?
I used the code below but it's not a good solution - the enemy never actually gets to the player, nor to home position, therefore the difference between the vectors is never 0. Also, the greater the distance, the greater the speed - even if multiplied by moveSpeed, the same issue occurs.
goblinRigidbody.velocity = new Vector3((target.transform.position.x - transform.position.x), (target.transform.position.y - transform.position.y));
//goblinRigidbody is the Rigidbody2D component on the enemy
I think it's a very easy solution which just escapes me, does anyone have any suggestions?
If you set the logs body type to Kinematic it no longer respects the collision boxes of the scenery around it. Is there a fix?
Hey there. Yes, later on in the series I change what I did here to make a better system. I initially wanted a kinematic enemy to make the player not push it, but I found that if the hit and hurt boxes are used properly then you don't have to set the enemy to be kinematic. I think I cover this in video 20
@@MisterTaftCreates Others are having issues it seems. This might be a comment worth pinning?
Very interesting. Im not sure if these things were available 2018, but its possible to get the same functionality with a NavMeshAgent! For anyone doing this today :)
Great vid, as usual. Do you have a list of what you plan to do ?
I do, but I sometimes change the order. As it stands here's what I'd like to do:
Ranged AI 2
Health System
Health UI
Indoor Environments
Knockback
Second Enemy Type
Dungeon layout
Boss Battle
Like I said, this list isn't definitive and will certainly change over time.
No inventory ? I find that part quite hard :D
Quests ? I've worked on my system but I'd be interested to see yours :)
(also interested in environment effects like rain/fog if you plan to do it)
lool Larry the log. I have a coworker whose name Larry. I started calling him Larry the log. Awesome way to piss him off. Thank you LOOL
Well I hope not too much!
My log just chases me forever and he is also not colliding with objects except for the character ho do I fix this.
I just had the same issue. It's not colliding with objects as the Rigidbidy2D is not Dynamic. Also if it keeps chasing you check your script. It did that for me and I had an ";" after the bracket in chaseRadius.
he fixes the collider in video 20 i think
Log is not chasing me i need help
hey, I know its been 2 years but lmao I have a huge problem my enemy is always just teleporting to me and makes that glitch I used time.delta time and vector2 but still the same results
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Log : Enemy
{
public Transform target;
public float chaseRad;
public float attackRad;
public Transform homePos;
// Start is called before the first frame update
void Start()
{
target = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
CheckDistance();
}
void CheckDistance()
{
if(Vector2.Distance(target.position, transform.position)
ok I fixed it lol
in the last void in void checkdis i wrote target.transfom and it was meant to be transform normally( no target) great vid helped me a lot
Unfortunately i've caught up on all the videos available :(
haha, Thanks for watching. The pace is going to slow down a little now that school has started. . . I'll try to keep putting out two videos a week, though.
Yeah, understandable and I kinda figured since it became apparent you were a teacher. Expected things to slow down. My needs just exceeded release, even in the short term. Your videos are great though. I just had to start doing Inventory and Resource management code
Hi man, I don't think Mr Taft plans to do inventoy right now, but if you have question I'd be happy to discuss it with you. I've been working on this for a while now, and if I'm certainly not a pro I've read and watched tutorials in some quantity so I might be able to help. Let me know :)
@@CyberGenesis1 Don't be afraid to incorporate other tutorial series elements also from other great content creators like Blackthornprod
or inScope Studios
to name a couple. inScope has a video series that addresses inventory management and equipment management ruclips.net/video/XhQ-hNbi-Lo/видео.html
Though I hope Taft makes a series on these topics because his teaching style is more my taste than the other content creators.
5:02 the perfect battery percentage.
MINE JUST RUNS TO ME, WHERE EVER IT IS AUTOMATICLY
mine is a sidescroller and it still keeps moving
dont know what im doing wrong
you need to work on the distance. i'm working on a sidescroller as well and wasnt having this problem. by any chance do you know how to make the .movetowards to only work on the x axis?
I made my AI so it goes after you until it's at half your atk radius, because I wanted it to be moderately sticky.
THE BOSS MAN ...
I prefer Inheritance vs Interface bc I can just use Virtual and override instead of writing the same code every time bc of the CONTRACT issue with interfaces
Thanks so much for the comment!
Why not simply make player Kinetic???
the enemy doesnt stop chasing me ever even if i put my chase radius at 0
Sounds like your CheckDistance method might be always firing true. I'd start with checking your chase radius code and making sure it matches whats below and also make sure you are only calling Vector3.MoveTowards within the if statement:
if (Vector3.Distance(target.position, this.transform.position) = attackRadius) {
this.transform.position = Vector3.MoveTowards(this.transform.position, target.position, enemyMoveSpeed * Time.deltaTime);
}
Good luck! Hope you can get it sorted out.
@@aaron58107 its fixed but thanks so much man
thanks i wanst using this.transform
Will this chasing and attacking also works with 2d ?
5:26
My log is not moving towards me and my script is word for word any suggestions
ADD Tag and Put "player" Not"Player" idk why but for me it work
have you called the function inside update.
@@tigabx22 Lol, oddly enough, this worked for me too! changing the tag from "Player" to "player" in log.cs
14:40