coding in c#

Coding In C# For FREE: Tutorial #2 Unity Multiplayer Game

Unity 3D MMO Tutorial Series

Welcome to the Unity Multiplayer Tutorial #2: Coding In C#. If you have not completed the first tutorial, go back and finish it. You can find the tutorial here.

We want to make the character do something. In this case, we want the character to move with keyboard presses. The first thing to do is add a component to the player character. According to Unity, a component is “the parts you attach to your GameObject. The parts, or components, that you attach, or add, to your GameObject are what makes your GameObject do the things you want it to do.”

In the Unity editor, select the LowPolyMan and click “Add Component” in the Inspector tab. In the search bar, type in “Character Controller.” The Character Controller puts a box around the player character so it may collide with other objects, including the ground plane. Click on the controller and press enter. In the Character Controller dialog box “Center,” change the Y axis to 0.88. In the “Radius” box, type 0.25. The “Height” should be 1.8. Your Character Controller is now snug around the player character.

We need to do one last thing to complete our character setup. In the Hierarchy tab, drag the “Main Camera” to the “LowPolyMan.” Attaching the camera to the character is a temporary task to follow the character around when we get it moving. We will change this later so we can better manipulate the camera for our needs.

unity multiplayer

Let’s Get Coding In C#!

Ok, now the fun begins! In the “Assets” folder, right-click and create a new folder. Name the new folder “Scripts.” Right-click and Create->C# Script in the newly created Scripts folder. Before pressing enter, name the script “BaseControls.”

NOTE: Change the name before pressing enter. You cannot change the name afterward it will not work.

Open up the BaseControls script in Visual Studio or whatever code editor you like. I’m using Microsoft Visual Studio 2022. If you want to use Visual Studio, go here to set it up for Unity. Once you open the file, you will see several lines of code automatically inserted for us. Most of this code won’t be used in this first script.

Delete the two methods “Start” and “Update.” Also, delete the colon and “MonoBehavior.” This code will not be needed for the first script. MonoBehavior is code that Unity uses to allow the attachment of the script to an object. The BaseControls script is only used by the PlayerControls script that we will write after BaseControls.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseControls
{
    
}

Your code should look like the above code. Now we will add our code to the script. In the BaseControls class, add the line:

public KeyCode forward, backward, rotateLeft, rotateRight, strafeLeft, strafeRight, runWalk;

Above the BaseControls class, add:

[System.Serializable]

You’re completed script will look like the following:

using UnityEngine;
[System.Serializable]
public class BaseControls 
{
    public KeyCode forward, backward, rotateLeft, rotateRight, strafeLeft, strafeRight, runWalk;
}

Coding In C#: What Does This Code Do?

The line inside the class BaseControls creates a list of movements we want to assign to the keyboard. The word “public” is used because we want another script to use this list. The term “KeyCode” will enable us to assign these to our chosen keyboard keys. [System.Serializable] is a Unity-specific code to place the list in the Inspector tab and manually select which keyboard keys or mouse buttons we want to assign to each movement on the list.

This tutorial will show the Serialized list in action with the next code script.

Star Wars Galaxies

Player Script

Ok, let’s create another script for the player keyboard movements. Right-click while in the Scripts folder and create a new script. Name this script “PlayerControls.” Open the script file and examine the contents.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

Notice that the MonoBehaviour is included. We will leave it there this time. We will need it to attach the script to the player character. Leave the Update and Start methods as well. Above the Start method, type the following:

public BaseControls baseControls;

Save your script file. We are going to attach the PlayerControls to the player character. In the Hierarchy tab, select the LowPolyMan. In the Inspector tab at the top, give the LowPolyMan a new name. I’ll call mine “Player.” While still in the Player Inspector tab, click on Add Component, search for, and click on Player Controls. A new component will pop up. Under Player Controls (Script), click the small arrow to the left of Base Controls and open it. Here is the list of controls we coded in BaseControls!

coding in c#
Base Controls

Adding The Keystrokes

In the “Player Controls (Script)” in the Inspector, click on the little white arrow to the far right of Forward and search for the letter “W.” You can also press “W” on your keyboard. Fill in the rest of the Base Controls with the following keys:

coding in c#
Base Controls Layout

Great! Now you know what the first script “BaseControls” does. The completed code so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
    public BaseControls baseControls;

    void Start()
    {
        
    }
    void Update()
    {
        
    }
}

The next tutorial will explain the code using the Base Controls. We will have the Player character moving soon! I hope you are enjoying this tutorial series. Please feel free to leave a comment.

Until next time, May The Force Be With You, Always. – Dragon

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *