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 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.
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).
We’re done! That small adjustment makes our prototype feel a bit better.
Stay tuned for the next post.