- Home /
Mario Kart Style random weapon
I want to make a mario style game and i want to make it so that a game object will give me a random weapon when i drive over it and also the object would have to respawn after a few seconds. Please help
Answer by Flafla2 · May 31, 2011 at 01:12 AM
This is a situation where if you actually knew Unityscript you would know what the solution is... Let's go through your question by looking at Key words: I want to make a mario style game and i want to make it so that a game object will give me a random weapon when i drive over it and also the object would have to respawn after a few seconds. Please help
Alright, the first keyword is game object. So you need to have a gameobject in your scene, and call it a certian name.
Random:Duh... Random.Range
Drive Over It: Colliders. Rigidbodies. Yay.
Respawn: Instantiate the "Item box" gameobject.
A few Seconds: Time, *Time*, ***Time!***
I hope this answers your question.
Answer by save · May 31, 2011 at 01:05 AM
You could do this on OnTriggerEnter(), select a random object with Random.Range(minval, maxval) and make use of yields to wait for the respawn (could be the objects renderer switch on/off).
But could i use Arrays with it? and how would i go about that exactaly in script? sorry im a noob
Yeah you definitely could apply an array to it, just use var weaponOfRandomChoice : GameObject = weapons[Random.Range(0, weapons.length)]. The array itself could store the actual GameObjects or otherwise integers, it all depends on how you want to wrap things up to finally give the randomized weapon to the player.
Answer by eeveelution8 · May 10, 2014 at 07:10 PM
I don't know how to do it either, but here's a good starting script to go with.
var items : GameObject [];
function OnTriggerEnter()
{
collider.enabled = false;
var index : int = Random.Range(1,items.length);
var thePrefab : GameObject = items[index];
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
Your answer
Follow this Question
Related Questions
Random.Range statement question 1 Answer
Sound playing at random. (JS) 2 Answers
Having random operators in an equation? 1 Answer
Adding GUI to this 1 Answer
Spawn random amount of gameobjects 2 Answers