Last active
July 18, 2022 19:17
-
-
Save aprilspeight/8c974a07623d740d7ec44013819e7672 to your computer and use it in GitHub Desktop.
Spawns a prefab at a random position.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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