Created
July 18, 2022 21:26
-
-
Save aprilspeight/91a82cf4da88050d72bc5fb84cec69db to your computer and use it in GitHub Desktop.
Instantiate a prefab at a random position on key press.
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