No video

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

  Рет қаралды 253,639

Blackthornprod

Blackthornprod

Күн бұрын

Welcome to BlackthornProd !
In this tutorial I will bring you through the process of bringing to life an enemy that moves towards a certain target, stops when he is near enough, shoots and retreats !
All will be done using the Unity game engine and the C# programming language !
Here is the link to more AI video : • AI TUTORIALS WITH UNIT...

Пікірлер: 660
@Reilly18
@Reilly18 3 жыл бұрын
this is the most intense unity tutorial I have ever watched
@xdmcreamysickalien9067
@xdmcreamysickalien9067 3 жыл бұрын
so now i’m gonna put this PERFECT MADE RED CUBE IN MY PROJECT!!!...
@andresherrerasashidbis5533
@andresherrerasashidbis5533 3 жыл бұрын
I can feel his enegery trhough my screen
@maaz_dev
@maaz_dev 3 жыл бұрын
The nasty little square also shoots little bullet at our inoccent circle
@thiagoeli6658
@thiagoeli6658 3 жыл бұрын
InstaBlaster...
@aokisea
@aokisea 6 жыл бұрын
DUDE YOUR TUTORIALS ARE SO EASY TO LEARN! THANKS A LOT. MOST TUTORIALS ON KZfaq 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 жыл бұрын
Thanks !! I'm delighted to hear that my tutorials have helped you :) ! Many, many more are coming up so stay tuned !
@aokisea
@aokisea 6 жыл бұрын
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 5 жыл бұрын
"SHIT LIKE ME" HAHAHAHA I FEEL YOU BRO
@jahmaijones6331
@jahmaijones6331 5 жыл бұрын
@@lhenardabraham9068 That made me LOL hard!! haha
@edreactsto1493
@edreactsto1493 4 жыл бұрын
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 жыл бұрын
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.
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 3 жыл бұрын
lmao.
@payalshah3968
@payalshah3968 3 жыл бұрын
ya
@korodev3415
@korodev3415 3 жыл бұрын
THANK YOU. I’m currently participating in my first game jam, and I needed this ._. Pog
@theemeraldfalcon9184
@theemeraldfalcon9184 5 жыл бұрын
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 жыл бұрын
thank you bro
@b.c.5618
@b.c.5618 4 жыл бұрын
legend
@CRUMVIII
@CRUMVIII 4 жыл бұрын
Oh nice. Thank you. Was just thinking about how to apply this in 3D. -b
@kalempster
@kalempster 4 жыл бұрын
ye just use vector 3 and add z axis everywhere
@Husmanmusic
@Husmanmusic 3 жыл бұрын
Legend!
@renishadesra7336
@renishadesra7336 4 жыл бұрын
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 жыл бұрын
@@renishadesra7336 no it's alright thanks, I just stole the full 3d designed script from that other comment lol
@ParasBansal10
@ParasBansal10 3 жыл бұрын
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 жыл бұрын
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
@noellekkar903
@noellekkar903 4 жыл бұрын
Soooo underrated
@devastatingninja8361
@devastatingninja8361 4 жыл бұрын
He should pin it
@devastatingninja8361
@devastatingninja8361 4 жыл бұрын
@@DopEZTam check the program name
@devastatingninja8361
@devastatingninja8361 4 жыл бұрын
@@DopEZTam OK... Well nice videos... Which game is that in ur yt
@devastatingninja8361
@devastatingninja8361 4 жыл бұрын
@@DopEZTam oh ...can we play in phone?
@DawnosaurDev
@DawnosaurDev 6 жыл бұрын
Could you make a video on A* Pathfinding or any other method on 2D Pathfinding? There are very few Up to Date Videos on KZfaq. Thanks hope you like the suggestion.
@aaravjain9461
@aaravjain9461 4 жыл бұрын
Look at brackeys tutorial
@Mynnt
@Mynnt 3 жыл бұрын
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.
@thetoastedmarshmallow1606
@thetoastedmarshmallow1606 6 жыл бұрын
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.
@venomtailOG
@venomtailOG 5 жыл бұрын
Nice but wished it showed an option of the projectile still following after it reaches the targeted position
@dragneeladhoo6023
@dragneeladhoo6023 4 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
I think you can use the rigidbody of the projectile and add force ..
@pacmanprogramador1971
@pacmanprogramador1971 4 жыл бұрын
when I have not found an answer for a while, this person arrives and solves it in 12.54 seconds
@diliupg
@diliupg 4 жыл бұрын
He uses Black magic. When he was small his mother prodded him with a black thorn.
@razakhan651
@razakhan651 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?
@mehtabahmed4694
@mehtabahmed4694 4 жыл бұрын
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)
@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.
@regrettisouls9574
@regrettisouls9574 5 жыл бұрын
love your videos, you should do a 2d grappling hook tutorial
@plabfishchowder8314
@plabfishchowder8314 2 жыл бұрын
I highly enjoy these videos, you and a few others are the reason why I've wanted to make video games
@Max-zs8zr
@Max-zs8zr 6 жыл бұрын
>finds no useful video, gives up on A-level coursework. >comes back a month later and finds this. >MightGetAnALevel.jpg
@chradw
@chradw 5 жыл бұрын
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
@redhood7105
@redhood7105 6 жыл бұрын
Wow...KZfaq suggested your channel. Very cool videos so far. Keep it up mate!
@Blackthornprod
@Blackthornprod 6 жыл бұрын
Hey :) ! Thanks man that is awesome to hear ! I definitely intend to keep it up !
@danucleardude7443
@danucleardude7443 5 жыл бұрын
I LOVE YOUR ART STYLE!!!
@jcarbon3008
@jcarbon3008 5 жыл бұрын
bruv..its just squares and circles lol
@NACOSFOREVER
@NACOSFOREVER 5 жыл бұрын
OMG! my AI is alive!! Thank you blackthorn!
@user-kx5zv6nl8y
@user-kx5zv6nl8y 11 ай бұрын
🎯 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
@tobiasmercader8091
@tobiasmercader8091 3 жыл бұрын
this video is old but thank you, you helped me whit a problem i had for 3-4 consecutive days.
@torktumlarn1364
@torktumlarn1364 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
@ommehta5876
@ommehta5876 2 жыл бұрын
Thanks!
@user-hi9dd4dl9q
@user-hi9dd4dl9q 4 жыл бұрын
Dude, You have inspired me to continue with my indie games. Thank you so much
@derpzzzstudio5983
@derpzzzstudio5983 3 жыл бұрын
How is the progress?
@Oxmond
@Oxmond 4 жыл бұрын
Cool! Really nice beginner shooting game in just 12 min! Well done! 👍🤓
@soufianebenamer7585
@soufianebenamer7585 4 жыл бұрын
Thank you We miss your awsome tutorials
@garrytalaroc
@garrytalaroc 4 жыл бұрын
Man I love your enthusiasm
@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😊😊
@chakashakur5186
@chakashakur5186 6 жыл бұрын
Your vids are forever awesomely. Please never stop😊
@h3rnaldo
@h3rnaldo 3 жыл бұрын
Oh man, this is gold. Thank you for sharing!
@etrys3487
@etrys3487 4 жыл бұрын
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 4 жыл бұрын
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. }
@foreducation408
@foreducation408 2 жыл бұрын
you are awesome dude, thanks for keeping things simple and easy to understand.
@scuffedtechnical1154
@scuffedtechnical1154 3 жыл бұрын
That intro gave me chills
@PemburuEgo
@PemburuEgo 4 жыл бұрын
i use this to make turret.Thanks Noa!
@letmedoit.
@letmedoit. 4 жыл бұрын
Sir your way of explaining is just Really really awesome 👍 love from INDIA
@johnpaulmontessori3424
@johnpaulmontessori3424 6 жыл бұрын
Great videos man, you deserved my like.
@rezwansaki
@rezwansaki 5 жыл бұрын
Awesome Tutorial. Thanks for this. :)
@Jermz0r
@Jermz0r 6 жыл бұрын
really easy to keep up with and actually good codes keep em coming mate!
@siddeshmetrani9969
@siddeshmetrani9969 4 жыл бұрын
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
@jsonzu3277
@jsonzu3277 4 жыл бұрын
I like the figures that you've drawn
@quimerico
@quimerico 3 жыл бұрын
Awesome tutorial, dude! Thanks!
@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!
@Disthron
@Disthron 4 жыл бұрын
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
@liammelnick5919
@liammelnick5919 4 жыл бұрын
Exactly what I was looking for. Thanks.
@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.
@GabrielFerreira-ot8cg
@GabrielFerreira-ot8cg 4 жыл бұрын
Love your tutorials! I can easily adapt the code into my game!
@fc6827
@fc6827 3 жыл бұрын
Thank you so much for these.
@YasserSedrati
@YasserSedrati 6 жыл бұрын
great video 👏 can you do special 👌 tuto about particular effect 😊 (add (specification place) change ...)
@tomiselma
@tomiselma 6 жыл бұрын
Very good tutorial. And i've seen a lot of them. Thank you!
@Blackthornprod
@Blackthornprod 6 жыл бұрын
Thanks tomiselma :) !!
@adm1n901
@adm1n901 3 жыл бұрын
copy and paste code is in reply's. Watch the video though it gives good explanations.
@adm1n901
@adm1n901 3 жыл бұрын
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 жыл бұрын
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
@thinkernest2844
@thinkernest2844 6 жыл бұрын
you should continue doing tutorials! loved it
@01lmac67
@01lmac67 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 Жыл бұрын
Which variables did you create before void start?
@-halogenic-7263
@-halogenic-7263 3 жыл бұрын
This is so helpful for game jams!
@cruising84
@cruising84 4 жыл бұрын
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
@ultraslay7635
@ultraslay7635 4 жыл бұрын
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
@sife-i9n
@sife-i9n 3 жыл бұрын
this brackeys 2D version 😂
@lyricaldemon2537
@lyricaldemon2537 11 ай бұрын
Brackeys Codes never work
@mirandahernandez6030
@mirandahernandez6030 Жыл бұрын
Awesome Tutorial
@Rayan-bk3fn
@Rayan-bk3fn 3 жыл бұрын
Loved the video you explain well!
@MrMikhaelJackson
@MrMikhaelJackson Жыл бұрын
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!
@user-io5ye3un9o
@user-io5ye3un9o Жыл бұрын
I know its an old video but if you want to do it in 3d just change Vector2 to vector3
@lers0305
@lers0305 5 жыл бұрын
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..
@dstmayne2593
@dstmayne2593 4 жыл бұрын
You never dissapoint me :D
@theyearofrupert
@theyearofrupert 5 жыл бұрын
@Blackthornprod Awesome Videos. For faster prototyping you can right click inside Assets and Select Create > Sprites to make basic shapes.
@joaosantos45
@joaosantos45 6 жыл бұрын
Very Very Very good... Thank you.. great video
@mindterror2054
@mindterror2054 4 жыл бұрын
Thanks man your frickin awesome and dont deny it
@maxikurschner423
@maxikurschner423 6 жыл бұрын
Well done! Keep it up!
@reynielmahinay7028
@reynielmahinay7028 Жыл бұрын
Great video!
@gerardkris146
@gerardkris146 6 жыл бұрын
nice tutorial, ill make this a base for my work. thank you!
@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
@sturmente
@sturmente 4 жыл бұрын
Nice Tutorial dude
@eljal
@eljal 3 жыл бұрын
Soo good video dude!
@bungercolumbus
@bungercolumbus 4 жыл бұрын
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!
@lothar6323
@lothar6323 2 жыл бұрын
Thanks for tutorial!
@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 4 жыл бұрын
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.
@lhenardabraham9068
@lhenardabraham9068 5 жыл бұрын
My enemy starts shaking really fast when he stops from following.
@tealsquid7205
@tealsquid7205 5 жыл бұрын
4:40 check if your code matches up with this last line here
@michaeljpatrick
@michaeljpatrick 4 жыл бұрын
I had that problem too. If you adjust the Stopping Distance and Retreat Distance variables it should help.
@kent_s
@kent_s 5 жыл бұрын
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 5 жыл бұрын
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?
@ShockbotStudios
@ShockbotStudios 3 жыл бұрын
When I have multiple enemies, they pile on top of each other. Is there a way to fix this?
@sillicon8227
@sillicon8227 3 жыл бұрын
Add box collider
@sillicon8227
@sillicon8227 3 жыл бұрын
Or make another retreat function but save enemy as prefab and use that prefab for target of the second function
@deg4882
@deg4882 2 жыл бұрын
Thanks! is very very cool, i search this code for all's pages!
@muadhk
@muadhk 3 жыл бұрын
Morgz has really changed hasn't he. He now makes 2D games in Unity. (This is a joke)
@JonqsSimulationsOfficial
@JonqsSimulationsOfficial 4 жыл бұрын
thank you I had some problems but I fixed it!
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 3 жыл бұрын
ultra great n00b tuts. u rule.
@RajeevKumar-kq3ob
@RajeevKumar-kq3ob 5 жыл бұрын
Could you please make a complete series on top down 3d shooter. Please
@payalshah3968
@payalshah3968 3 жыл бұрын
you are great small videos and neat explanation thanks
@user-yv4gr2vf8o
@user-yv4gr2vf8o 4 жыл бұрын
Gooood man!
@lunalikestuna333
@lunalikestuna333 5 жыл бұрын
this is really easy thank you!
@valentinkadushkin324
@valentinkadushkin324 3 жыл бұрын
How can i make projectiles go further than current player position. In platformer it looks just like proejctiles are stuck en players previous location. How can i make them go further (in the same direction that they were flying)?
@Husmanmusic
@Husmanmusic 3 жыл бұрын
This is soo great!
@gohelboy
@gohelboy 4 жыл бұрын
Man thisi is useful but how can we set a range of Bullete if only Player goes enemies range than enemy start firing bullet..
@xAjido
@xAjido 11 ай бұрын
Praise KZfaq speed settings.
@vedrane0
@vedrane0 3 жыл бұрын
Wow That was really cool
@jamescraft5300
@jamescraft5300 3 жыл бұрын
Wow that was really cool
@calmsh0t
@calmsh0t 5 жыл бұрын
I got to say, these are some of the best and easiest to follow tutorials on youtube! One simple thing I don't understand though. Why are you using public, instead of [SerializeField] in order to make a value changeable via the editor? Since public has the downside of exposing your data out side the class? Is there a performance gain or some other benefit to it? Also, you are leaving raw code in update. Coming from an oop background, I was always told to group up my code in functions if possible. Does that apply to unity or would it slow down the game, if I would call the function in update, rather than the raw code? Thanks! And keep up the great work! :D
@OwlTeaGames
@OwlTeaGames 4 жыл бұрын
No reason for not splitting functions, only to show that it's possible. There's a lot of things you'd want to do, like Object Pooling, right? Well, that's another 15 minutes of talking, and it's easier to just say "Destroy the Projectile," than it is to say, "Disable the Projectile So It Stays In Memory, Allowing Us To Pool It Later." Once people get the basics, it makes the other stuff easier to understand.
@laurlore11
@laurlore11 5 жыл бұрын
Instead of writing player = GameObject.FindGameObjectWithTag(“Player”) can’t I just assign it in the inspector?
@miloverreijt6396
@miloverreijt6396 5 жыл бұрын
?
@rockogames245
@rockogames245 5 жыл бұрын
It's not assigning it it's looking for something with the tag and you have to assign it in the inspector. If you don't type the script won't work.
@OwlTeaGames
@OwlTeaGames 4 жыл бұрын
I assume you'd have more than one enemy, so your best bet would be to make a Prefab of an enemy in which you can swap out sprites, movement, and shooting methods by using a few nifty bools. However, your Player exists in the hierarchy. Objects from the Hierarchy cannot be dragged into Prefabs. When you're looking at a prefab, that prefab kinda *is* the hierarchy. And your actual game scene hierarchy is inaccessible! Blocked! Okay, you say, well, I'll just make my Player into a prefab, and then drag it from the Prefabs folder into the slot in the Enemy Prefab... Well, that's fine, but the prefab Player is not the same as the Player in the Hierarchy, so it won't work. Even if you instantiate the Player, the Prefab you put into that public slot is what's being called, not the Prefab currently used in the scene. Why? Dunno. It just is. It's weird, so that's why we use tags.
@ApexArtistX
@ApexArtistX 5 жыл бұрын
Do a platform enemy ai
@prathyushsai99
@prathyushsai99 5 жыл бұрын
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
@kavachmalhotra9765
@kavachmalhotra9765 4 жыл бұрын
I'm getting an error inline player = GameObject.FindGameObjectsWithTag("player").transform; any help is appreciated.
@GabrielFerreira-ot8cg
@GabrielFerreira-ot8cg 4 жыл бұрын
@Lazzzy_69 Bruh, I tried everything but this. Thank you very much!
@kavachmalhotra9765
@kavachmalhotra9765 4 жыл бұрын
@Lazzzy_69 thank you so much. I have a few more queries, It would be great if you could help me here. 1. I want my bullet projectile to constantly move in the same direction until it goes out of the screen. Not end at the target position as should in this tutorial. 2. when my player gets destroyed by the projectile. I get an error "The object of type 'Transform' has been destroyed but you are still trying to access it." 3. How to achieve that animation when the projectile hits something. (breaking into small circles and then disappearing). Thanks
@wozamigamez592
@wozamigamez592 3 жыл бұрын
Man Why isnt this code working the error is that whenever I write .Transform it gives error Gameobject cannot find Transform as the defination
@timce2
@timce2 3 жыл бұрын
most likely you typed it wrong. player = GameObject.FindGameObjectWithTag("Player").transform; edit: i now see your comment was 7 months old, well the comment might still help someone else
@sniffednuts
@sniffednuts 3 жыл бұрын
i was just wondering how you would be able to destroy the enemy upon hitting it with your own bullet?
@jesseramon5013
@jesseramon5013 3 жыл бұрын
kzfaq.info/get/bejne/rdF7ps9isszUZYE.html its very helpful
@soareverix
@soareverix 5 жыл бұрын
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 4 жыл бұрын
Thanks so much! However my bullets are shoot straight down, Do you know how to fix this?
@masoncrowe
@masoncrowe 4 жыл бұрын
Also do I need the Rigidbody part?
@soareverix
@soareverix 4 жыл бұрын
@@masoncrowe Hmm... did you select the correct target?
@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.
PATROL AI WITH UNITY AND C# - EASY TUTORIAL
9:29
Blackthornprod
Рет қаралды 154 М.
8 DEVS Make a GAME without COMMUNICATING!
13:21
Blackthornprod
Рет қаралды 980 М.
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 39 МЛН
а ты любишь париться?
00:41
KATYA KLON LIFE
Рет қаралды 2,9 МЛН
My Cheetos🍕PIZZA #cooking #shorts
00:43
BANKII
Рет қаралды 26 МЛН
Learning C# In A Week... Otherwise I Fail University
9:04
Переходишь с Unity на Godot? Посмотри это видео!
25:19
Тот Самый Келин
Рет қаралды 112 М.
6 DEVS Make a GAME without COMMUNICATING!!
13:53
Blackthornprod
Рет қаралды 1,7 МЛН
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 176 М.
When AI Plays Wordle..
21:49
Why Suda
Рет қаралды 11 М.
How I learned Unity without following tutorials (Developing 1)
18:11
Game Maker's Toolkit
Рет қаралды 2 МЛН
6 DEVS Make a GAME without COMMUNICATING! (HARDCORE edition)
20:12
Blackthornprod
Рет қаралды 1,7 МЛН
2D Enemy Shooting Unity Tutorial
12:52
MoreBBlakeyyy
Рет қаралды 55 М.
I Made the Same Game in 8 Engines
12:34
Emeral
Рет қаралды 4 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 39 МЛН