Skip to content

Instantly share code, notes, and snippets.

@aprilspeight
Last active July 18, 2022 19:17
Show Gist options
  • Save aprilspeight/8c974a07623d740d7ec44013819e7672 to your computer and use it in GitHub Desktop.
Save aprilspeight/8c974a07623d740d7ec44013819e7672 to your computer and use it in GitHub Desktop.
Spawns a prefab at a random position.
// Assign the prefab in the editor
public GameObject prefab;
void Update()
{
// Define range for random values
int spawnPointX = Random.Range(-2, 2);
int spawnPointY = Random.Range(-2, 2);
int spawnPointZ = Random.Range(-2, 2);
Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, spawnPointZ);
// Spawns a prefab at a random position when a key is pressed
if (Input.GetKeyDown("space"))
{
Instantiate(prefab, spawnPosition, Quaternion.identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment