inappropriateCode
inappropriateCode

Reputation: 211

Implementing weapons dependency (unit/weapon/projectile/warhead) [Unity3D C#]

In my unity game units can pick up a new weapon. There are four relevant classes; Unit, Projectile (both of which are monobehaviour classes on a gameobject), Weapon, Warhead (both should not exist as gameobjects in 3d space).

In addition to various attributes (damage, range, etc) a weapon instance must store a warhead and projectile as properties; the former being a class instance, and the latter being a prefab. The weapon class has a shooting method which instantiates a projectile, aims it, applies the warhead, and reloads.

I have encountered problems with this, as classes which do not inherit monobehaviour, or are scriptableobjects, cannot assign a prefab in the editor window (or to my knowledge via code). And scriptableobjects cannot be assigned in the editor either. I can't assign a projectile to a weapon, or a weapon to a unit, by inspector or code. And stringing scriptableobject and monobehaviour classes together like this is breaking my code.

I had this working previously by assigning prefab references, along with the shoot method, to the unit class. Presently there is a base warhead and base weapon class, from which children are derived, which is how I'd like it to remain. But I'm not happy with how I had to put all of the weapon methods and properties into the unit class, because I wish to put the shooting functions in the weapon (that makes sense?) and cleanly separate units from weapons.

What is the best way to go about implementing this system?

Upvotes: 0

Views: 539

Answers (1)

DisturbedNeo
DisturbedNeo

Reputation: 743

So, you want Weapon to Inherit from BaseWeapon, but if you do that you cannot then directly inherit from MonoBehaviour, which you need in order to set up the Prefab references.

The way I see it, the best way to approach this is probably to implement BaseWeapon as an Interface that each weapon then implements, whilst also inheriting from MonoBehaviour.

Upvotes: 1

Related Questions