Creating a Laser Game Object Offset Position in Unity

Objective: To create an offset position for our laser to fire from instead of being in the middle of the player in Unity.

Now that we have set up our laser code to instantiate when we press the Space key and fire upwards, it is time to create a small offset so that the laser itself isn’t being shot from inside the middle of the player:

Lasers being fired from the middle of the player

Let’s Add Some Code:

Laser offset

Let’s break this down:

public Vector3 laserOffset = new Vector3(0, 1, 0);
// create a new variable called laserOffset of type Vector3 and assign it a new Vector3 with X = 0, Y = 1and Z = 0. It is also best at this stage to leave it as a public variable so you can play around with the Vector3 coordinates in the Inspector.

Instantiate(laserPrefab, transform.position + laserOffset, Quaternion.identity);
// Now let’s add the laserOffset Vector3 values to the players transform.position.
E.g. If our players current transform.position is = 0, 0, 0 then when we append the laserOffset values of = 0, 1, 0 it will now be = 0, 1, 0. So every time the laser is fired, it will be instantiated 1 unit higher on the Y-axis from the players current transform position as it is a positive value.

Useful Links:

Aspiring Unity Game Developer with a passion for bringing code to life!