DEV Community

Cal
Cal

Posted on

What I've learnt the first day with Unity

I've decided to delve into the wonders of game development so that I can nurture my passion for video games alongside my passion for coding. I've taken a look into game development before but my coding skills weren't that good and so creating a game seemed almost impossible back then. Fast forward 3 years and my knowledge of C#, and coding in general, has improved massively, to the point where I think I can definitely give this another shot!

Before I talk about what I've learnt on the first day, I want to give a massive shoutout to @Tyler_Potts_ and @BrackeysTweet as their game dev tutorials are awesome and have helped a lot!

Game Engine

A game engine is the foundation for building a video game. It can provide features ranging from animation to artificial intelligence and they're responsible for all of the physics, rendering, memory management and more! The 2 main game engines that I looked into were Unity and UnrealEngine.

  • Unreal engine allows you to create higher quality assets straight out of the box, whereas it takes longer to create the same affect in Unity.
  • Unity can work on any machine. Unreal is designed more for powerful computers.
  • Unity is easier for smaller or 1 man teams. Unreal requires different teams for things such as particles, shaders, assets etc.

From these points, I decided to download Unity and make a start by following the "How to make a video game" series on YouTube.

How to make a Video Game - Getting Started

Adding Assets

This can be done from the Hierarchy panel, mine is located on the left-hand side, all you need to do is right-click and pick the object you want. For me, I was using 3D Objects such as cubes.

https://i.imgur.com/XGdoxKR.png

Adding Materials

Once I had the asset in place, I needed to make it look a bit prettier. To do this, you need to right-click on the Projects panel, go to Create, and then Material.

https://i.imgur.com/1IHK9ZD.png

Once you have that up, you can then go to the Inspector window and change the values for the material to make it whatever colour and texture you'd like. When you're happy with it, just drag it onto the asset that you want to use it.

Box Collider

Box Collider

The box collider is something which you can add to an asset so that once it comes into contact or overlaps with another GameObject, it will detect that and trigger something. These are the properties for it:

https://i.imgur.com/P7hgZU8.png

Rigidbody

Rigidbody

This will allow for GameObjects to act under the control of physics. It allows for force and torque to be added and make objects move in a realistic way.

Start and Update Methods

These are 2 methods that are created when you create a new script. The start method runs when the program starts and the update method runs for every frame that is rendered.

OnCollisionEnter Method

This method runs when a collision happens and allows you to do things like reset a score, destroy something, reset the level etc.

Adding a Force to a Rigidbody

Adding a force should be done in the FixedUpdate method as Unity works better like this than doing it in the Update method. The AddForce method has 3 params in this overload, one for each axis, x, y and z. We have to times the force by the deltaTime so that it syncs up correctly, otherwise the force will be different depending on how many frames your pc can render.

void FixedUpdate()
    {
        // Add a forward force.
        rb.AddForce(0,0,forwardForce * Time.deltaTime); // Add a force of 2000 on the z-axis.

        // This is not a brilliant way of getting the input, especially if we want to do smoothing
        // or use a controller.
        if(Input.GetKey("d")){
            rb.AddForce(sidewaysForce * Time.deltaTime,0,0);
        }

        if(Input.GetKey("a")){
            rb.AddForce(-sidewaysForce * Time.deltaTime,0,0);
        }
    }

Making a Camera Follow a Player

Here's the script for a camera that is just behind a player:

public class FollowPlayer : MonoBehaviour
{
    public Transform player;

    // stores 3 floats which makes it good for position (x, y, z);
    public Vector3 Offset;

    // Update is called once per frame
    void Update()
    {
        transform.position = player.position + Offset;
    }
}

You need to make sure to set the offset on Unity so that the camera isn't directly on top of the player, unless you want a 1st person view.

Getting User Input

This just requires you to use the Input.GetKey method, and specify which key to use. This isn't the best way of doing it, especially if you want to handle controller input, but for now it works.

void FixedUpdate()
    {
        // Add a forward force.
        rb.AddForce(0,0,forwardForce * Time.deltaTime); // Add a force of 2000 on the z-axis.

        // This is not a brilliant way of getting the input, especially if we want to do smoothing
        // or use a controller.
        if(Input.GetKey("d")){
            rb.AddForce(sidewaysForce * Time.deltaTime,0,0);
        }

        if(Input.GetKey("a")){
            rb.AddForce(-sidewaysForce * Time.deltaTime,0,0);
        }
    }

Tagging Objects

This can be good for applying the same logic to a group of objects, in my case, some collision objects.

public class PlayerCollision : MonoBehaviour
{
    public PlayerMovement playerMovement;

    public string obstacleTag = "Obstacle";

    /// <summary>
    /// OnCollisionEnter is called when this collider/rigidbody has begun
    /// touching another rigidbody/collider.
    /// </summary>
    /// <param name="collisionInfo">The Collision data associated with this collision.</param>
    void OnCollisionEnter(Collision collisionInfo)
    {
        if(collisionInfo.collider.tag == obstacleTag){
            Debug.Log(collisionInfo.collider);
            playerMovement.enabled = false;
        }
    }
}

https://i.imgur.com/Bim6lHy.png

YouTube

Thanks for getting to the end of this blog post, I hope that you've learnt a thing or two. If you learn better in video form, then make sure to subscribe to my Gaming channel, Cal Plays. I'm going to be playing as well as learning to make video games on there.

Cal Plays

Happy coding!

Cal

Top comments (0)