First dose of endorphin

Game Dev

So I’ve had my fill of knowledge from the great official tutorials from Unity Learn.

Long story short, I went back to the necessary basics, because of course I forgot a lot of things after a long break without Unity and programming at all.

Right at the beginning I had a bit of a dilemma which course or tutorial to start with, but after a while I got my bearings and chose Create with Code. It was the obvious choice for a beginner so I could bounce back later.

At first I expected it to be a bit too basic, but after a while I learned about e.g. the InvokeRepeating() function.

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

public class SpawnManager : MonoBehaviour
{
    public GameObject obstaclePrefab;
    private Vector3 spawnPos = new Vector3(25, 0, 0);

    private float startDelay = 2;
    private float repeatRate = 2;

    private PlayerController playerControllerScript;

    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void SpawnObstacle()
    {
        if(playerControllerScript.gameOver == false)
            Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation);
    }
}

During the course, they will also throw a bunch of assets at you that you can download for the prototypes. For example, I was pleasantly surprised by the fully animated 3D character from Synty Studios. Definitely the animatable character will help you bounce back if you’re terribly lazy about animating characters like me.

Another interesting feature was the personal project document template, where you fill in the key fields and then you can more or less plan the project sensibly and stick to the milestones you design yourself. They talk about the so-called MVP – Minimum Viable Product. It’s the kind of project that you don’t kill a lot of time with, but others can rate it somehow and give you enough feedback to see if the project makes sense or not. How many times have I gone into something like this and didn’t even put it out there for people to comment on.

I think this is the biggest weakness of all aspiring game developers, that they set a big goal that is unrealistic. This is the thing I need to focus on.

I was also surprised a lot by the way they scroll the infinite 2D background. If you’ve ever thought about an endless 2D platformer, this piece of code might come in handy.

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

public class RepeatBackground : MonoBehaviour
{
    private Vector3 startPos;
    public float repeatWidthX = 45;

    // Start is called before the first frame update
    void Start()
    {
        startPos = transform.position;
        repeatWidthX = GetComponent<BoxCollider>().size.x / 2;
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x < startPos.x - repeatWidthX)
        {
            transform.position = startPos;
        }

    }
}

I was also surprised a lot by the way they scroll the infinite 2D background. If you’ve ever thought about an endless 2D platformer, this piece of code might come in handy.

It is definitely worth mentioning the use of ForceMode.Impulse

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

public class PlayerController : MonoBehaviour
{

    private Rigidbody playerRb;
    public float jumpForce = 10f;
    public float gravityModifier;
    public bool isOnGround = true;
    public bool gameOver = false;

    

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        Physics.gravity *= gravityModifier;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }
    }
}

What ForceMode.Impulse applies the force instantly independently of time and, more importantly, it takes into account the Rigidbody’s Mass, so it applies a force relevant to the RigidBody’s weight. Such a use case is useful when calculating physics, e.g. when jumping or exploding.

And the last one, maybe common but not very familiar to me, was using AudioSource.PlayOneShot().

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

public class PlayerController : MonoBehaviour
{
    private AudioSource playerAudio;
    public AudioClip jumpSound;
    public AudioClip crashSound;

    // Start is called before the first frame update
    void Start()
    {
        playerAudio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerAudio.PlayOneShot(jumpSound, 1.0f);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            playerAudio.PlayOneShot(crashSound, 1.0f);
        }
    }
}

I like the simplicity of initializing a basic set of sounds through the editor and then playing the sound straight through PlayOneShot. The way I used to do it was to always set the AudioClip of a given AudioSource before playing.

All things considered, I like that Unity Learn’s official courses are taught by experts and, more importantly, use the full potential of Unity, which is not the case in a lot of YouTube or Udemy videos where instructors do it “their way”. But I wouldn’t want to offend anyone by saying that. Of course I won’t dismiss channels like Brackeys, Jason Weimann, Sebastian Lague, Sam Hogan, etc.

So this is my farewell for today and I will report back next time!

Leave a Reply

Your email address will not be published.