Building a multi layers game using Unity and C#

Building a 2D game where the cube goes around the farm field to collect pieces of gold. It’s COVID time, every one needs to earn a bit of extra income, including once-upon-a-time in-animated objects.

The project goal was to get familiarized with the Unity and Visual Studio Code environments and begin to build a game scene with multiple layers and inter-activities.

The game object [box] should be able to:

  • Obey the rules of gravity and physics

  • Move with left, right, top, down by ADWS and by keyboard arrows

  • Jump with space bar

  • Stalked by the main camera object

Assets:

  • PBR Brack Material (free)

  • ADG_Textures (free)

  • Prototyping_Pack_Free

Settings: Getting the cube to obey the rules of gravity and physics

Code and assign a rigidbody to the cube (see code below) and makes sure Mesh Collider is enabled for planes and terrain

In progress #1

In progress #1

Grass material

Grass material

In progress #2

In progress #2

In progress #3

In progress #3

Gif Demo

Gif Demo

Code: Getting the cube to move with left, right, top, down by ADWS and by keyboard arrows + Jump with space bar

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cubeBehavior : MonoBehaviour
{   
    private Light myLight;
    private new Rigidbody rigidbody;

    // jump variables
    public float jumpHeight;

    // rotate variable
    public Vector3 rotateAmount;

    // Start is called before the first frame update
    void Start()
    {
        print( message: "Hi from start method");
        myLight = GetComponent<Light>();
        rigidbody = GetComponent<Rigidbody>();
    }
    // declare the light variable
    // grab the light variable
    // Update is called once per frame
    void Update()
    {
        // print( message: "Hi from update method");
        // script to turn light on / off the light variable

        if (Input.GetMouseButtonDown (0)) {
            print ("The Left mouse button was pressed");
            myLight.enabled = !myLight.enabled;
            // rigidbody.MovePosition(transform.position + transform.forward);
            rigidbody.transform.Rotate(new Vector3(0, -10, 0));
            // rotate the cube wieh clicks rigidbody.transform.Rotate(new Vector3(3, 4, 1));
        }

        if (Input.GetMouseButtonDown (1)) {
            print ("The Right mouse button was pressed");
            myLight.enabled = !myLight.enabled;
            // rigidbody.MovePosition(transform.position + transform.forward);
            rigidbody.transform.Rotate(new Vector3(0, 10, 0));
            // rotate the cube wieh clicks rigidbody.transform.Rotate(new Vector3(3, 4, 1));
        }
        // adding keyboard navigation
        if (Input.GetKey (KeyCode.W)) {     
            transform.Translate (0.0f, 0f, 0.01f);
            }
        if (Input.GetKey (KeyCode.S)) {
            transform.Translate (0.0f, 0f, -0.01f);
            }
        if (Input.GetKey (KeyCode.D)) {
            transform.Translate (0.01f, 0f, 0f);
            }
        if (Input.GetKey (KeyCode.A)) {
            transform.Translate (-0.01f, 0f, 0f);
            }
        // adding arrows navigation
        if (Input.GetKey("up")) {     
            transform.Translate (0.0f, 0f, 0.01f);
            }
        if (Input.GetKey("down")) {
            transform.Translate (0.0f, 0f, -0.01f);
            }
        if (Input.GetKey("right")) {
            transform.Translate (0.01f, 0f, 0f);
            }
        if (Input.GetKey("left")) {
            transform.Translate (-0.01f, 0f, 0f);
            }
        if (Input.GetKey("space")) {
            print ("the space button was pressed");
            rigidbody.AddForce(Vector3.up * jumpHeight);
        }
        
    }
}

Code: Getting the Main camera to stalk the cube

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameraController : MonoBehaviour
{   public float turnSpeed = 4.0f;

   //create a myCube variable
    public Transform myCube;
    // create offset camera
    public Vector3 cameraOffset;

    // Start is called before the first frame update
    void Start()
    {
        cameraOffset = new Vector3(myCube.position.x, myCube.position.y + 4.0f, myCube.position.z + 3.0f);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void LateUpdate()
    {
        cameraOffset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * cameraOffset;
        transform.position = myCube.position + cameraOffset;
        transform.LookAt(myCube.position);
    }
}

Learnings

I think that Unity and its integration with Visual Studio Code is a good smart relationship. What’s not as smooth is the constant drag and drop and assignment that needs to happen in Unity after the code is in VSC. How come Unity doesn’t allow for code editing in-app? Seems like a mis-opportunity to me.

What’s next:

  • Switching that cube into a farmer, because yes - I’m biased to the pre-conception that a farmer should be on a farm vs. a cube

  • Create a reward interactive whereas upon coming into contact, the gold would turn into dust with some magical animation that celebrates the user’s winning moment

  • Change the keyboard movement to reset based on current cube direction and not original orientation