Build a 2D Top Down Game – Zero to Published – Part 5

Welcome to Part 5!  Modular Scripts and Scripting!  I’m sure many of you as we have been going through this series have said “what the heck, why don’t I just plug all of this stuff into a single class?”  This will be demonstrated very shortly.  This session will be partly hands on, but also fairly heavy in discussion.

Again if you haven’t read the previous parts, please go read those.

Part 1:  Getting Started

Part 2: Animations

Part 3: Prefabs

Part 4: Enemies

Part 5: Modular Scripting

Part 6: Timed Prefab Instantiation

Part 7: Health Bars

Part 8: Finishing Touches

Part 9: Publishing

Man, I really like the skeleton better, I want to use the skeleton as my main character and the spider as my AI enemies.

What we do in the project

Modify the Spider

Remove the CharacterInputscript

Add the AIScript

Uncheck fixed angle from the RigidBody2D checkbox

Modify the Skeleton Script

Remove the AIScript

Add the CharacterInputscript

Check Fixed Angle

Modify the camera

Drag the skeleton onto the camerafollowscript

Save your prefabs

Drag your skeleton and spider onto their respective prefabs.



So what essentially we have here are two input scripts operating on our generic Character Script.  AI is simply making some calculations to generate a heading and calling move in that direction, while CharacterInput is simply taking your input from your brain’s calculations, which generates a heading and then calling move in that direction.  By coding in a modular way we can easily use these same scripts on other game objects and easily swap out various components in an additive manner.


Why didn’t we just do inheritance?

Good Point!  I’m glad you asked.  In this scenario Inheritance would have made more sense, that is very true.  One reason we would do this is to prevent those using the class that relies on this class from overriding properties or methods of our class.  Essentially what we are doing is sealing the class and only allowing the exposition of our provided api.  This is particularly useful on large teams when you don’t want individuals who don’t know what you are doing in your update function to override it or do silly things.  Alternatively you can use the sealed features from c#, however I am not sure how that translates down to other platforms since at the end of the day, your c# code is not actually being compiled down to IL.  So I do this as a bit of a safety net.  The only performance hit you receive is a little bit of extra memory for a script component and a little bit of extra load time for finding and caching that component.

7 thoughts on “Build a 2D Top Down Game – Zero to Published – Part 5

  1. Pingback: Ross

  2. If you are thinking to create a new house, and then make sure that
    you are using this technology. Importantly,
    there are design programs that are specifically designed for children and students.
    (Emails kept private and so are used and then we
    can notify the winners).

  3. Pingback: Build a 2D Top Down Game – Zero to Published – Part 8 | DaCrook

  4. Pingback: Build a 2D Top Down Game – Zero to Published – Part 3 | DaCrook

  5. Pingback: Build a 2D Top Down Game – Zero to Published – Part 7 | DaCrook

Leave a Reply

Your email address will not be published. Required fields are marked *