Monster shadow

VIDEO

Point and Shoot / Turret Tutorial - Unity

Description:
DISCLAIMER: Replace the line: transform.up = Vector3.MoveTowards(transform.up, mousePosition, _rotationSpeed * Time.deltaTime); with: var dir = mousePosition - transform.position; transform.up = Vector3.MoveTowards(transform.up, dir, _rotationSpeed * Time.deltaTime); Learn how you can rotate your turret towards your mouse cursor (or anything you desire), fire projectiles, and kill enemies. Art Assets: https://imgur.com/a/W47V2UD Script: https://bit.ly/3l3LyGr =========
ADD A COMMENT

Walney moreira klein
very good tutorial, how spawn enemys any locals ?
DurVids
Great tutorial, thank you! Looking forward to more!
Егор Франц
Thank you friend!
srisrid multi purpose channel
Gooooooood
SEE ALL COMMENTS


Transcript:

in this video i'll show you how to create a turret which follows your mouse
cursor shoots projectiles and kills your
enemies all right first thing is we'll need our
turret so i've drawn these two sprites here and
i'll attach them in the description if you would like to use
these exact ones just to test uh just one note whatever turret you use
make sure that the pivot point is exactly where you would like it to
rotate or else it will look a little bit strange uh next let's create a
c-sharp script call this turret and let's actually attach it to the
rotating part of our turret open it up all right first we need to
get our mouse position so let's grab a camera
which we'll save a reference to so camera
then in our awake function let's set that to camera dot main
now we can say cam dot the screen to world point and let's actually put in
our mouse position now as we're using 2d we don't need a
z-axis so let's zero that out so if we head back into unity
we'll see this green arrow here is the up
rotation or the up direction and all we effectively need to do
is point our up direction towards the mouse
cursor okay and it's as simple as that so i'll show you how to do that
transform dot up equals mouse position and if we press play now that should be
all we need to actually look towards our camera uh
our pointer now we can add a little bit to this we
can add a little bit of uh realism so we could create a
float and this could be rotation speed and let's default this to one
and actually we could make this a range slider and we'll default it from
minimum one maximum 100 which should probably be just about instant
and now instead of setting it directly to mouse position we can set it to
vector uh move towards and our current will be the transform up
and our goal is the mouse position and then
it will be rotation speed times time dot delta time
have a look yeah and there you go so now it's kind
of like slowly moves towards it that's obviously painfully slow so if we
go up to nine or something there's just like that tiny little bit of lag to it
and it makes it feel like a little bit better than just snapping
to it in my opinion probably not for the player but
uh if it's an enemy you obviously don't want it just
constantly just aim locking on the player you would you'd want to give the
player a little bit of a chance so let's actually shoot something from
this turret um i've already got a projectile here but i'm going to
destroy them and delete them just so i can build it from scratch with you guys
so i've got this little projectile that i drew
a little plasma ball or something uh what i'm going to do is i'm going to
attach a circle collider to it i'm going to attach and that's way too
big uh a rigid body 2d and i'll turn off my
gravity scale and then let's write a c sharp script
call this projectile
i'll attach it and open up the projectile script
okay so first we need a reference to uh rigidbody as that's how we were
going to propel the the projectile
and we'll need to set how fast the projectile
flies so let's have a speed variable and now we need a
way for the turret to tell the bullet what to do once it's spawned
so i'm going to make an init function here and it's going to send in
a direction so all we need from the turret is which direction it's currently
facing and then in the in the projectile we can
say a rigidbody velocity equals direction times
speed then we'll need to actually uh destroy the enemy if we hit them and
then also destroy the projectile itself so
let's go to let's hook into the on collision enter
2d and this takes a collision 2d and
so i have got an enemy script here i'll just quickly show you it's
literally just got an explosion prefab and a function called
die so what i'm going to do is i'm going to
say cold.transform get component and this is easy as the
enemy is the only thing in the scene and i'll just call die on it and then
i'll also destroy this projectile itself and
that's all we need in the projectile script
okay on our projectile which my face is blocking move myself
over here on the speed let's set it to something like three and then
let's send in our rigid body and make a prefab of this projectile
just like that now back on our turret scripts
okay we need a reference to our projectile prefab
whoops projectile prefab and i cannot spell that word projectile
prefab and yet again my face is in the way you go really small now
over there up there in uh update function let's check for input
get mouse button down and left click and then when that happens let's
instantiate a projectile prefab now it would be nice
if instead of it spawning just directly in the middle of the turret it spawns
kind of like at the at the um what do you call it
at the end at the end you know what i mean
spawn point i will spawn it at the spot
point.position and the identity is fine for the rotation
now if you remember on the projectile script we've got this init function here
so let's immediately call inits and our direction will be transform dot
up back to unity
on our turret so we need to now put in the projectile prefab so let's chuck
that in and then this spawn point so i will
create an empty game object here and call it spawn
and this will be right here on the barrel the barrel i guess i don't
know all right let's give that a try and that
is incredibly slow projectile so let's just speed that up a little bit
crazy rain outside hopefully that doesn't destroy the sound but
there we go uh so hopefully that helped you if it did
leave a like subscribe and i'll see you next time for the next
tutorial you