# (2D) Instantiating in front with offset

Hey

In front of my moving player GameObject (rigidbody2D) another GameObject shall be instantiated. But it should not collide with my player. This always means ±0.7 to ±2 units next to it.
Here is my approach:

``````Vector3 spawnPos = (player.transform.position + player.transform.forward) * spawnDistance;
Vector3 playerWay = spawnPos;
float distance = Vector2.Distance(spawnPos, playerWay);
while ((distance < 0.7f) && (distance ) > 2f))
{
spawnPos.x = Random.Range(spawnPos.x - 2f, spawnPos.x + 2f);
spawnPos.y = Random.Range(spawnPos.y - 2f, spawnPos.y + 2f);
distance = Vector2.Distance(spawnPos, playerWay);
}
Instantiate(enemy, spawnPos, Quaternion.identity, transform);
``````

I think my approach with x and y is not correct. It may only move the spawn point to the left and right.

float minDistance = 0.7f;
float maxDistance = 2f;
float distance = Random.Range( minDistance, maxDistance );
if( Random.value < 0.5f ) distance = -distance;
Vector3 spawnPos = (player.transform.position + player.transform.up) * spawnDistance + player.transform.right * distance;
Instantiate(enemy, spawnPos, Quaternion.identity, transform);

@Hellium
That’s not what I’m looking for.
Here is a sketched example:
[137822-spawnlogic.png|137822]