Prototype: Projectile Offset

Introduction

In the last post, we added the ability for the player to shoot a projectile. To do so, I simply used the player’s current position as the position when instantiating the prefab.

Let’s make this look and feel better with an offset to position the projectile closer to the front of the player.

The Challenge

The challenge is to introduce an offset to the player’s position that will instantiate the prefab in front of the player, instead of the local center position.

Adding Vectors

To do this, we’ll be doing some extremely basic vector math; adding two vectors.

When two vectors are added, each respective element from the vector is operated on. For example, adding two vectors is done like this:

Vector3 vectorA = new Vector3(1, 0, 0);
Vector3 vectorB = new Vector3(0, 0.8f, 0);

Vector3 sum = vectorA + vectorB;
Debug.Log(sum); // (1.0, 0.8, 0.0)

Let’s add this in our Player script. For the prototype, I know I will not be modifying this offset value, so I’ll create a private Vector3 field and set the value to a new Vector3(0, 0.8f).

Adding Offset

Summary

We’re done! That small adjustment makes our prototype feel a bit better.

Stay tuned for the next post.
Take care.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s