- Home /
Creating random objects
So I'm doing a first person thing where you toss trash around the woods and eventually get attacked by a bear...anyway, i want to randomly create trash. I've modeled the different pieces of trash (5 for now, i might add more).
I have the code to create objects on a mouse click attached to the first person controller...
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate(newObject, transform.position, transform.rotation);
}
what do i need to add to make it create random objects?
Answer by Eric5h5 · Apr 10, 2012 at 08:47 PM
var trash : GameObject[];
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate (trash[Random.Range(0, trash.Length)], transform.position, transform.rotation);
}
}
do you know what i could do to throw the trash? I've tried looking it up and can't find anything that i can just add to this script. any thoughts?
look up AddForce. Your pieces of trash will need a rigidbody.
Answer by gregzo · Apr 10, 2012 at 08:49 PM
Hi PileOfDeadNinjas!
Is this what you are looking for :
var variousTrashPrefabs : GameObject[]; //assign all kinds of trashy objects here via the inspector
function RandomTrash()
{
var randomIndex : int = Random.Range(0,variousTrashPrefabs.length);
Instantiate(variousTrashPrefabs[randomIndex], transform.position, transform.rotation);
}
???
Your answer
Follow this Question
Related Questions
random object creation 1 Answer
Generating objects randomly, only one will Instantiate 0 Answers
Instantiate Random Object at Random Position 1 Answer
Beginner : Random gameobject from array 2 Answers
Objects move on play 3 Answers