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.
↧