How do i make an object spawn in the direction in which the first person controller is facing?

I’m making a game where a block spawns in front of the first person controller. To do this i made the following code:

var thePrefab : GameObject;

function Update () {
	if (Input.GetMouseButtonDown(0)) {
    
    var instance : GameObject = Instantiate(thePrefab, transform.position + Vector3(-1,1,0), transform.rotation);
 }
}

But the object spawns only along one space in front of him on the x-axis do to “Vector3(-1,1,0)” in the code above…is there anyway to change that so that it spawns in the direction the first person controller is facing?

1 Answer

1

You should convert the desired position from local to world space with TransformPoint:

...
var pos = transform.TransformPoint(Vector3(-1,1,0));
var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation);
...