SHOOTING/FOLLOW/RETREAT ENEMY AI WITH UNITY AND C# - EASY TUTORIAL

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 658

  • @Reilly18
    @Reilly18 3 роки тому +95

    this is the most intense unity tutorial I have ever watched

    • @xdmcreamysickalien9067
      @xdmcreamysickalien9067 3 роки тому +9

      so now i’m gonna put this PERFECT MADE RED CUBE IN MY PROJECT!!!...

    • @andresherrerasashidbis5533
      @andresherrerasashidbis5533 3 роки тому +4

      I can feel his enegery trhough my screen

    • @maaz_dev
      @maaz_dev 3 роки тому +1

      The nasty little square also shoots little bullet at our inoccent circle

    • @thiagoeli6658
      @thiagoeli6658 3 роки тому +1

      InstaBlaster...

  • @aokisea
    @aokisea 6 років тому +149

    DUDE YOUR TUTORIALS ARE SO EASY TO LEARN! THANKS A LOT. MOST TUTORIALS ON UA-cam ARE QUITE COMPLICATED FOR ME. BUT YOUR'S IS JUST SO WELL MADE THAT EVEN SHIT LIKE ME CAN UNDERSTAND IT! THANKS SO MUCH!!!!!!

    • @Blackthornprod
      @Blackthornprod  6 років тому +16

      Thanks !! I'm delighted to hear that my tutorials have helped you :) ! Many, many more are coming up so stay tuned !

    • @aokisea
      @aokisea 6 років тому +5

      Blackthornprod Woooooow. I cant believe you actually noticd me! I really like your videos sir. its really helpin me out. If its ok. Can you make more tutorials about ai?

    • @lhenardabraham9068
      @lhenardabraham9068 6 років тому +3

      "SHIT LIKE ME" HAHAHAHA I FEEL YOU BRO

    • @jahmaijones6331
      @jahmaijones6331 6 років тому

      @@lhenardabraham9068 That made me LOL hard!! haha

    • @edreactsto1493
      @edreactsto1493 4 роки тому +1

      Im more shitter than you that i couldn't understand it in first time watching 😢.
      Gotta watch it a few more times and start writing the code on my computer to learn it

  • @danivalentine1280
    @danivalentine1280 4 роки тому +39

    Spent around 2 minutes trying to figure out what the heck a "project tile" was. Haha, great tutorial man, making my first fully finished game, a mini space shooter, and this helped out alot. You and Brackys are awesome.

  • @theemeraldfalcon9184
    @theemeraldfalcon9184 5 років тому +97

    for anyone doing this in 3d, here is the modified script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class EnemyBehavior : MonoBehaviour
    {
    public float speed;
    public float stoppingDistance;
    public float retreatDistance;
    public GameObject bullet;
    public Transform player;
    private float timeBtwShots;
    public float startTimeBtwShots;
    // Start is called before the first frame update
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    timeBtwShots = startTimeBtwShots;
    }
    // Update is called once per frame
    void Update()
    {
    if (Vector3.Distance(transform.position, player.position) > stoppingDistance)
    {
    transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    }
    else if (Vector3.Distance(transform.position, player.position) < stoppingDistance && Vector3.Distance(transform.position, player.position) > retreatDistance)
    {
    transform.position = this.transform.position;
    }
    else if (Vector3.Distance(transform.position, player.position) < retreatDistance)
    {
    transform.position = Vector3.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    }
    if (timeBtwShots

    • @vonstudios2028
      @vonstudios2028 4 роки тому +1

      thank you
      bro

    • @b.c.5618
      @b.c.5618 4 роки тому +1

      legend

    • @CRUMVIII
      @CRUMVIII 4 роки тому

      Oh nice. Thank you. Was just thinking about how to apply this in 3D. -b

    • @kalempster
      @kalempster 4 роки тому +2

      ye just use vector 3 and add z axis everywhere

    • @Husmanmusic
      @Husmanmusic 4 роки тому

      Legend!

  • @korodev3415
    @korodev3415 3 роки тому +11

    THANK YOU. I’m currently participating in my first game jam, and I needed this
    ._. Pog

  • @renishadesra7336
    @renishadesra7336 4 роки тому +1

    The clarity, simplicity and straight forward approach in the tutorial shows that you are someone who has made many games!
    Thanks for sharing your knowledge.

    • @minecraftmadlad3593
      @minecraftmadlad3593 4 роки тому

      Does this work for you? It won't let me put .transform at the end of the player find tag gameobject bit

    • @renishadesra7336
      @renishadesra7336 4 роки тому

      MinecraftMadlad yes it did work for me, just check the spelling and capitalisation once. If not Let me know I’ll give the script that worked for me

    • @minecraftmadlad3593
      @minecraftmadlad3593 4 роки тому +1

      @@renishadesra7336 no it's alright thanks, I just stole the full 3d designed script from that other comment lol

  • @Mynnt
    @Mynnt 3 роки тому +4

    This tutortial is really good! I actually made a better version (in my opinion) of the movement.
    public GameObject target;
    float distance;
    public float speed;
    void Update()
    {
    distance = Vector3.Distance(gameObject.transform.position, target.transform.position) - 6.5f; (higher = enemy stays farther away, can be anything)
    speed = distance;
    transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
    }
    This makes it so that the enemy speeds up depending on how far away the player is form the enemy.

  • @ParasBansal10
    @ParasBansal10 3 роки тому +1

    I was simling the whole time I was watching this. It was so intense. It felt like I was watching a conspiricy theory. Amazing tutorial BTW.

  • @solidlucho1
    @solidlucho1 4 роки тому +127

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Enemyshooting : MonoBehaviour
    {
    public float speed;
    public float stoppingDistance;
    public float retreaDistance;

    private float timeBtwShots;
    public float startTimeBtwShots;
    public Transform player;
    public GameObject projectile;
    // Start is called before the first frame update
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    timeBtwShots = startTimeBtwShots;
    }
    // Update is called once per frame
    void Update()
    {
    if(Vector2.Distance(transform.position, player.position) > stoppingDistance)
    {
    transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    } else if(Vector2.Distance(transform.position, player.position) > stoppingDistance && Vector2.Distance(transform.position, player.position) > retreaDistance)
    {
    transform.position = this.transform.position;
    }
    else if (Vector2.Distance(transform.position, player.position) > retreaDistance)
    {
    transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    }
    if(timeBtwShots

  • @thetoastedmarshmallow1606
    @thetoastedmarshmallow1606 6 років тому +1

    At first I thought is tutorial was going to be hard to understand because I've never done c# or any other form of coding except scratch. Except for the past 3 days. But scratch actually helps me understand the else if statements and stuff but it's actually really simple to understand! Great job. I'm going to try this later.

  • @DawnosaurDev
    @DawnosaurDev 6 років тому +51

    Could you make a video on A* Pathfinding or any other method on 2D Pathfinding? There are very few Up to Date Videos on UA-cam.
    Thanks hope you like the suggestion.

  • @harshshah8772
    @harshshah8772 4 роки тому

    damn after combing your previous video about top down shooting and combining it with the screen shake video and this my game has reached new heights. Thank you too much for these amazing tutorials.

  • @razakhan651
    @razakhan651 4 роки тому +4

    If you see that your enemy is shaking or vibrating constantly, you probably just need to set the stopping and retreat distance to not be equal (if they are already). Mine were both at 5 so I just changed the stopping distance to 5.2.

    • @dreambyte_studio
      @dreambyte_studio 2 роки тому

      Dude, thk you!!!!!

    • @Spider-Man_12345
      @Spider-Man_12345 Рік тому

      Ik this is a really old comment and you probably forgot about this at this point but is there explanation for why it shakes given other numbers?

  • @venomtailOG
    @venomtailOG 6 років тому +25

    Nice but wished it showed an option of the projectile still following after it reaches the targeted position

    • @dragneeladhoo6023
      @dragneeladhoo6023 4 роки тому +2

      Its easy, just put the "target = ......" code which is located at *start* into the *update* , so that _player.location_ is always updated, not Set.
      Note: to anyone who might have the same question

    • @keremaslan1988
      @keremaslan1988 4 роки тому

      @@dragneeladhoo6023 it didnt work

    • @dragneeladhoo6023
      @dragneeladhoo6023 4 роки тому +1

      @@keremaslan1988 you could try this amd check...
      public class HomingMissile : MonoBehaviour{
      public Transform target;
      public Rigidbody2D rigidBody;
      public float angleChangingSpeed;
      public float movementSpeed;
      void FixedUpdate () {
      Vector2 direction = (Vector2)target.position - rb.position; direction.Normalize ();
      float rotateAmount = Vector3.Cross (direction, transform.up).z;
      rigidBody.angularVelocity = -angleChangingSpeed * rotateAmount;
      rigidBody.velocity = transform.up * movementSpeed;
      }}

    • @dragneeladhoo6023
      @dragneeladhoo6023 4 роки тому

      Hope you can make it work. It works for me.

    • @sohaybalnajjar4968
      @sohaybalnajjar4968 4 роки тому +1

      I think you can use the rigidbody of the projectile and add force ..

  • @plabfishchowder8314
    @plabfishchowder8314 2 роки тому

    I highly enjoy these videos, you and a few others are the reason why I've wanted to make video games

  • @tobiasmercader8091
    @tobiasmercader8091 3 роки тому +1

    this video is old but thank you, you helped me whit a problem i had for 3-4 consecutive days.

  • @mehtabahmed4694
    @mehtabahmed4694 4 роки тому +1

    Quick tip if you want to Destroy a game object after a few seconds then just specify the time by doing Destroy(gameObject, write the time here)

  • @adm1n901
    @adm1n901 3 роки тому +3

    copy and paste code is in reply's. Watch the video though it gives good explanations.

    • @adm1n901
      @adm1n901 3 роки тому +2

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class Projectile : MonoBehaviour
      {
      public float speed;
      private Transform player;
      private Vector2 target;
      void Start()
      {
      player = GameObject.FindGameObjectWithTag("Player").transform;
      target = new Vector2(player.position.x, player.position.y);
      }
      // Update is called once per frame
      void Update()
      {
      transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
      if(transform.position.x == target.x && transform.position.y == target.y){
      DestroyProjectile();
      }
      }
      void DestroyProjectile(){
      Destroy(gameObject);
      }
      void OnCollisionEnter2D(){
      DestroyProjectile();
      }
      }

    • @adm1n901
      @adm1n901 3 роки тому +2

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class Enemy : MonoBehaviour
      {
      public float speed;
      public float stoppingDistance;
      public float retreatDistance;

      private float timeBtwShots;
      public float startTimeBtwShots;
      public GameObject projectile;
      public Transform player;
      // Start is called before the first frame update
      void Start()
      {
      player = GameObject.FindGameObjectWithTag("Player").transform;
      timeBtwShots = startTimeBtwShots;
      }
      // Update is called once per frame
      void Update()
      {
      if(Vector2.Distance(transform.position, player.position) > stoppingDistance){
      transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);

      } else if(Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance){
      transform.position = this.transform.position;
      } else if(Vector2.Distance(transform.position, player.position) < retreatDistance){
      transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
      }
      if(timeBtwShots

  • @redhood7105
    @redhood7105 7 років тому +2

    Wow...UA-cam suggested your channel. Very cool videos so far. Keep it up mate!

    • @Blackthornprod
      @Blackthornprod  7 років тому +2

      Hey :) ! Thanks man that is awesome to hear ! I definitely intend to keep it up !

  • @regrettisouls9574
    @regrettisouls9574 6 років тому +14

    love your videos, you should do a 2d grappling hook tutorial

  • @Max-zs8zr
    @Max-zs8zr 6 років тому +85

    >finds no useful video, gives up on A-level coursework.
    >comes back a month later and finds this.
    >MightGetAnALevel.jpg

    • @chradw
      @chradw 6 років тому +3

      haha im currently doing my A-level coursework too!, trying to do an aim training game with an AI. was super optimistic for mine during the planning stage and realised how hard it is so i've had to downgrade a lot.

    • @MontyEditz
      @MontyEditz 3 роки тому

      lol

  • @danucleardude7443
    @danucleardude7443 5 років тому +3

    I LOVE YOUR ART STYLE!!!

    • @jcarbon3008
      @jcarbon3008 5 років тому +6

      bruv..its just squares and circles lol

  • @ЦветославПаскалев-д9х

    Dude, You have inspired me to continue with my indie games. Thank you so much

  • @Oxmond
    @Oxmond 4 роки тому +1

    Cool! Really nice beginner shooting game in just 12 min! Well done! 👍🤓

  • @pacmanprogramador1971
    @pacmanprogramador1971 4 роки тому +2

    when I have not found an answer for a while, this person arrives and solves it in 12.54 seconds

    • @diliupg
      @diliupg 4 роки тому +2

      He uses Black magic. When he was small his mother prodded him with a black thorn.

  • @pranjaljoshi6454
    @pranjaljoshi6454 6 років тому

    You are awesome bro ,the way you present your video tutorials is awesome and you even reply to almost every comment to help them it's something i really appreciate. I wish you become more successful than you can even imagine😊😊

  • @etrys3487
    @etrys3487 5 років тому +1

    The not moving bullet can also be usefull feature, Its like an enemy placing mines that if you step on explodes. but for the explosion it costs a diffrent coding

    • @OwlTeaGames
      @OwlTeaGames 5 років тому +1

      You can *easily* place mines in the Projectile script!
      public bool isAMine = false;
      private void Update()
      {
      if ( isAMine )
      {
      //donut thing.
      }
      if ( !isAMine )
      {
      MoveEnemy();
      }
      }
      private void MoveEnemy()
      {
      //Move the code that used to be in the Update function here.
      }

  • @letmedoit.
    @letmedoit. 4 роки тому

    Sir your way of explaining is just Really really awesome 👍 love from INDIA

  • @foreducation408
    @foreducation408 2 роки тому

    you are awesome dude, thanks for keeping things simple and easy to understand.

  • @soufianebenamer7585
    @soufianebenamer7585 5 років тому

    Thank you
    We miss your awsome tutorials

  • @zombietron666
    @zombietron666 6 років тому +1

    OMG! my AI is alive!! Thank you blackthorn!

  • @themightyjinglesaj2522
    @themightyjinglesaj2522 6 років тому

    Yay! Thx for this. I implemented this in 3d and it worked just great. This video encouraged me to be creative with my code. Thx a lot.

  • @h3rnaldo
    @h3rnaldo 3 роки тому

    Oh man, this is gold. Thank you for sharing!

  • @garrytalaroc
    @garrytalaroc 4 роки тому

    Man I love your enthusiasm

  • @lers0305
    @lers0305 5 років тому +1

    Thanks a bunch :)
    I applied this to my current 2D platformer project and it works but I made an additional script using a 2D collider trigger that activates your follow script on trigger enter

    • @sarahcruz2103
      @sarahcruz2103 5 років тому

      can you show me how? for some reason when i add on trigger enter it doesnt fire the shoot script..

  • @chakashakur5186
    @chakashakur5186 6 років тому

    Your vids are forever awesomely. Please never stop😊

  • @user-io5ye3un9o
    @user-io5ye3un9o Рік тому +1

    I know its an old video but if you want to do it in 3d just change Vector2 to vector3

  • @ultraslay7635
    @ultraslay7635 4 роки тому +1

    You are awesome.your tutorial are really very easy and helpful.thankyou so much keep making your tutorial. I want a tutorial how to spawn enemies.please

  • @jsonzu3277
    @jsonzu3277 4 роки тому

    I like the figures that you've drawn

  • @xAjido
    @xAjido Рік тому +1

    Praise UA-cam speed settings.

  • @PemburuEgo
    @PemburuEgo 5 років тому +1

    i use this to make turret.Thanks Noa!

  • @theyearofrupert
    @theyearofrupert 5 років тому

    @Blackthornprod Awesome Videos. For faster prototyping you can right click inside Assets and Select Create > Sprites to make basic shapes.

  • @Jermz0r
    @Jermz0r 6 років тому

    really easy to keep up with and actually good codes keep em coming mate!

    • @siddeshmetrani9969
      @siddeshmetrani9969 5 років тому

      Hi,
      This is very nice video for player followed by enemy. I would like to understand the canon rotating in direction of player's position
      Regards
      Siddesh Metrani

  • @MrMikhaelJackson
    @MrMikhaelJackson 2 роки тому

    When he is retreating and I lead him to a wall and they both have colliders but the enemy clips through the wall, other than that great tutorial!

  • @GabrielFerreira-ot8cg
    @GabrielFerreira-ot8cg 4 роки тому

    Love your tutorials! I can easily adapt the code into my game!

  • @ballkicker2391
    @ballkicker2391 4 роки тому

    you are incredibly good teacher

  • @bungercolumbus
    @bungercolumbus 4 роки тому +3

    I have seen that lots of people wanted to have the bullet move beyond the players position.
    This command is used only on the EnemyBullet.
    public float moveSpeed = 7f;
    public Rigidbody2D rb;
    private Vector2 moveDirection;
    private Transform target;
    // Start is called before the first frame update
    void Start()
    {
    target = GameObject.FindGameObjectWithTag("Player").transform;
    moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
    rb.velocity = new Vector2(moveDirection.x, moveDirection.y + 0.5f);
    }
    This way every new bullet found will go towards the last player's position.
    You need to AddForce towards that position.

    • @DonutDev
      @DonutDev 4 роки тому

      the tutorial is still up to date right?

    • @desert5651
      @desert5651 4 роки тому

      Thx!

  • @fc6827
    @fc6827 3 роки тому +1

    Thank you so much for these.

  • @rezwansaki
    @rezwansaki 5 років тому +3

    Awesome Tutorial. Thanks for this. :)

  • @yunusmile
    @yunusmile 3 роки тому

    finally a tutorial i understood, subscribed, i am hoping to make a farming survival monster breeding game, if you can make any tutorial about those that would be great

  • @quimerico
    @quimerico 3 роки тому

    Awesome tutorial, dude! Thanks!

  • @tomiselma
    @tomiselma 7 років тому

    Very good tutorial. And i've seen a lot of them. Thank you!

  • @johnpaulmontessori3424
    @johnpaulmontessori3424 6 років тому

    Great videos man, you deserved my like.

  • @YasserSedrati
    @YasserSedrati 6 років тому +4

    great video 👏
    can you do special 👌 tuto about particular effect 😊 (add (specification place) change ...)

  • @liammelnick5919
    @liammelnick5919 4 роки тому

    Exactly what I was looking for. Thanks.

  • @MinhTuấnĐỗ-c3k
    @MinhTuấnĐỗ-c3k Рік тому +1

    I have a question. How to make enemy AI same as enemy in the bomberman game. thanks a lot

  • @thinkernest2844
    @thinkernest2844 6 років тому

    you should continue doing tutorials! loved it

  • @-halogenic-7263
    @-halogenic-7263 4 роки тому

    This is so helpful for game jams!

  • @deg4882
    @deg4882 2 роки тому

    Thanks! is very very cool, i search this code for all's pages!

  • @Rayan-bk3fn
    @Rayan-bk3fn 3 роки тому

    Loved the video you explain well!

  • @gerardkris146
    @gerardkris146 6 років тому

    nice tutorial, ill make this a base for my work. thank you!

  • @Leon_Jacques_Rossi
    @Leon_Jacques_Rossi Рік тому

    Awesome Tutorial

  • @cruising84
    @cruising84 5 років тому

    In the beginning, it sounds like you are talking to Conan who are about to go for a epic quest or something, easy now xD

  • @mindterror2054
    @mindterror2054 4 роки тому

    Thanks man your frickin awesome and dont deny it

  • @vs_3080
    @vs_3080 6 років тому

    Found a mistake!,
    Woaw! what is that man you've created a PUBLIC transform for the player gameobject and started finding it through the tag in the start method ( just blew my mind for a moment).which can be seen in the video before 5:24 and then smartly you've converted the public access specifier into a private variable in the later part of the video.
    and the second thing you don't need to use trasform.position = this.trasform.position; just use simply return if you want or else it's not required either.
    by the way your doing good job keep it up :D

    • @OwlTeaGames
      @OwlTeaGames 5 років тому

      I know, I saw that public and started freaking out. If the Enemy was a prefab, that slot would have been impossible to load with a Player from the Hierarchy.

  • @JonqsSimulationsOfficial
    @JonqsSimulationsOfficial 4 роки тому

    thank you I had some problems but I fixed it!

  • @prathyushsai99
    @prathyushsai99 6 років тому +2

    bro your tutorials are awesome i am also 17 and i want to get into game dovelopment for that your tutorials are very helpful for me

  • @payalshah3968
    @payalshah3968 3 роки тому

    you are great small videos and neat explanation thanks

  • @joaosantos45
    @joaosantos45 6 років тому +1

    Very Very Very good... Thank you.. great video

  • @Disthron
    @Disthron 5 років тому +7

    Hey man, this is a really cool tutorial, but you don't mention how to get the bullet to keep going past the player's position? Could you tell me how this might be done?

    • @bungercolumbus
      @bungercolumbus 4 роки тому

      @@noelmidenimstad6573 no. this will make the bullet miss the player. You need to instantly add a force to the bullet which will make it move to the point where the player stood when the bullet was instantiated.

    • @rachh6500
      @rachh6500 4 роки тому

      @@bungercolumbus help how do i do this

  • @LERPish
    @LERPish 7 років тому

    ive been watching some of your tutorials and i just wanted to tell you, you might be my new favorite tutorial guy yes you say lots of stuff i already know BUT i like the way you teach and explain stuff its like even with you telling me something that i already knew its almost like i learned something new and interesting i just find myself enjoying your tutorials a lot so yeah thx for your tutorials im liking your character creation videos (a thing that i would like that existed 5 years ago when i was trying to reproduce one myself) and overall your artstyle is really neat (havent watched this video just wanted to post this on your most recent video)
    p.s. sorry for my bad english

    • @LERPish
      @LERPish 7 років тому

      btw hyped to play fire of belief the game looks so cool it makes me remember of binding of isaac

    • @Blackthornprod
      @Blackthornprod  7 років тому

      That's so appreciated and motivating :) !! Thanks !

  • @soareverix
    @soareverix 5 років тому +2

    I can confirm that it works for 3D! Here's my version (I mostly just changed Vector2's to Vector3's)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ProjectileAim : MonoBehaviour
    {
    public float speed;
    private Transform player;
    private Vector3 target;
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    target = new Vector3(player.position.x, player.position.y, player.position.z );
    }
    // Update is called once per frame
    void Update()
    {
    transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    if (transform.position.x == target.x && transform.position.y == target.y && transform.position.z == target.z)
    {
    Destroy(gameObject);

    }
    }
    void OnCollisionEnter(Collision collision)
    {
    if (collision.collider.tag != "Obstacle" || collision.collider.tag == "null")
    {
    Destroy(gameObject);
    }
    }
    IEnumerator DestroySoon()
    {
    yield return new WaitForSeconds(2);
    Destroy(gameObject);
    }
    }

    • @masoncrowe
      @masoncrowe 5 років тому

      Thanks so much! However my bullets are shoot straight down, Do you know how to fix this?

    • @masoncrowe
      @masoncrowe 5 років тому

      Also do I need the Rigidbody part?

    • @soareverix
      @soareverix 5 років тому

      @@masoncrowe Hmm... did you select the correct target?

  • @01lmac67
    @01lmac67 3 роки тому +3

    use this to flip the sprite of the enemy
    void Start()
    {
    rb = GetComponent();
    }
    void FixedUpdate()
    {
    moveInput = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    if (facingRight == false && moveInput > 0)
    {
    Flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
    Flip();
    }
    }
    void Flip()
    {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
    }
    make sure to erase the Void Start or pste the code

    • @oreoking9513
      @oreoking9513 2 роки тому

      Which variables did you create before void start?

  • @RajeevKumar-kq3ob
    @RajeevKumar-kq3ob 5 років тому +1

    Could you please make a complete series on top down 3d shooter. Please

  • @sturmente
    @sturmente 4 роки тому

    Nice Tutorial dude

  • @reynielmahinay7028
    @reynielmahinay7028 2 роки тому

    Great video!

  • @lothar6323
    @lothar6323 3 роки тому

    Thanks for tutorial!

  • @torktumlarn1364
    @torktumlarn1364 4 роки тому +4

    Here is the full code
    ENEMY
    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using UnityEngine;
    public class Enemy : MonoBehaviour
    {
    public float speed;
    public float stoppingDistance;
    public float retreatDistance;
    private float timeBtwShots;
    public float startTimeBtwShots;
    public GameObject projectile;
    public Transform player;
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    timeBtwShots = startTimeBtwShots;
    }
    void Update()
    {
    if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
    {
    transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    }
    else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
    {
    transform.position = this.transform.position;
    }
    else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
    {
    transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    }
    if(timeBtwShots

  • @kevinandreselorzacortes3324
    @kevinandreselorzacortes3324 Рік тому +1

    Hey man! How can I make the bullet to keep going straight instead of just let it die after reaching the Player coordinates??? If anyone know PLEASE let me know

  • @Boxply
    @Boxply 3 роки тому

    behold! I show you all! The HAPPIEST coder that has ever walked (or sat on his chair coding) over the face of this planet!

  • @scuffedtechnical1154
    @scuffedtechnical1154 4 роки тому

    That intro gave me chills

  • @eljal
    @eljal 3 роки тому

    Soo good video dude!

  • @kent_s
    @kent_s 6 років тому +7

    How do you shoot the bullet in the direction instead of just the position? Meaning the bullet continues ahead after passing through the target position?

    • @dreamingacacia
      @dreamingacacia 6 років тому +1

      I develop 3D game, it's Vector3.forward. I think it'll be Vector2.forward ?

    • @foreversleepy4379
      @foreversleepy4379 5 років тому

      By having a velocity vector and adding it to the position of the projectile every frame, so that it will just carry on into the distance, but in the initial direction of the player.

    • @pipun8862
      @pipun8862 5 років тому

      @@foreversleepy4379 Any Idea on how to do this with the code we have here?

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 3 роки тому

    ultra great n00b tuts. u rule.

  • @umpteenthreason9627
    @umpteenthreason9627 5 років тому

    I'm gonna try to use this for an escort mission kind of code, i think it will work well when i remove the shooting part. Thanks!

  • @wojciechtomaszewski6319
    @wojciechtomaszewski6319 3 роки тому

    Side products in the process: I made 'deadly octopus' - without timeBtwShoots = startTimeBtwShoots shoot looks like big following hand with reach; and also mine setting monster - when projectile is still.

  • @bharadwajn.k9185
    @bharadwajn.k9185 3 роки тому

    So help full. Thank you

  • @maxikurschner423
    @maxikurschner423 7 років тому

    Well done! Keep it up!

  • @dstmayne2593
    @dstmayne2593 4 роки тому

    You never dissapoint me :D

  • @Misaplay
    @Misaplay 3 роки тому +1

    wow ..muito bom ...thanks

  • @hypermuffin84
    @hypermuffin84 2 роки тому

    This is amazing code, thanks.

  • @StefanSimonssonLinköpingFriaLä

    🎯 Key Takeaways for quick navigation:
    00:00 🎮 Introduction to creating a shooting enemy in Unity using C#.
    00:27 🚀 The enemy moves toward the player and stops at a certain distance. Retreats if player approaches.
    03:08 ⚙️ Using Vector2.Distance to check distance between player and enemy for movement logic.
    05:26 🔫 Implementing shooting mechanics with adjustable time between shots and projectiles.
    08:07 🛠️ Adjusting shooting frequency and adding projectile prefab to the enemy.
    10:21 💥 Detecting if the projectile reaches its destination and triggering its destruction.
    11:04 🔁 Using OnTriggerEnter2D to detect collision with the player and destroy the projectile.
    11:45 💡 Importance of Rigidbody2D and Collider2D components for collision detection.
    12:27 👍 Encouragement to ask questions and engage in the comments section.
    Made with HARPA AI

  • @Husmanmusic
    @Husmanmusic 4 роки тому

    This is soo great!

  • @lunalikestuna333
    @lunalikestuna333 5 років тому

    this is really easy thank you!

  • @phillipables3689
    @phillipables3689 6 років тому

    dang dude, FIRE TUTORIALS!!1

  • @javaexpertsa8947
    @javaexpertsa8947 6 років тому +1

    Please make even more Videos. I have watched the first video from you today and your code style is like mine. Short and not complicated. Already subscirbed and liked the Video. You deserve it.
    Can you make some Videos about 2D Character Design, i would love that. :)!!!

  • @vedrane0
    @vedrane0 4 роки тому

    Wow
    That was really cool

  • @ДенисЗаболоцкий-ф4л

    Gooood man!

  • @Diamond-yn4nu
    @Diamond-yn4nu 4 роки тому

    Very nice

  • @MarcelloBonello-k3y
    @MarcelloBonello-k3y 10 місяців тому

    Thank you very much