It is time for our final item class, which is the projectile item class. These kinds of items could be bullets, arrows, thrown items, and so on. The projectile item class will be similar to the melee item class, except this one will have functions that will allow it to move in the game world. We'll start by creating a new script and naming it itemRanged
.
As we did in the previous two classes, we'll need to first add a few enums to our script. Add these variables to our script:
public enum RangedAction {BuffDebuff, ChangeHP, ActivateEnv, None}; public enum RangedType {Weapon, None}; public enum MovementType {Basic, Drop, None};
You can see that we have a couple of familiar variables that we will use for the action and type of the item. We also have a new enum; this one will be used to determine how the object will move when it's created. The basic type will move the object through the air with simple movement. The drop type is similar to the basic type, but will allow the object to drop in the air as if gravity was acting on it.
Now, let's add the rest of our variables:
public int Amount, Value; public float Weight, Speed, DropSpeed; public string Name, Stat; public RangedAction rangedAction = RangedAction.None; public RangedType rangedType = RangedType.None; public MovementType moveType = MovementType.None;
As you can tell, many of these variables are similar to the ones we previously used. These variables are typical to our items; the only one that is different is the MovementType
enum. Now let's move on to adding our functions.
Let's allow our projectile to affect enemy stats; add this function to our script:
void BuffDebuffStat(GameObject other) { other.SendMessage("BuffDebuffStat", new KeyValuePair<string, int>(Stat, Amount)); }
Just like the melee item, we receive the GameObject that the projectile collides with. Then, we send a message to that object to call a function and send it a KeyValuePair
variable.
Our next function will allow our projectile to do the most common effect that projectiles have, which is hurt or heal others. Let's add the following function to our script:
void ChangeHealth(GameObject other) { other.SendMessage("ChangeHealth", Amount); }
This function should be familiar; it acts the same way as the one we used in the melee item.
These next few functions will add movement to our projectile. We have two kinds of movements, so we'll separate them into two different functions:
void BasicMovement() { transform.Translate(Vector3.forward * (Time.deltaTime * Speed)); } void DropMovement() { transform.Translate(new Vector3(0, DropSpeed, 1) * (Time.deltaTime * Speed)); } void Update() { switch(moveType) { case MovementType.Basic: BasicMovement(); break; case MovementType.Drop: DropMovement(); break; } }
In the Update
function, we check the moveType
variable in a switch
statement to determine how the projectile will move through the air. Depending on the value you assign to it, it'll either call the BasicMovement
function or the DropMovement
function. Let's take a look at the BasicMovement
code:
transform.Translate(Vector3.forward * (Time.deltaTime * Speed));
Here we set the transform of the GameObject to move forward in the z axis. We multiply the movement Vector
by deltaTime
and our Speed
variable. The Speed
variable will allow you to control how fast or slow the projectile will go:
Now let's take a look at the DropMovement
code:
transform.Translate(new Vector3(0, DropSpeed, 1) * (Time.deltaTime * Speed));
This line is similar to the BasicMovement
line, but our movement Vector
is different. We use the DropSpeed
variable in the y axis to make our projectile drop to the ground. It will appear as if gravity is acting on our projectile, giving it a more realistic appearance. Dropping the projectile will also make it a little more difficult for the player to attack, adding a new mechanic to the game.
Now we'll add detection method to our projectile. We will use a similar system that we used in the melee item class. Add the following code to your script:
void OnTriggerEnter(Collider col) { switch(col.gameObject.tag) { case "Enemy": if(rangedType == RangedType.Weapon) { if(rangedType != RangedType.None) { if(rangedAction == RangedAction.ChangeHP) ChangeHealth(col.gameObject); if(rangedAction == RangedAction.BuffDebuff) BuffDebuffStat(col.gameObject); if(rangedAction == RangedAction.ActivateEnv) ActivateEnvironment(col.gameObject); } } break; case "Environment": if(rangedType != RangedType.None) { if(rangedAction == RangedAction.ChangeHP) ChangeHealth(col.gameObject); if(rangedAction == RangedAction.BuffDebuff) BuffDebuffStat(col.gameObject); if(rangedAction == RangedAction.ActivateEnv) ActivateEnvironment(col.gameObject); } break; } Destroy(gameObject); }
Just as in the melee item class, we use triggers to detect whether the projectile has hit something; if it does, we take the collider of that GameObject. Once we have detected the collision and received the collider, we follow these steps to decide what to do next:
switch
statement, we use the tag of the GameObject collider to check what it's colliding withrangedType
variable isn't equal to None
if
statements to see what action we are usingAt first, it may look confusing, but we are really just following a step-by-step process of logic to decide what our projectile should do. With this, we conclude the projectile item class as well as all of our item classes we created. Next, we move on to playtesting!
18.226.251.70