# Instantiate prefab behind moving player

I'm trying to instantiate a bomb prefab behind the player.

The script below works fine when the y rotation is 0,90,180,360. The bomb is instantiated behind the player.

But for other angles, the bomb is not instantiated directly behind the player. Is there any way to use maybe sin/cos functions (depending on the y rotation) to get the bomb to instantiate directly behind the player?

I'm using a cube as the player and sphere as bomb. The sphere is scaled to 0.3.

private float yRotation;

void FixedUpdate()
{
transform.position += transform.forward * speed;
float rotation = Input.GetAxis("Horizontal");
transform.Rotate(Vector3.up * rotation * 180.0f * Time.deltaTime);//Keyboard movement

yRotation = transform.localEulerAngles.y;

if (Input.GetKey(KeyCode.B))
{
PlaceBomb();
}
}

void PlaceBomb(){

if (yRotation >= 0 && yRotation < 90)
{
Instantiate(bombPrefab, new Vector3(transform.position.x, 0, transform.position.z - 1f), Quaternion.identity);
}
if (yRotation >= 90 && yRotation < 180)
{
Instantiate(bombPrefab, new Vector3(transform.position.x - 1, 0, transform.position.z), Quaternion.identity);
}
if (yRotation >= 180 && yRotation < 270)
{
Instantiate(bombPrefab, new Vector3(transform.position.x, 0, transform.position.z + 1), Quaternion.identity);
}
if (yRotation >= 270 && yRotation <= 360)
{
Instantiate(bombPrefab, new Vector3(transform.position.x, 0, transform.position.z - 1), Quaternion.identity);
}

}


We can use the transform.forward property, and reverse it to put the object behind the player.

void PlaceBomb(){
float distance = 1;
var position = transform.position - (transform.forward * distance);
Instantiate(bombPrefab, position, Quaternion.identity);
}


Or we can use transform.TransformPoint() to convert a point that is directly behind us in local space into the equivalent point in world space:

void PlaceBomb(){
float distance = 1;
var position = transform.TransformPoint(new Vector3(0, 0, -distance));
Instantiate(bombPrefab, position, Quaternion.identity);
}