Zitao He

Row Your Kart

A 3D kart racing game built by Unity game engine and C#. Github Repo

Image
Image

Unity has some default methods for MonoBehaviour base class (most classes many scripts derive from). For example the void Start() method is called whenever a object is instantiated from a class. void Update() is the method that is called every time the game frame gets updated. void OnTriggerEnte() executtes whenever the object collide (physics is handled by Unity engine) with another object.

An example is the class that is used to count the laps the player has finished:

public class LapsCounter : MonoBehaviour
{
    private int elapsedLaps = 0;
    private Boolean isColliding = false;
    public bool backwardWallActivated = true;
    //initialize something before frame is rendered
    void Start()
    {
      //some codes here
    }
    // Update is called once per frame
    void Update()
    {
        isColliding = false;
    }
    // when the object collides with another object, trigger the script
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Finish")
        {
            if (isColliding) return;
            isColliding = true;
            elapsedLaps++;
        }
        else if (other.tag == "FinishCheck")
        {
            backwardWallActivated = false;
        }
    }
    // any other utility methods
    public void resetLaps()
    {
        elapsedLaps = 0;
    }
    // more methods...
}