What’s up Game Developers? CouchFerret here, and today we’ll continue our archery game with our script to Shoot Arrows. We’ll Instantiate arrows
from a Prefab, and give them Velocity to move towards the enemy. Because this topic is a bit
bigger, I’ve broken it into two parts, so come back next Wednesday for part 2! If it sounds fun, then stay with me, and consider subscribing so you won’t miss any future videos of this game. Cool, let’s begin! First, need to draw the arrow before we can shoot it.
Let’s fire up Aseprite, the pixel art editor I use, and start drawing! When we finish,
we need to export it as a png for Unity. Let’s save it directly to our project’s directories.
Now in Unity, we can see that it’s a bit blurry. We have to set Filter Mode to Point (no filter)
and Compression to None, then hit Apply. Much better! Dragging and Dropping the Sprite into
the scene we can create an arrow GameObject. Unity automatically adds a Sprite Renderer
Component to it as well. To make arrows from script in the future, we need to create a
template for Unity to know what exactly an arrow is made of. In Unity, this template
is called a Prefab. Before we do anything, let’s create a folder for Prefabs. We can
create a Prefab by right clicking on the Project window and selecting Create/Prefab. This Prefab
will be empty, so we need to drag a Gameobject into it. However, it’s easier to just drag
and drop the gameobject from the scene into the Project window. Now, we have our arrow
Prefab, so we can delete the arrow gameobject we have in the scene. Let’s jump into the
code! We have to declare a variable for the arrow Prefab, to refer to it when we make
a copy of it. Save the code, and let’s fill the variable by selecting Keith and dragging
the Prefab from the Project window into the Player Controller script’s Arrow Prefab variable.
Notice the difference in the icons between a GameObject like the CrossHair and a Prefab
like the Arrow. Okay, let’s continue our script! From a previous episode, we have this if statement
to check whether we press the A button on an Xbox Controller or not. Now, we only print
out a Debug message which says FIRE!, so let’s change it to something a little more exciting.
We can make a copy of a Prefab by calling the Instantiate() function. Let’s type Instantiate(arrowPrefab,
transform.position, Quaternion.identity);. The first argument is the object we want to
make a copy of, the second its initial position, and the third is its starting orientation
or rotation. In our case the object is our arrow Prefab, the position is the archer’s
current position, and the orientation is the identity which means there is no rotation.
Let’s try it out! If I fire, then a bunch of arrows appear in the scene. The script
we wrote creates copies every single frame while we’re holding down the fire button. We need to change GetButton(“Fire”) into GetButtonDown(“Fire”), so it only shoots at the first frame after
we’ve pressed down the button, and not every frame. To give the freshly shot arrow velocity, first, we need to make a variable to store it temporally. Let’s declare one, name it
arrow and make it equal to the Instantiate part of the code. Before we can give it velocity,
the arrow needs to have a Rigidbody2D component, which gives the arrow physics behavior. Let’s
go back to the Unity Editor and select the arrow Prefab. Hitting Add Component in the
Inspector window, we can see all the available components we can add to our Prefab. Let’s
search for Rigidbody2D and add it to it. Now, we can go back to the script editor, and make
our arrows move. All arrows now have a Rigidbody2D component. This component has a variable,
called velocity which is a 2D vector with the movement direction in it. We can access
the current arrow GameObject’s Rigidbody2D component by typing arrow.GetComponent
refer to its velocity variable by typing .velocity to the end. Changing the velocity variable’s
value to a new Vector2(1.0f, 0.0f), we make the arrow move horizontally to the right.
Let’s try it out! Oh no, our arrow dropped like a stone. We have to get rid of the gravity
to let our arrows fly smoothly. Let’s select the arrow Prefab again, and in the Rigidbody2D
component, we need to change Gravity Scale to 0. If we test it now, then the arrows move
smoothly to the right. But its a bit slow, so let’s crank up the velocity a bit! Let’s
change the first value of the Vector2 to 5.0f. That’s much better! That’s it for today folks! Next
Wednesday, we will continue with part 2 of Shooting Arrows. We will fire arrows in any
direction and rotate them to match that direction as well. So be sure to subscribe and leave
a thumbs up! If you have any questions from today, feel free to ask and I’ll try to answer
all of them. See you next time!