Unity Tips — Code Clean Up

Scottrlooney
4 min readOct 2, 2023

--

Greetings folks! As promised, we are going to get into a brief article covering the benefits of code cleanup in our project’s PlayerCtrl script.

If you recall our last few articles we have been performing movement and shooting functions on our submarine under the Update loop. This is fine, but it does tend to create a cluttered and disorganized layout.

Furthermore, as the game progresses in complexity, and we have to handle more things in every frame, the size of our update loop gets larger and larger, which also makes it difficult to track exactly what’s going on.

So what we’re going to do is take these two functions of moving and firing our torpedo, and put them in separate functions that will both be called every frame via the Update loop. This is actually quite easy to do.

We are going to create a function or a void called PlayerMovement(). It’s referred to as a void because it doesn’t return any value — it just does the function.

You’ll also see that this has no input of any kind either, because it doesn’t need it.

All we need to do now is to copy the code in the Update loop pertaining to the movement, and put it in between the curly brackets of our PlayerMovement function.

And the last step is to delete our movement code

and call the PlayerMovement() function from the Update loop like so:

Next, we will look into our torpedo spawning code and do the same thing with a function called FireTorpedo(). We’re going to copy all of the code from the update loop we want and place it between the curly brackets.

Then we go back to the Update loop, delete our code pertaining to firing and call the FireTorpedo() function. You can also just cut and paste but copying does offer a certain degree of security in case the cut is not correct.

Note that this can actually be handled in a number of ways — for single events like this we can technically call our function after detecting the input for the mouse button, but this way is definitely visually cleaner and more easy to analyze.

Here’s what the whole code looks like at this point. You can see how much easier it is to track what’s going on in the script.

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

public class PlayerCtrl : MonoBehaviour
{
//new player location to move to
public Vector3 newLocation;
public float speed = 10.0f;
[SerializeField]
private GameObject torpedoObject;

// Start is called before the first frame update

void Start()
{

}

// Update is called once per frame
void Update()
{
PlayerMovement();

FireTorpedo();
}

void PlayerMovement()
{
//if player x position is within screen bounds
if(Input.GetKey(KeyCode.D))
{
if (transform.position.x < 9.7f)
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
else if(Input.GetKey(KeyCode.A))
{
if(transform.position.x > -9.7f)
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
else if(Input.GetKey(KeyCode.W))
{
if (transform.position.y < 4.6f)
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
else if(Input.GetKey(KeyCode.S))
{
if (transform.position.y > -4.6f)
transform.Translate(Vector3.down * Time.deltaTime * speed);
}
}

void FireTorpedo()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log("Pressed the Mouse L Button");
//set the vertical offset
float Yoffset = transform.position.y + 1.2f;
//create Vector3 to store the new position
Vector3 offset = new Vector3(transform.position.x,Yoffset,transform.position.z);
//use the new Vector3 to spawn the object at the new position
GameObject.Instantiate(torpedoObject,offset,Quaternion.identity);
}
}
}

Although it does increase the number of lines in the script it also breaks up the various functions in a logical way and leaves room for more functions reliant on Update that we’ll add in the future.

Hope this has been helpful to you. In the next article we will be discussing how we can reduce our fire rate using a cooldown function. See you then!

--

--