Part 16 - Enemy AI Part 1: Make a game like Zelda using Unity and C#

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

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

  • @nibernator
    @nibernator 5 лет назад +61

    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!

  • @KayFabeMedia
    @KayFabeMedia 3 года назад +2

    I have learnt more from this tutorial than I did from 1 year of basic C# programming in university.

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

    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!

  • @MQNGameDev
    @MQNGameDev 5 лет назад +11

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

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

    I have to say, this has been an amazing series and has already helped me immensely with the project I am working on.

  • @EnigmaWho1993
    @EnigmaWho1993 5 лет назад +22

    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. ( ͡º ͜ʖ ͡º)

  • @aaronedvalson104
    @aaronedvalson104 5 лет назад +7

    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.

    • @MisterTaftCreates
      @MisterTaftCreates  5 лет назад

      Thanks for commenting about that!

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

      how dumb am i right now that it was there

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

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

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

    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.

  • @mzzzcz
    @mzzzcz 2 года назад +2

    I have a problem with Log not colliding with any of the Collision tiles or Pots. Any ideas how to fix?

  • @Saikodan99
    @Saikodan99 2 года назад +2

    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.

  • @justins7796
    @justins7796 5 лет назад +13

    Bruh that log wasn't playing any games x_x

  • @tudypie
    @tudypie 5 лет назад +2

    I made the box collider like you but the log can go through objects please help me

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

    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.

  • @FortbloxNET
    @FortbloxNET 5 лет назад +1

    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.

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

    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.

  • @Plexiux
    @Plexiux 3 года назад +2

    Step 1: Abduct Hero.
    Step 2: OBLVION AWAITS!

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

    I love this tutorial it's calm to listen and easy to learn

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

    Dude you are a hero or more like my hero :)

  • @spectrumelectrum1008
    @spectrumelectrum1008 3 года назад +2

    The log is not moving towards the player, I have tried everthing I could think of, I need some help

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

    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?

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

    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

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

    Your tutorial videos are very helpful! Thanks for your work :)

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

    thanks a lot for the series. Just learned how to do inheritance!

  • @HowToLoves
    @HowToLoves 5 лет назад

    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. ¯\_(ツ)_/¯

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

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

  • @piedon1051
    @piedon1051 5 лет назад

    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.

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

    Why?(((( error CS0117: 'GameObject' does not contain a definition for 'FindWithTag'

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

    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

  • @lee1davis1
    @lee1davis1 5 лет назад +3

    I've had so much trouble with Rigidbody, I just gave up and made my own script for collision

    • @MisterTaftCreates
      @MisterTaftCreates  5 лет назад +3

      Understandable. Unity's rigidbody system can seem to make more problems than it solves.

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

    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.

  • @kaptainriflebeard5980
    @kaptainriflebeard5980 5 лет назад +2

    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.

    • @MisterTaftCreates
      @MisterTaftCreates  5 лет назад +1

      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.

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

    actually great man

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

    Larry the Log? Is the better name oof the life

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

    What if We dont want diagonal movement on AI?

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

    If your enemy is kinematic it can move through colliders :(

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

      Is there a fix for this yet?

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

    The enemy is still able to push the player from a top diagnol position though?????/

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

    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);
    }

  • @truong-px
    @truong-px 4 года назад

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

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

    Great series!!!!

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

    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

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

      I also have this problem , if you figured it out would you mind letting me know how you fixed it

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

      @@greave6720 have to tag the player as player under untagged

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

    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?

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

      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

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

    how can we make the log collide with other collidable stuff in the map other than the player

  • @RiddleTerranceGolf
    @RiddleTerranceGolf 5 лет назад

    *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?*

    • @MisterTaftCreates
      @MisterTaftCreates  5 лет назад

      Hey there. I cover this in a later part, I think 20 or something. I think the video is called enemy animations

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

    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?

  • @jferris2514
    @jferris2514 6 лет назад +1

    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?

    • @MisterTaftCreates
      @MisterTaftCreates  6 лет назад +2

      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

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

      @@MisterTaftCreates Others are having issues it seems. This might be a comment worth pinning?

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

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

  • @thedoctor1471
    @thedoctor1471 6 лет назад

    Great vid, as usual. Do you have a list of what you plan to do ?

    • @MisterTaftCreates
      @MisterTaftCreates  6 лет назад +4

      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.

    • @thedoctor1471
      @thedoctor1471 6 лет назад +1

      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)

  • @LonelyDev71
    @LonelyDev71 5 лет назад

    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

  • @calebhixson2768
    @calebhixson2768 5 лет назад

    My log just chases me forever and he is also not colliding with objects except for the character ho do I fix this.

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

      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.

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

      he fixes the collider in video 20 i think

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

    Log is not chasing me i need help

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

    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

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

      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)

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

      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

  • @CyberGenesis1
    @CyberGenesis1 6 лет назад +2

    Unfortunately i've caught up on all the videos available :(

    • @MisterTaftCreates
      @MisterTaftCreates  6 лет назад

      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.

    • @CyberGenesis1
      @CyberGenesis1 6 лет назад

      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

    • @thedoctor1471
      @thedoctor1471 6 лет назад

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

    • @jungoogie
      @jungoogie 5 лет назад +1

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

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

    5:02 the perfect battery percentage.

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

    MINE JUST RUNS TO ME, WHERE EVER IT IS AUTOMATICLY

  • @alicadus
    @alicadus 5 лет назад

    mine is a sidescroller and it still keeps moving
    dont know what im doing wrong

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

      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?

  • @xavdest5481
    @xavdest5481 5 лет назад

    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.

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

    THE BOSS MAN ...

  • @lee1davis1
    @lee1davis1 5 лет назад

    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

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

    Why not simply make player Kinetic???

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

    the enemy doesnt stop chasing me ever even if i put my chase radius at 0

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

      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.

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

      @@aaron58107 its fixed but thanks so much man

    • @Aaron-tb3fc
      @Aaron-tb3fc 3 года назад

      thanks i wanst using this.transform

  • @Assniffer
    @Assniffer 5 лет назад

    Will this chasing and attacking also works with 2d ?

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

    5:26

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

    My log is not moving towards me and my script is word for word any suggestions

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

      ADD Tag and Put "player" Not"Player" idk why but for me it work

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

      have you called the function inside update.

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

      @@tigabx22 Lol, oddly enough, this worked for me too! changing the tag from "Player" to "player" in log.cs

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

    14:40