Hi,
I hope that everyone is well!
As the title suggests, I am just curious to see if anyone has any boilerplate code to procedurally launch an object in random directions while keep performance in mind.
Cheers!
Hi,
I hope that everyone is well!
As the title suggests, I am just curious to see if anyone has any boilerplate code to procedurally launch an object in random directions while keep performance in mind.
Cheers!
How about this? Raycasting seems… overkill.
int x = /* player spawn x grid coordinate */
int y = /* player spawn y grid coordinate */
List<Vector2> possibleDirections = new List<Vector2>();
// Can we go left?
if (x > 0) {
possibleDirections.Add(new Vector2(-1, 0));
}
// Can we go up and left?
if (x > 0 && y > 0) {
possibleDirections.Add(new Vector2(-1, -1));
}
// Can we go up?
if (y > 0) {
possibleDirections.Add(new Vector2(0, -1));
}
// Can we go up and right?
if (x < GridLength - 1 && y > 0) {
possibleDirections.Add(new Vector2(1, -1));
}
// Can we go right?
if (x < GridLength - 1) {
possibleDirections.Add(new Vector2(1, 0));
}
// FILL IN THE REST OF THE DIRECTIONS...
Vector2 directionToLaunchPlayer = possibleDirections[UnityEngine.Random.Range(0, possibleDirections.Count)];