|
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
(comments are locked)
|
|
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:
If you define a class named Gun like that, you can then put the following at the top of your script:
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: Unless you can hold more than one gun at once, strip 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)
|
|
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 and when you wanted to set them all to false you could just write 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. 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! 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 and then assign to currentGun whenever you change weapons. Then, when you would shoot or something, use 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 { } function Update() { } function RayGun() { }
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: 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)
|

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!