x


Game Nearly Complete need some advice

Hi there and thanks for looking the question I have is this. I have a game where you can unlock 11 weapons I was wandering if there is an easier way to do it compared to the way I have also with a mouse scroll weapon selection. The way I have it I am sure is not optimised and i'm sure it wont work the more weapons I add all i want is some advice on the best way to do this whether that be separate weapon scripts or what. Here is the code not finished but want advice before i carry on

var electroDrill : boolean = true;

var rayGun1 : boolean = false;

var rayGun2 : boolean = false;

var rayGun3 : boolean = false;

var laser : boolean = false;

var laser2: boolean = false;

var laser3 : boolean = false;

var bazooka : boolean = false;

var freezeGun : boolean = false;

var frogGun : boolean = false;

var emp : boolean = false;



var electroDrillModel;

var rayGun1Model;

var rayGun2Model;

var laser1Model;

var laser2Model;

var laser3Model;

var bazookaModel;

var freezeGunModel;

var frogGunModel;

var empModel;



// Weapon variables



// ElectroDrill

var eLECTRODRILLINSP : String;

var shootForce : float;

var bullet : GameObject;

var bulletHolder : Transform;

var ammo : int;

var canShootElectroDrill : boolean;

// RayGun

var rayGunForce : float;

var rayGunBullet : GameObject;

var rayGunBulletHolder : Transform;

var rayGunAmmo : int;

var canShootRayGun : boolean = true;







function Start()

{

    electroDrillModel = gameObject.Find("ElectroDrill").renderer.enabled = true;

    rayGun1Model = gameObject.Find("Raygun_lvl1").renderer.enabled = false;

}











function Update()

{

    if(electroDrill == true)

       {

         ElectroDrill();

       }

    if(rayGun1 == true)

       {

         RayGun1();

       }



}



function ElectroDrill()

{

    if(electroDrill == true && PauseGame.paused == false)

    {

       if(Input.GetButton("Fire1") && ammo > 0 && canShootElectroDrill == true)

          {

              canShootElectroDrill = false; // can we shoot

              animation.CrossFade("Gun");

              //AudioSource.PlayClipAtPoint(bulletSoundGun, transform.position);//AUDIO

              var shoot = Instantiate(bullet, bulletHolder.position, bulletHolder.localRotation); // instantiate the bullet at the bulletHolderGunCam's position and rotation

              shoot.rigidbody.AddForce(transform.forward * shootForce);//add force to the bullet prefab // BULLET \\

              ammo -= 1; // take 1 away from our ammo

              yield WaitForSeconds(0.2); // wait 1 seconds so that the animations can get reset 

              canShootElectroDrill = true; // we can now shoot again;

          }

    }

}





function RayGun1()

{

    if(rayGun1 == true && PauseGame.paused == false)



       {

         rayGun1Model.renderer.enabled = true;

         if(Input.GetButton("Fire1") && rayGunAmmo > 0 && canShootRayGun == true)

          {

              canShootRayGun = false;

              animation.CrossFade("Gun");

              var shootRay = Instantiate(rayGunBullet, rayGunBulletHolder.position, rayGunBulletHolder.localRotation);

              shootRay.rigidbody.AddForce(transform.forward * rayGunForce);

              rayGunAmmo -= 1;

              yield WaitForSeconds(0.2);

              canShootRayGun = true;

          }

       }

}   

















// Reminder Create the scripts for the rest of the weapons here
function LateUpdate()

{

    if(rayGunAmmo <= 0)

       {

         rayGunAmmo = 0;

       }



    if(ammo <= 0)

       {

         ammo = 0;

       }



    if(electroDrill == true)

       {

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }

    if(rayGun1 == true)

       {

         electroDrill = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }



       if(rayGun2 == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }

    if(rayGun3 == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }



       if(laser == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }

    if(laser2 == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }



       if(laser3 == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }

    if(bazooka == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         freezeGun = false;

         frogGun = false;

         emp = false;

       }



       if(freezeGun == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         frogGun = false;

         emp = false;

       }

    if(frogGun == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         emp = false;

       }



       if(emp == true)

       {

         electroDrill = false;

         rayGun1 = false;

         rayGun2 = false;

         rayGun3 = false;

         laser = false;

         laser2 = false;

         laser3 = false;

         bazooka = false;

         freezeGun = false;

         frogGun = false;
       }
}     
more ▼

asked Oct 24 '11 at 07:40 PM

john essy gravatar image

john essy
174 24 41 48

Well, when you realise it's basically all reused code with subtle differences every time, it becomes nowhere near as big! In any case, as the OP says, it could be coded in a much more efficient way. I can see two good ones already, just below!

Oct 24 '11 at 10:58 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I recommend learning about classes and arrays.

Your project is screaming for you to define a class to represent a gun.

Here is an example of what that might look like:

class Gun
{
    var name          :  String;
    var isSelected    :  boolean;
    var canUserShoot  :  boolean;
    var shootForce    :  float;
    var ammo          :  int;
    var bullet        :  GameObject;
    var bulletHolder  :  Transform;
}

If you define a class named Gun like that, you can then put the following at the top of your script:

var gunList : Gun [];

This will declare an array of guns that you can maniplulate in your script. You can edit the guns in the array in the Inspector as well.

Learning about arrays is a must. They are one of the most important, useful, and fundamental concepts of programming.

Once you've defined your gun class and put some data in it, you can process the array in your script like this:


for ( var thisGun : Gun in gunList ) {
    print(thisGun.name + " has this power: " + thisGun.shootForce);
}

more ▼

answered Oct 24 '11 at 08:57 PM

jahroy gravatar image

jahroy
3.2k 14 18 41

Unless you can hold more than one gun at once, strip isSelected in favour of holding the index of, or a reference to, the selected gun in the managing class to avoid searching for it every time.

Possible next step: define a base class for the guns, move the gun logic into methods of subclasses. E.g. the lasers and rayguns might be very similar and only differ a bit regarding the values, while the bazooka will have other behaviour.

Oct 24 '11 at 11:48 PM gfr

0 Cool guys thanks i did not know you could create classes in unityscript thanks for this if you have any links describing the best use of this could you share and again thanks guys

Oct 25 '11 at 08:22 AM john essy
(comments are locked)
10|3000 characters needed characters left

You have a crazy amount of variables that are similar. Like RayGun1, RayGun2, RayGun3. You could make a class or just a boolean list (sorry not familiar with javascript), so if you ever wanted to create RayGun4 it would be easy. And also instead of setting each one to false, you could just loop through each RayGun in your RayGun list.

In C# it would be like so

List<bool> RayGuns = new List<bool>();
RayGuns.Add("RayGun1")
RayGuns.Add("RayGun2")

and when you wanted to set them all to false you could just write

foreach(bool b in RayGuns)
{
b = false;
}

but you could take this one step further if you created a Gun Class, and then create a class that holds all the Guns, so you would have all your guns (RayGuns, Lasers) in one list, allowing easy setting to true or false.

more ▼

answered Oct 24 '11 at 09:02 PM

sp00ks gravatar image

sp00ks
132 4 5 14

Ok so i have created the gun class and the thing is this is very noobish but how would i decide which gun is selected and how would i select the gun. The reason i am mixed up is well how does the gun class know which gun is which?

Oct 25 '11 at 10:14 AM john essy

The gun class is a template, which you can use to create a whole bunch of guns!

Gun raygun1;
Gun raygun2;
Gun pistol;
Gun BFG;

All of these share the same list of paramaters, name, ammo, damage, cost, whatever- you can then use them however you like. I'd have something like

Gun currentGun = BFG;

and then assign to currentGun whenever you change weapons.

Then, when you would shoot or something, use

damage = currentGun.damage;

to get the current weapon's stats.

Oct 25 '11 at 10:22 AM syclamoth

Thanks will try this now :)

Oct 25 '11 at 11:41 AM john essy

I understand how you mean but i just cant seem to utilize it very well. I am trying to create a couratine so that there is a delay between shots but it just won't work. I am also creating functions for each of the guns but im not sure this is what you all mean. here is the code also how i would decide which is selected. I am new to creating classes and would like to thankyou all for helping lol

I also get this error NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) WeaponManager+$RayGun$87+$.MoveNext () (at Assets/WeaponManager.js:38)

var gunList : Gun[];

class Gun

{

var name : String;

var isSelected :  boolean;

var canUserShoot :  boolean;

var shootForce :  float;

var ammo : int;

var bullet :  GameObject;

var bulletHolder : Transform;

var shootSound : AudioClip;

var cost : int;

var unlocked : boolean;

}

function Update()

{

if(Input.GetButton("Fire1"))

    {

       RayGun();

    }

}

function RayGun()

{

for(var rayGun : Gun in gunList)

    {

       Debug.Log("We are shooting");

       rayGun.canUserShoot = false; // can we shoot

       animation.CrossFade("Gun");

       //AudioSource.PlayClipAtPoint(bulletSoundGun, transform.position);//AUDIO

       var shoot = Instantiate(rayGun.bullet, rayGun.bulletHolder.position, rayGun.bulletHolder.localRotation); // instantiate the bullet at the bulletHolderGunCam's position and rotation

       shoot.rigidbody.AddForce(transform.forward * rayGun.shootForce);//add force to the bullet prefab // BULLET \\

       rayGun.ammo -= 1; // take 1 away from our ammo

       yield WaitForSeconds(0.2);

       rayGun.canUserShoot = true; // we can now shoot again;

    }

}

Oct 25 '11 at 12:10 PM john essy

You set the variable named canUserShoot to false, but you never do anything with it.

Try adding something like this to the top of your function:

if ( someGun.userCanShoot == false ) {
    return;
}

This checks the value of userCanShoot and exits the function if it's not true.

... although now that I read your code closer, it appears you're looping through every gun in the array. I don't think that's what you want to do.

You should read the link above about arrays and check out some examples so you understand what arrays are and how they work.

Oct 25 '11 at 04:42 PM jahroy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x29

asked: Oct 24 '11 at 07:40 PM

Seen: 662 times

Last Updated: Oct 25 '11 at 04:46 PM