Thursday 10 December 2015

How I implemented my Variables and Datatypes in my Unity game

Today's blog post will cover examples of:

-Variables
-And Datatypes

In my Unity game.

Firstly: I've used and applied a variety of variables with different datatypes in my scripts (such as my Player and Camera scripts as shown below) as they serve different purposes and have different uses. These variable datatypes that I've used include: boolean, floats, integers, references to call upon other game objects and Vector 2 and 3 (as directional movement along the X, Y and Z axis). These all serve a different function that go hand-in-hand with my code as it references them throughout, and these variables serve as an important backbone that holds all of my code together. Here are a few examples of the variables I've used, with annotated code explaining their purpose.

My player variables:

"//Floats
    public float maxSpeed = 4; //Values that set the player's maximum movement speed, acceleration and jump power. Floats let you use numbers with decimal points.
    public float speed = 50f;
    public float jumpPower = 150f;

    //Booleans
    public bool grounded; //A true/false variable for whether or not the player is on the ground and can double jump.
    public bool canDoubleJump;

    //Integers for stats
    public int curHealth; //Values for the player's maximum and current health, so when you take damage the value drops! An int value as perfect for this as it's a whole number.
    public int maxHealth = 100;

    //References
    private Rigidbody2D rb2d; //Declaring the rigid body and animations as variables.
    private Animator anim;"


My camera variables:

  "private Vector2 velocity; //Declaring my variables

    public float smoothTimeX; //Smooth the horizontal and vertical movement
    public float smoothTimeY;

    public GameObject player; //Call upon the tagged player game object

    public bool bounds; //To limit the around the camera can travel to

    public Vector3 minCameraPos; //Minimum and maximum camera positions
    public Vector3 maxCameraPos; //Both of these values can be changed in the camera object's   settings"


My variables as shown above follow professional working practices by not capitalising the first letter for every word (as explained by (duck, 2010)), but they use capitalisation for following words in the variable's name in replacement to space, while being appropriately named so at a glance it's easy to tell what their function is. (So for example, my canDoubleJump variable is a boolean datatype that is literally designed to be true or false, as implied by the name, and if the conditions are right the variable will be set to true and the player will be able to double jump. So the name is fitting, can you double jump?) The reason I did this variable naming style is it's a good habit to get into now while I'm in education, so further on down the line when I get into the games industry I will easily adapt my skills and habits to continue working in a professional manner following industry-level technques.

The datatypes used in these variables are also appropriately used and the right tools for the job, which is another example of a professional working practise. For example my player health variables are using an integer, whereas my player movement options are floats. This is because integers only represent whole numbers (so 1, 2, 3, etc), whereas floats can use decimal points too. This means my player's movement options can be much more specific, especially when slowing down, as it can occur at a much more gradual pace as the float value increases and decreases, whereas if it was an integer the Bird would come to a sudden, jerky stop!

Here is also a picture of the final version of my Bird character, I am going for a pixel-art style which will match all of the assets used in the game (that will be sourced by me royaltyfree from OpenGameArt.org, which was created and animated in Photoshop and will be used in my Unity game prototype:

There were a few syntax errors with the variables upon testing, but there weren't any major logic issues as it was mostly just me making a spelling/ grammar error in my code that was quickly amendable. (Such as me misspelling the maxSpeed variable in the code, so they couldn't reference each other. Luckily Unity's excellent debugging tools quickly picked up on the issue and the exact line it was on and the problem was fixed in no time).

My research of variables beforehand was very useful for when I got round to implementing them, as it helped me gain a greater understanding of how they work and essentially store all the information you need, and my research for various datatypes helped greatly to decide what variable datatype is the best choice for all my different game mechanics, since I had already compiled a list in the form of a table, with a huge amount of them before I'd even started working on the Unity game. Which was a handy sheet of reference all in one place as I constantly look back to it if I find code online that uses examples with datatypes I may not be familiar with.

No comments:

Post a Comment