- Home /
ramdomised weapon pickup
hey guys, I've got this weapon pickup script set to a pickup object that gives the player a weapon when picked up, so far the pickup only gives one fixed weapon per single pickup and I must create a new pickup for each weapon, so what I want to do is make a mario kart style pickup where it will give a random weapon when picked up instead of each weapon needing it's own pickup. How can I do this ? here's the script
var MachineGun : GameObject;
var RocketLauncher : GameObject;
var Unarmed : GameObject;
var UsingNothing : boolean = true;
var Using_MG : boolean = false;
var Using_RL : boolean = false;
function Start(){
UsingNothing = true;
Using_MG = false;
Using_RL = false;
}
function OnTriggerEnter (other:Collider){
if (other.GetComponent.<Collider>().gameObject.name == "MachineGun Pickup"){
UsingNothing = false;
Using_MG = true;
Using_RL = false;
}
if (other.GetComponent.<Collider>().gameObject.name == "RocketLauncher Pickup"){
UsingNothing = false;
Using_MG = false;
Using_RL = true;
}
}
function Update(){
if (UsingNothing){
Unarmed.gameObject.active = true;
MachineGun.gameObject.active = false;
RocketLauncher.gameObject.active = false;
}
if (Using_MG){
Unarmed.gameObject.active = false;
MachineGun.gameObject.active = true;
RocketLauncher.gameObject.active = false;
}
if (Using_RL){
Unarmed.gameObject.active = false;
MachineGun.gameObject.active = false;
RocketLauncher.gameObject.active = true;
}
}
Answer by CodeMonkeyYT · Nov 09, 2019 at 08:27 AM
You can use Random.Range(min, max); to get a random value https://docs.unity3d.com/ScriptReference/Random.Range.html
So you could do this:
private void SelectRandomWeapon() {
int weaponIndex = Random.Range(0, 3);
switch (weaponIndex) {
default:
case 0:
UsingNothing = true;
break;
case 1:
Using_MG = true;
break;
case 2:
Using_RL = true;
break;
}
}
Not sure why you are using the booleans though, why aren't you activating the weapon game object on the collision?
Your answer
![](https://koobas.hobune.stream/wayback/20220612214547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
weapon pickup and equiping it to FPS player 2 Answers
Gun Random Rotation 0 Answers
picking up weapons script? 3 Answers
FindChild not working? (Pick up Object) 2 Answers