- Home /
Player Weapon Scripting
I want to have the player start off with the standard 2 weapon deal. Well thats done. But also find other weapons onn the way. How would i go about this when I go to edit the PlayerWeapons script from the tutorial. this is what I got so far it doesnt work though. If thier is a better script please show me what it is, ive been botherd for 5 days now about it.
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
else if (Input.GetKeyDown("3")) {
if (gameObject("AssultRifle").SetActiveRecursively(true));{
SelectWeapon(2);
}
else if (gameObject("AssultRifle").SetActiveRecursively(false));{
SelectWeapon(null);
}
}
D: Could you please make the code a little more readable?
I dont know what you want me to do with it thats how most of it was made. selecting a weapon is just plane and simple to begin with.
He was talking about the code formatting on the site. You need to highlight the code and click the "Code Format" button if you're going to post code here. I fixed this post for you, but in the future don't forget this.
Answer by xToxicInferno · Jun 08, 2010 at 10:09 PM
Well i do not know if this is the 'best' way but you can do it like i did it.
var weapon: int = 0;
function Update () { if (Input.GetKeyDown("1") { weapon = 0; } if (Input.GetKeyDown("2") { weapon = 1; }
if (Input.GetButton ("Fire1")){
if(weapon == 0) {
//pistol stuff
}
if (weapon == 1) {
//assault rifle stuff
}
}
}
You can also have 'weapon' be static so when you pick up a new gun it changes it to 3 so you can do:
if (weapon == 3) {
//rocket launcher stuff
}
If you want to do a Metal Gear Solid thing where once you get a weapon you unlock it forever (i guess its similar in other games its just i like MGS haha) you can do:
var rocketlauncher : boolean = false;
if(rocketlauncher) { if(Input.GetKeyDown("3") { weapon = 3 } }
You can then also have rocketlauncher be static so when you get a rocketlauncher it changes it to true.
Well good luck!