How would i go along making a script to add ammo to a gun? and once it runs out i can’t shoot anymore. But if i shoot any number of bullets i can press R for a reload animation and the ammo would be filled again. Thanks!
Very rough pseudo-code. Probably some edge cases to catch and it won’t work in your editor at all, but gives you an idea if you can read it of what you need to do.
maxAmmo:int;
currAmmo:int;
clipSize:int;
currClipSize:int;
outofAmmo:boolean;
update(){
if(currAmmo<=0){ NoAmmoEffects() }
else if(outofAmmo || inputkey=='R') reloadGun();
else if(inputButton=='fire'){ fire(); }
}
reloadGun(){
if(currAmmo>=clipSize){
currAmmo-=clipSize-currClipAmmo;
currClipAmmo=clipSize;
}
else {currClipAmmo=currAmmo; currAmmo=0;}
}
I got a lot of errors with that code O.o
If you would read, you would have read where I said it won’t work and it’s rough pseudo-code. It’s there to give you an idea of how you need to code something. It’s not there to plug and play script for you.
Oh, i see. Im a noob atm.
Alright i fixed it up a bit but im getting an error:
Unkown identifier: else.
any idea on how to fix that?
Please post your code so we can help you.
var maxAmmo:int;
var currAmmo:int;
var clipSize:int;
var currClipSize:int;
var outofAmmo:boolean;
update();
if(currAmmo<=0){ NoAmmoEffects();
{
else if(outofAmmo || inputkey=='R') reloadGun();
else if(inputButton=='fire'){ fire(); }
}
reloadGun(){
if(currAmmo>=clipSize){
currAmmo-=clipSize-currClipAmmo;
currClipAmmo=clipSize;
}
else {currClipAmmo=currAmmo; currAmmo=0;}
}
update(); → is a function so the syntax would be:
function Update ()
{
// your code goes here
}
You also need to respect the command syntax.
reloadGun → is also a function.
ok, i will try that
Unexpected token: else
An if-else block goes like this:
if (condition){
//do something
} else{
//do something else
}
Make sure to place all the needed brackets. However, if you don’t know how to program, don’t even waste your time with trial and error. Go learn some basic programing, there are decent tutorials on the internet. For starters try these:
http://www.unity3dstudent.com/
If you want to achieve anything at all you must know how to script. You can’t just rush in, write something and expect things to work. This is an exact science.
Thank you for your advice. I know basic JavaScript atm and am still learning