Position an instantiated prefab relative to its translation vector

First time using Unity Answers but so far it has been really useful. I am currently doing an Asteroids Clone for Unity3D as my first learning project and so far it’s going great. I had originally made Asteroids with Flash and now I am just figuring out how to do things the Unity way. Let me just say, Unity is awesome! What would take me 20-30 lines of code in Flash takes me 4 lines of code in Unity. Simply brilliant.

In my Flash version though, I came up with a way to have my asteroids spawn off of the screen and depending on the direction they were heading towards, they would spawn either top, bottom, left, or right. The reason I was doing this was so because I wanted the Asteroids to Destroy themselves once they left the screen again. You can imagine that if I just spawn them randomly without taking into account the direction they are heading to, at least 50% of them will be spawned and just Destroy immediately. Not efficient at all.

The way I achieved this in Flash was fairly simple, here’s some pseudo code;

if (Asteroid.direction.x > 0 && Asteroid.direction.y > 0)
spawnLeftSide(); //Because this is heading towards the top right
if (Asteroid.direction.x < 0 && Asteroid.direction.y < 0)
spawnRightSide(); //This one is going the opposite direction

So is there any fancy way of doing this in unity?

Another alternative is I could do it the other way around and first establish where they spawn, then give them the Vector. I can Clamp the values so as to make sure that they have a Vector that will direct them to the main view, instead of towards the out of bounds area.

While I currently have two ways of doing this, I would love to hear of any Unity specific ways to achieve this or if there are any more sophisticated algorithms out there, that would be really cool.

Thanks a bunch!

If I was doing something like this, I would probably create random position and direction, then mirror the wrong directions - something like this pseudo-code:

if (position.x too close to the left side && direction.x < 0) {
  direction.x = -direction.x
}

and only after x and y directions were right I would instantiate the asteroid. This is much more a matter of logic than an engine-specific question - but you’re right: Unity is awesome! Flash is good for some simple applications, but a real game needs a real game engine - and Unity does the job!

Well, if anyone is interested, this is what I came up with;

//Spawn Position
	float spawnPadding = 2;
	float spawnRangeX = Random.Range(boundChk.minWrap.x, boundChk.maxWrap.x);
	float spawnRangeZ = Random.Range(boundChk.minWrap.z, boundChk.maxWrap.z);
	Vector3 quadrantN = new Vector3(spawnRangeX, 1, boundChk.maxWrap.z + spawnPadding);
	Vector3 quadrantE = new Vector3(boundChk.maxWrap.x + spawnPadding, 1, spawnRangeZ);
	Vector3 quadrantS = quadrantN * -1;
	Vector3 quadrantW = quadrantE * -1;
	float asteroidAngle = Mathf.Atan2(randomDirection.x, randomDirection.z) * Mathf.Rad2Deg;
	
	if (asteroidAngle > 45 && asteroidAngle < 135) {
		transform.position = quadrantW;
		print(asteroidAngle + " to the West");
	} else if (asteroidAngle >= 135 || asteroidAngle < -135) {
		transform.position = quadrantN;
		print(asteroidAngle + " to the North");
	} else if (asteroidAngle >= -135 && asteroidAngle <= -45) {
		transform.position = quadrantE;
		print(asteroidAngle + " to the East");
	} else if (asteroidAngle > -45 && asteroidAngle <= 45) {
		transform.position = quadrantS;
		print(asteroidAngle + " to the South");
	}

I spent a lot of time because I tried calculating the angle between vectors, should’ve done it like I did in Flash and used Atan2. Would’ve saved myself a good 45 minutes.

Code is pretty straight forward; Set up quadrants which act as spawn points. They are all outside the visible map. I use a BoundaryCheck class that allows me to encapsulate all of the boundary checking. minWrap and maxWrap just return the edges of the screen using ScreenToWorldPoint. I then calculate the angle the Asteroid is heading to and depending on the angle, I use the if statements to spawn it at a quadrant where it makes sense given the trajectory. So if it’s heading at a 3 o’clock direction, it spawns on the West Quadrant, allowing it a greater chance of colliding with the ship.

Hope this helps anyone.