- Home /
Reloading A Gun My Way
Hi Yes, I know there are several topics like this, but I think that I should be able to load a gun this way. I think its just that I don't know how to tell the computer to do it. Can you help?
var MPS : int = 35; //How fast the bullet goes
var BB : Rigidbody;
var automaticWeapon : boolean = true;
var semiAutoWeapon : boolean = false;
private var lastShot : float;
var shotInterval : float = .1;
var deleteBulletTime : int = 3;
var magSize : int = 15;//Clip Size
function Update () {
if(semiAutoWeapon == true && automaticWeapon == true){
Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
}
if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
if(Input.GetButtonDown('Fire1')){
if(magSize > 0){//If magSize isn't empty, Fire Away!
var instantiatedBB : Rigidbody = Instantiate(BB,
transform.position, transform.rotation );
instantiatedBB.velocity = transform.TransformDirection(
Vector3( 0, 0, MPS ));
var magSize = magSize - 1; //What i want is that every
time you shoot, the var magSize lessens by one.
}
else{ //If magSize is empty (not greater then 0), reload!
Reload();
}
}
}
if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
if(Input.GetMouseButton(0)){
if(Time.time - lastShot >= shotInterval){
var instantiatedTwoBB : Rigidbody = Instantiate(BB,
transform.position, transform.rotation);
instantiatedTwoBB.velocity =
transform.TransformDirection(Vector3( 0, 0, 50 ));
lastShot = Time.time;
}
}
}
}
function Reload (){ //I'm just testing to see if I can get to the Reload stage...
Debug.Log('Oops, Out of Ammo!');
}
My main problem is subtracting from the varible. Thank You for all the help!
Could you please reformat your code? This is unreadable. Add a TAB
by adding four SPACE
s
It also seems you forgot to check if you have ammo when you're in 'automatic' mode, and the bullet velocity is not equal to $$anonymous$$PS for some reason.
Read about enum ... having two mutually exclusive booleans is odd.
An enum would be a start. It's worth considering that this kind of situation is exactly what polymorphism is for, as a way of making this code more flexible.
Answer by burnumd · Jun 24, 2011 at 04:21 PM
This problem stems from the fact that you're re-declaring your magSize
variable locally within update:
var magSize = magSize - 1;
should be
magSize = magSize - 1;
or even be fancy and use
magSize--;
You'd be well served doing some research on object-oriented design. Using booleans to determine the type of gun is liable to lead to all sorts of errors. You even attempt to catch a design-time error with the automatic/semi-automatic flags that could be abrogated by subclassing different types of guns (or even using an enum rather than boolean flags).
O$$anonymous$$... Can You tell me where I can get started with object oriented design? I kinda a total newbie at this.
PS - Thanks for the script help! That worked wonders!
In terms of free resources, Wikipedia is generally a decent jumping-off point for program$$anonymous$$g topics (when you've digested them, be sure to check out the source/external links). The OOD page is here: http://en.wikipedia.org/wiki/Object-oriented_design If you're interested in books, I'm personally fond of the Headfirst series: http://headfirstlabs.com/books/hfooad/ which are very beginner friendly.
Answer by Byterunner · Jun 24, 2011 at 04:29 PM
Your problem is with scope. You're redeclaring the variable magSize as a new variable locally within your if block and thus not affecting the global variable. Here's a good start to understanding scope: http://javascript.about.com/library/bltut07.htm
Also make sure that your automaticWeapon and semiAutomaticWeapon booleans are set properly - as it's written currently you'll only get the reload message when your weapon is set to semiAutoWeapon.
Try this:
var MPS : int = 35; //How fast the bullet goes
var BB : Rigidbody;
var automaticWeapon : boolean = true;
var semiAutoWeapon : boolean = false;
private var lastShot : float;
var shotInterval : float = .1;
var deleteBulletTime : int = 3;
var magSize : int = 15;//Clip Size
function Update () {
if(semiAutoWeapon == true && automaticWeapon == true){
Debug.Log('Houston, We have a Problem!');//My gun can't be BOTH Semi and Auto
}
if(semiAutoWeapon == true && automaticWeapon == false){//Semi-Auto Settings
if(Input.GetButtonDown('Fire1')){
if(magSize > 0){//If magSize isn't empty, Fire Away!
var instantiatedBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation );
instantiatedBB.velocity = transform.TransformDirection(Vector3( 0, 0, MPS ));
magSize--; //What i want is that every time you shoot, the var magSize lessens by one.
}
else{ //If magSize is empty (not greater then 0), reload!
Reload();
}
}
}
if(automaticWeapon == true && semiAutoWeapon == false){//Auto Settings
if(Input.GetMouseButton(0)){
if(Time.time - lastShot >= shotInterval){
var instantiatedTwoBB : Rigidbody = Instantiate(BB, transform.position, transform.rotation);
instantiatedTwoBB.velocity = transform.TransformDirection(Vector3( 0, 0, 50 ));
lastShot = Time.time;
magSize--; //What i want is that every time you shoot, the var magSize lessens by one.
}
}
}
}
function Reload (){ //I'm just testing to see if I can get to the Reload stage...
Debug.Log('Oops, Out of Ammo!');
}
Your answer
Follow this Question
Related Questions
Reload after certain amount of shots? 1 Answer
Gun - Ammo , reloading and UI problem. 1 Answer
Pass a JS Script to C#? 1 Answer
realistic reload system? 1 Answer
Gun reloading script/instructions? 1 Answer