Unity: Show Grid Coordinates Scene View

I’ve been working with the FFTWindow analysing audio input and needed a quick way to view the coordinate space of the grid. I’m surprised there is not a feature in Unity to do this (at least not that I could find). This is a quick script to help with debugging coordinates in the Scene View.

Grid Line Coordinates on the XY plane

The script basically just draws a set of Axis in the scene and labels the x,y,z coordinates. It’s best when you are viewing along the alignment of an axis (I use the XYZ Gizmo in the corner to switch between them).

In 3D view with Cyan and Magenta sample lines.

You can toggle the Z plane on and off to make it easier to see just the XY plane and there is a few sample lines to show how it might be useful.

I was using to analyse audio spectrum data on the fly like this:

The blue Logarithmic representation of the PCM signal in red

Code

Here is the code for the grid lines/coordinates below:

It’s also up on GitHub: https://github.com/zuluonezero/UnityGrid

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

public class GridLines : MonoBehaviour
{
	public int X_Axis_Length = 10;
	public int Y_Axis_Length = 10;
	public bool Z_Plane = true;
	public int font_Size;
	public bool show_Test = true;
	
	private GUIStyle guiStyle = new GUIStyle(); //create a new variable
	public void OnDrawGizmos()
	{
		// Axis
		Gizmos.color = Color.red; // X
		Gizmos.DrawLine(new Vector3((X_Axis_Length / -1), 0, 0), new Vector3((X_Axis_Length), 0, 0));
		Gizmos.color = Color.green; // Y
		Gizmos.DrawLine(new Vector3(0, (Y_Axis_Length / -1), 0), new Vector3(0, (Y_Axis_Length), 0));
		Gizmos.color = Color.blue; // Y
		Gizmos.DrawLine(new Vector3(0, 0, (X_Axis_Length / -1)), new Vector3(0, 0, (X_Axis_Length)));  // assumes Z Axis is the same length as the X


		//Gizmos.DrawWireSphere(centre, Radius);
		guiStyle.fontSize = font_Size;
		int x = -X_Axis_Length;
		int y = -Y_Axis_Length;	
	
		while (x < X_Axis_Length)
		{

			Handles.Label(new Vector3(x, y, 0), (x + "," + y));
			
			while ( y < Y_Axis_Length)
			{
				Handles.Label(new Vector3(x, y, 0), (x + "," + y));
				y++;
			}
			 x++;
			 y = -Y_Axis_Length;
		}
		
		if (Z_Plane)
		{
			int z = -X_Axis_Length;	// Z Axis
			y = -Y_Axis_Length;	
	
			while (z < X_Axis_Length)  // Z Axis
			{

				Handles.Label(new Vector3(0, y, z), (z + "," + y));
				
				while ( y < Y_Axis_Length)
				{
					Handles.Label(new Vector3(0, y, z), (z + "," + y));
					y++;
				}
				z++;
				y = -Y_Axis_Length;
			}
		}
		
		if (show_Test)
		{
		// Test lines
		Gizmos.color = Color.cyan; 
		Gizmos.DrawLine(Vector3.zero, new Vector3(4, 5, 0));
		Gizmos.DrawWireSphere(Vector3.zero, 0.2f);
		Gizmos.DrawCube(new Vector3(4, 5, 0), new Vector3(0.2f, 0.2f, 0.2f));
		
		Gizmos.color = Color.magenta; 
		Gizmos.DrawLine(new Vector3(-3, 3, -3), new Vector3(2, 2, 2)); 
		Gizmos.DrawWireSphere(new Vector3(-3, 3, -3), 0.2f);
		Gizmos.DrawCube(new Vector3(2, 2, 2), new Vector3(0.2f, 0.2f, 0.2f));			
		}
	}
}

Unity: High CPU on Small Projects

Quick Tip: I have been working on a TCP/IP Networking project using a client/server architecture. The client (and the server for that matter) are both relatively small code bases and the UI and object count are really low in the scene. I had been struggling with CPU load in the project and feverishly trying to work out why my code was baking the CPU (and GPU!). I’d assumed it was something stupid I had done in a loop with the networking structures I was not that familiar with. It’s really not easy to concentrate on new code when your laptop fan is literally screaming at you! I’d hit Play and the CPU would spike almost immediately. So I would switch to my local terminal and scrape through the open ports and network connections looking for a smoking gun. Turns out it was the default frame rate in the Editor trying to deliver the fastest graphics performance it could on my PC – and with such a low object count and and very simple graphics being asked for it was running like a Formula One race car when all I wanted was an old jalopy.

This is my CPU on Speed

Solution: Set Target Frame Rate!

A Unity project will attempt to run your project as fast as possible. Frames will be rendered as quickly as they can (limited by your display device’s refresh rate).

There are two ways to control frame rate:

Application.targetFrameRate – controls the frame rate by specifying the number of frames your game tries to render per second. (I wrote a script to use this – see below).

QualitySettings.vSyncCount – specifies the number of screen refreshes to allow between frames. (look for it in the Editor Settings). For a 60Hz display, setting vSyncCount=2 will cause Unity to render at 30fps in sync with the display.

Note that mobile platforms ignore QualitySettings.vSyncCount and use Application.targetFrameRate to control the frame rate.

The default value of Application.targetFrameRate is -1. (the platform’s default target frame rate)

I set mine using the script to 20 and when I hit Play got this result:

This is my CPU chilling out
using UnityEngine;

public class SetFrameRate : MonoBehaviour
{		
		[SerializeField]	// Just so you can check it in the inspector
		private int FrameRate = 20; // 20 is really low but got my CPU down to < 10% - 30 is the target for mobile and was < 20% CPU usage
		//private int FrameRate = -1; // reset to default
	
		private void Awake()
		{
			Application.targetFrameRate = FrameRate;
		}
}

I attached it to my Camera object.

Set Frame Rate

One interesting behavior of setting this using a script in Unity 2020.3.26f1 was that once it was attached to the Camera object and Play was initiated for the first time it must have set the frame rate somewhere internally in the Engine. When I removed the script (for testing) the frame rate did not automatically reset to -1. I had to re-attach the script and update it to set the frame rate back to the default. I had a search of the settings in the Inspector and Preferences and couldn’t find a visible reference to it anywhere so you have to be careful if you are going to put this on a Production build that you reset it before releasing otherwise you might end up with a lower frame rate than what the platform could achieve by default.

Enough procrastination – back to sockets, ports and buffers.

Unity Message Bus

How to make use of a message bus in Unity. This is a good solution to decouple components and logically organise how your game runs. Instead of doing all the dragging in the editor of game objects and components into scripts the message bus works a bit like a proxy for the communication between scripts. This is good (makes complex things easier to manage – ie. no noodling of dependencies everywhere – and no heavy FindObject calls – plus I get to make a funny bus pun.

Events

When using Events your Objects become Subscribers and Publishers of actions. When you subscribe (or listen) to a particular event channel you get notified when something changes. These notifications go out to every object that is listening on the bus and each object script can respond to the message in their own way.

You can make this as complex as you like but it gets harder the more complex the data is that you are passing around on the bus. This example uses just four components and our events are nice and simple:

  1. An enum that lists the Events on the Bus.
  2. The YellowBus class that handles subscribtions and publishing (by using a dictionary of events)
  3. A BigYellowBusController script that is attached to our bus game object and subscribes to all the “bus” events like starting, stopping at stops, taking on passengers, etc.
  4. A PassengerController that is attached to the Bus Rider game objects (the coloured circles) which subscribes and reacts to rider type events like calling the bus, getting on and off, and ringing the bell when you want to get off.

EventsOnTheBus

This is just a simple collection of named constants that are meaningful for our events.


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

namespace AdvancedCoding.YellowBus
{
    public enum EventsOnTheBus
    {
        CALL_BUS, ALL_ABOARD, START_ENGINE, NEXT_STOP_PLEASE, STOP_ENGINE, EMERGENCY_STOP, END_O_THE_LINE
    }
}

YellowBus

This class sets up the dictionary of events matching the EventsOnTheBus to a UnityEvent call. (An even simpler example of an event call is in the docs: https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html).

It exposes the Subscribe, Unsubscribe, and Publish methods.

Notice the using UnityEngine.Events directive at the top.


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

namespace AdvancedCoding.YellowBus
{
    public class YellowBus
    {
        private static readonly IDictionary<EventsOnTheBus, UnityEvent>

        Events = new Dictionary<EventsOnTheBus, UnityEvent>();

        public static void Subscribe(EventsOnTheBus eventType, UnityAction listener)
        {
            UnityEvent thisEvent;

            if (Events.TryGetValue(eventType, out thisEvent))
            {
                thisEvent.AddListener(listener);
            }
            else
            {
                thisEvent = new UnityEvent();
                thisEvent.AddListener(listener);
                Events.Add(eventType, thisEvent);
            }
        }

        public static void Unsubscribe(EventsOnTheBus type, UnityAction listener)
        {
            UnityEvent thisEvent;

            if (Events.TryGetValue(type, out thisEvent))
            {
                thisEvent.RemoveListener(listener);
            }
        }

        public static void Publish(EventsOnTheBus type)
        {
            UnityEvent thisEvent;

            if (Events.TryGetValue(type, out thisEvent))
            {
                thisEvent.Invoke();
            }
        }

    }
}

BigYellowBusController

The controller on the bus is a subscriber to the events that it needs to react to and defines private methods of handling those events when they are broadcast. You can have any number of subscribers listening in to your events. In this example we only got two: The YellowBus and the Rider.


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

namespace AdvancedCoding.YellowBus
{
    public class BigYellowBusController : MonoBehaviour
    {
        private string _an_event;
        private bool _engine_is_running;
        private bool _rider_waiting;
        private bool _next_stop;
        public float speed = 3;

        void OnEnable()
        {
            YellowBus.Subscribe(EventsOnTheBus.START_ENGINE, StartBus);
            YellowBus.Subscribe(EventsOnTheBus.STOP_ENGINE, StopBus);
            YellowBus.Subscribe(EventsOnTheBus.EMERGENCY_STOP, Emergency);
            YellowBus.Subscribe(EventsOnTheBus.CALL_BUS, RiderWaiting);
            YellowBus.Subscribe(EventsOnTheBus.NEXT_STOP_PLEASE, NeedToGetOff);
        }

        void OnDisable()
        {
            YellowBus.Unsubscribe(EventsOnTheBus.START_ENGINE, StartBus);
            YellowBus.Unsubscribe(EventsOnTheBus.STOP_ENGINE, StopBus);
            YellowBus.Unsubscribe(EventsOnTheBus.EMERGENCY_STOP, Emergency);
            YellowBus.Subscribe(EventsOnTheBus.CALL_BUS, RiderWaiting);
            YellowBus.Subscribe(EventsOnTheBus.NEXT_STOP_PLEASE, NeedToGetOff);
        }

        private void StartBus()
        {
            _an_event = "Started!";
            _engine_is_running = true;

        }

        private void StopBus()
        {
            _an_event = "Stopped!";
            _engine_is_running = false;

        }

        private void Emergency()
        {
            _an_event = "Emergency Stop!";
            _engine_is_running = false;
        }

        private void RiderWaiting()
        {
            _rider_waiting = true;
            _an_event = "Rider waiting...";
        }

        private void NeedToGetOff()
        {
            _next_stop = true;
            _an_event = "Passenger needs to get off...";
        }


        private void OnGUI()
        {
            GUI.color = Color.green;
            GUI.Label(new Rect(300, 60, 200, 20), "BUS EVENT is...  " + _an_event);

            if (GUILayout.Button("Start Bus Up")) YellowBus.Publish(EventsOnTheBus.START_ENGINE);
            if (GUILayout.Button("Stop Bus")) YellowBus.Publish(EventsOnTheBus.STOP_ENGINE);
        }

        public void Update()
        {
            if (_engine_is_running)
            {
                transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
                if (transform.position.x > 18f)
                {
                    transform.position = new Vector3(-17f, 0f, 0f);
                }

                if (_rider_waiting)
                {
                    if (transform.position.x > 0 && transform.position.x < 1)
                    {
                        YellowBus.Publish(EventsOnTheBus.STOP_ENGINE);
                        YellowBus.Publish(EventsOnTheBus.ALL_ABOARD);
                        _rider_waiting = false;
                    }
                }

                if (_next_stop)
                {
                    if (transform.position.x > 0 && transform.position.x < 1)
                    {
                        YellowBus.Publish(EventsOnTheBus.STOP_ENGINE);
                        YellowBus.Publish(EventsOnTheBus.END_O_THE_LINE);
                        _next_stop = false;
                    }
                }
            }
        }
    }
}

RiderController

The Rider Controller does pretty much the same as the Big Yellow Bus Controller but only listens to those events that relate to the riders (some of which it shares with the Big Yellow Bus Controller). Now here is where it gets interesting… look at the NEXT_STOP_PLEASE parts. Start with the OnGui() method below on line 75 where a button press will publish the NEXT_STOP_PLEASE event. In the OnEnable() method our Subscription code calls the PressTheBell() private method (line 17) that let’s our game object handle the call and produce the text message. BUT the YellowBus Controller also subscribes to this event and handles it with the NeedToGetOff() method so that it knows it has to stop and let the passenger off (line 21, 59 and 95 in the BigYellowBusController script above). AAANNDDDD responds by triggering the STOP_ENGINE event and the END_O_THE_LINE event. See how they can chain together into larger behaviours and complex interactions between different game objects without actually entangling them at the script reference level.

Also be aware that on these scripts there is a subscription in OnEnable() and an Unsubscribe in OnDisable() (so we are not holding on to the listener when the game object disappears) the thing to note here is that we are listening to our own events. For example the RiderController triggers the CALL_BUS event and both our own script and the BigYellowBusController script react to it.


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

namespace AdvancedCoding.YellowBus
{
    public class RiderController : MonoBehaviour
    {

        private bool _lightMeGUIup;
        private string _message;
        private GameObject _bus;

        void OnEnable()
        {
            YellowBus.Subscribe(EventsOnTheBus.CALL_BUS, HopOnTheBus);
            YellowBus.Subscribe(EventsOnTheBus.NEXT_STOP_PLEASE, PressTheBell);
            YellowBus.Subscribe(EventsOnTheBus.ALL_ABOARD, JumpOn);
            YellowBus.Subscribe(EventsOnTheBus.END_O_THE_LINE, JumpOff);
        }

        void OnDisable()
        {
            YellowBus.Unsubscribe(EventsOnTheBus.CALL_BUS, HopOnTheBus);
            YellowBus.Unsubscribe(EventsOnTheBus.NEXT_STOP_PLEASE, PressTheBell);
            YellowBus.Unsubscribe(EventsOnTheBus.ALL_ABOARD, JumpOn);
            YellowBus.Unsubscribe(EventsOnTheBus.END_O_THE_LINE, JumpOff);

        }

        private void Start()
        {
            _bus = GameObject.Find("BigYellowBus");
        }

        private void PressTheBell()
        {
            _message = "I need to get off the bus !!";
            _lightMeGUIup = true;
        }


        private void HopOnTheBus()
        {
            _message = "I need to get on the bus please!!";
            _lightMeGUIup = true;
        }

        private void JumpOn()
        {
            _message = "Hi Driver!";
            transform.parent = _bus.transform;
            transform.position = new Vector3(0f, 0.2f, 0f);
        }

        private void JumpOff()
        {
            _message = "Thanks a lot!";
            transform.parent = null;
            transform.position = new Vector3(0f, -3f, 0f);
        }


        private void OnGUI()
        {


            if (GUI.Button(new Rect(160, 10, 100, 30), "Call Bus"))
            {
                YellowBus.Publish(EventsOnTheBus.CALL_BUS);
            }

            if (GUI.Button(new Rect(160, 50, 100, 30), "Bell Press"))
            {
                YellowBus.Publish(EventsOnTheBus.NEXT_STOP_PLEASE);
            }


            if (_lightMeGUIup)
            {
                GUI.color = Color.blue;
                GUI.Label(new Rect(300, 40, 200, 20), "..." + _message);
               StartCoroutine(DropGUI());
            }

        }

        private IEnumerator DropGUI()
        {
            yield return new WaitForSeconds(2f);
            _lightMeGUIup = false;
        }
    }
}

The other thing to note is that this message bus is like a party line – all messages are broadcast to anyone listening. All the passengers respond to the NEXT_STOP_PLEASE event as they all share the same code…but of course they could implement different controller scripts per player and all handle this event differently. It’s pretty powerful stuff.

Playtime in Bus Controller Land !

The code above was modified from an example in Game Development Patterns with Unity 2021: Explore practical game development using software design patterns and best practices in Unity and C#, 2nd Edition by David Baron – I recommend buying a copy it is the best book of this type that I have read – take yourself from a beginner to an intermediate Unity programmer. 🙂

Rust on RHEL WSL2

I’ve been doing a lot of back end investigation the past few months – trying to build something to support future projects. Most of it’s just random poking about with new technologies and different ways of doing “stuff” but then sometimes while doing this you find something that is really cool and resonates with the way your brain works. For me this month it was Rust and RHEL WSL.

I got here by a round about route of looking at golang programming and docker integration for back end processing of a game. A way to offload non-game-critical systems to other processors. Like a high score system that keeps player profiles and scores online which can be called from within the game when needed. I know there are heaps of good services out there that do this – but I like poking into stuff and having that level of control.

The other thing is – sometimes stuff just makes sense. I’ve been a long time linux user and champion but have always been locked into a Wintel desktop in the studio due to the support needs of my audio kit. (I have a Line 6 KB37 which integrates the midi keyboard and guitar effects pedals into one unit – it’s freaking awesome and I’m terrified that one day it will break and be out of support). I’d run linux as a virtual machine and used cygwin and my favourite mobaXterm as a solution to this but while poking around within the docker community I came across WSL. The Windows Subsystem for Linux.

WSL

The Windows Subsystem for Linux is a compatibility layer which runs Linux binary executables natively on Windows. WSL v2 has a real Linux kernel (though there are some storage issues if you need more info read the docs). The default linux kernel is Ubuntu which I’m OK with – one of my favourite linux distributions is the Ubuntu Studio but I’ve been a Red Hat admin for a long time so am more comfortable there but there was no RHEL or CentOS supported platform. But then I found the RHWSL project by a Japanese Developer called Yosuke Sano and I was intrigued and immediately hooked.

Basically this is how I set up WSL on my Windows 10 Workstation.

C:\Users\zulu>wsl –list –online
The following is a list of valid distributions that can be installed.
Install using ‘wsl –install -d ‘.

C:\Users\zulu>WSL –list –all
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
openSUSE-42 openSUSE Leap 42
SLES-12 SUSE Linux Enterprise Server v12
Ubuntu-16.04 Ubuntu 16.04 LTS
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS

C:\Users\zulu>wsl –set-default-version 2
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
The operation completed successfully.

C:\Users\zulu>WSL –HELP
Invalid command line option: –HELP
Copyright (c) Microsoft Corporation. All rights reserved.
Usage: wsl.exe [Argument] [Options…] [CommandLine]

I downloaded the RHWSL package from git, extracted it and run the executable to register the package with WSL and install the root file system and that was it.

This is how I set the RHWSL distribution as my default:

d:\RedHat\RHWSL>wsl -d RHWSL

Here are a bunch of useful links if you want to explore more:

https://docs.docker.com/desktop/windows/wsl/
https://docs.microsoft.com/en-us/windows/wsl/install
https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers
https://dev.to/bowmanjd/using-podman-on-windows-subsystem-for-linux-wsl-58ji

https://github.com/yosukes-dev/RHWSL
https://github.com/yosukes-dev/RHWSL/releases

If you want more info on WSL yosukes-dev has an “Awesome” resource list.

Rust

I started looking at Rust as part of a wider investigation into “modern” programming languages. I spent a few weeks looking at go (golang) and as much as it was a great multi-purpose language and super easy to start using I was a little gobsmacked at how large the binaries were (they are statically compiled) and being someone who is always on tiny machines I kept getting a niggling feeling that a whole system of go programs would be a big chunk of disk on a small device doing work that that might be easier done slower with a simple shell script (I exaggerate!). Anyway in a lot of the stuff I read golang and rust were comparable. I will come back to go as the community and contributions seemed most excellent.

Plus Rust made sense to me in places where Go didn’t. I really don’t have a logical excuse here or a well reasoned argument. Sometimes you just like a language cause it “feels” right. I had the same thing with Ruby. Anyway this is how I got started with Rust on the RHWSL…

https://www.rust-lang.org/learn/get-started
If you’re a Windows Subsystem for Linux user run the following in your terminal, then follow the on-screen instructions to install Rust.

curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh

I started following the hello world tutorial to set up the system and it wasn’t all plain sailing. For one I probably should have done the install as a normal user but after mucking around with environment variables and permissions into the root user directories it was just easier to do the work as root. (I know rolling over in my grave). So it wasn’t all plain sailing – and once I got to the compile stage there were a few basic compiling tools that the RHWSL needed. This is how it went:

https://doc.rust-lang.org/cargo/getting-started/first-steps.html

[root@Venom RHWSL]# mkdir rustProgramming
[root@Venom RHWSL]# cd rustProgramming/
[root@Venom rustProgramming]# cargo new hello_world
Created binary (application) hello_world package
[root@Venom rustProgramming]#
[root@Venom rustProgramming]# ls -ltr
total 0
drwxrwxrwx 1 root root 4096 Feb 8 18:27 hello_world
[root@Venom rustProgramming]#
[root@Venom rustProgramming]# cd hello_world/
[root@Venom hello_world]# ls -ltr
total 0
-rwxrwxrwx 1 root root 180 Feb 8 18:27 Cargo.toml
drwxrwxrwx 1 root root 4096 Feb 8 18:27 src

[root@Venom hello_world]# cat Cargo.toml
[package]
name = “hello_world”
version = “0.1.0”
edition = “2021”
See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[root@Venom hello_world]# cat src/main.rs
fn main() {
println!(“Hello, world!”);
}

[root@Venom hello_world]# cargo build
Compiling hello_world v0.1.0 (/mnt/d/RedHat/RHWSL/rustProgramming/hello_world)
error: linker cc not found
|
= note: No such file or directory (os error 2)
error: could not compile hello_world due to previous error

Bingo – first problem – no compiler. I installed make and gcc – not really sure if I needed make but figured it would be a nice to have anyway.

[root@Venom hello_world]# which make
/usr/bin/which: no make in (/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/Program Files (x86)/ ……….lots more here

[root@Venom hello_world]# which cc
/usr/bin/which: no cc in (/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/Program Files (x86)/ ……….lots more here

[root@Venom hello_world]# yum install make
…Dependencies resolved.
Package Architecture Version Repository Size
Installing:
make x86_64 1:4.2.1-10.el8 ubi-8-baseos 498 k
…. more in here you don’t need to see …
Installed:
make-1:4.2.1-10.el8.x86_64
Complete!

[root@Venom hello_world]# which make
/usr/bin/make

[root@Venom hello_world]# yum install gcc
…Dependencies resolved
…. more in here you don’t need to see …
Installing dependencies:
binutils x86_64 2.30-108.el8_5.1 ubi-8-baseos 5.8 M
cpp x86_64 8.5.0-4.el8_5 ubi-8-appstream 10 M
glibc-devel x86_64 2.28-164.el8 ubi-8-baseos 1.0 M
glibc-headers x86_64 2.28-164.el8 ubi-8-baseos 480 k
…Transaction Summary
Install 14 Packages
Total download size: 51 M
Installed size: 123 M
Installed:
Complete!

[root@Venom hello_world]# which cc
/usr/bin/cc

[root@Venom hello_world]# cargo build
Compiling hello_world v0.1.0 (/mnt/d/RedHat/RHWSL/rustProgramming/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 2.09s

[root@Venom hello_world]# ls -ltr
total 0
-rwxrwxrwx 1 root root 180 Feb 8 18:27 Cargo.toml
drwxrwxrwx 1 root root 4096 Feb 8 18:27 src
-rwxrwxrwx 1 root root 155 Feb 8 18:28 Cargo.lock
drwxrwxrwx 1 root root 4096 Feb 8 18:28 target

You can run the executable like this:

[root@Venom hello_world]# ./target/debug/hello_world
Hello, world!

Or just run from command:

[root@Venom hello_world]# cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running target/debug/hello_world
Hello, world!

Yay !

More Gaps

This is more concept art for a protagonist in the new project called The Gap. Okapi Boy – I have always loved Okapi’s. They are shy and remained hidden from the world in the deepest parts of the jungle until they were discovered in the 50’s. Beautiful Zebra like patterns on skin like a Deer. This is a deeply peaceful animal with large soulful eyes. He dreams and offers advice but you have to seek him out and finding him is not always easy or straightforward. In The Gap you get the chance to play as the Okapi for part of the story.

Okapi Boy

There used to be a great example of this rare creature in the Melbourne Museum Taxidermy collection (which I used to love but has now sadly been taken off the exhibit floor). You can take a virtual tour of the old “Wild” exhibition here.

I have a LOT more concept art to do as I want to get a very clear picture of my characters before I start modelling.

I also stared working on the programming for the game. One element I want to include in the story is a “mini-game” called Fox and Geese. It’s played on a chess board and was one of the first games I remember my dad teaching me how to play. It’s pretty straightforward and uses a chess board and pawns. Four white pawns are the Geese who can only move forward and diagonally one row at a time. The Fox is a black pawn that can move diagonally one space at a time in any direction. Moves are taken in turn and the aim of the game is for either the Fox to break the line of Geese or the Geese to “capture” the Fox so that he cannot move.

Fox And Geese

It’s early days yet with only the board set up and pawn selection scripts started but it was actually pretty fun setting up the programming that took care of the board. When you learn programming you get to do lots of arrays and nothing screams “ARRAY” like a chess board. So it’s kind of nice to use one of those basic skills that you use in all programming in such a nice simple example. You get to add a little interest to the basic array with the inclusion of alternating colours. Here is the code below:

using UnityEngine;

public class FnG_MakeBoard : MonoBehaviour
{
    public GameObject boardPiece;
    public Vector3[] positions;

    // Start is called before the first frame update
    void Start()
    {
        positions = new Vector3[64];
        float start_x_pos = -1.75f;
        float start_y_pos = -1.75f;
        float rowcount = 0f;
        float linecount = 0f;


        for (int i = 0; i < positions.Length; i++)
        {
            positions[i] = new Vector3(start_x_pos, start_y_pos, 0f);
            start_x_pos += 0.5f;
            rowcount += 1;

            if (rowcount == 8)
            {
                start_x_pos = -1.75f;
                start_y_pos += 0.5f;
                rowcount = 0f;
                linecount++;
            }

            Instantiate(boardPiece, positions[i], Quaternion.identity);
            Color32 myRed = new Color32(185, 48, 48, 255);
            Color32 myGray = new Color32(106, 106, 106, 255);
            if (i % 2 == 0)
            {
                if (linecount % 2 == 0)
                {
                    boardPiece.GetComponent<SpriteRenderer>().color = myGray;
                }
                if (linecount % 2 != 0)
                {
                    boardPiece.GetComponent<SpriteRenderer>().color = myRed;
                }
            }
            if (i % 2 != 0)
            {
                if (linecount % 2 == 0)
                {
                    boardPiece.GetComponent<SpriteRenderer>().color = myRed;
                }
                if (linecount % 2 != 0)
                {
                    boardPiece.GetComponent<SpriteRenderer>().color = myGray;
                }
            }
        }
    }

Vector Maths with Unity Gizmos

It’s really nice to get a game finally published. Endless Elevator is now on the Google Play Store so I’ve been planning and experimenting and just plain playing around with Unity and Game Ideas before I start a new project. It’s kind of like a holiday.

I like maths and I also like visualising it with Unity. This little project is one of the simplest types of maths that is generally associated with game programming and that’s adding and subtracting vectors and getting the distance between two vectors.

These sorts of functions are endlessly useful. They answer questions like: How far away is my target? Is my Player going to fall off the platform? Can I jump between two platforms? And so on and so on.

What I like about this one is that it highlights two things. 1. The difference between a player and a target’s perspective (in this project I nominate the circle as the player and the cylinder as the target), and 2. calculating the magnitude or distance between them.

To calculate the distance (magnitude) between two points you take the square root of the square of the x and y values. Which sounds a little complicated but is really just making a square the size and length of your x, and adding that to a square the size of your y, and then getting the square root of the total size of that square.

This is how it looks in the Scene View:

How it works

The project is really easy to set up. All you need is an Empty Game Object to hold the script and two other objects. I used a Capsule and a Sphere. Drag the two objects into the public Transform components on the script.

The Project Hierarchy and Properties of the Script

Here is the reproduced script below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UI;
    
public class GridGizmo : MonoBehaviour
{

    public Transform circle;
    public Transform capsule;
    public Vector3 differenceToCapsule;
    public Vector3 differenceToCircle;
    public float magnitude;

    void Update()
    {
        differenceToCapsule = capsule.position - circle.position;
        differenceToCircle = circle.position - capsule.position;
        /*
           Magnitude of a Vector (Length of the Vector)
           We use Pythagoras' theorem to calculate it:
           |a| = √( x2 + y2 )
        */
        magnitude = Mathf.Sqrt(Mathf.Pow(differenceToCapsule.x, 2) + Mathf.Pow(differenceToCapsule.y, 2));
    }


    public void OnDrawGizmos()
    {

        // Grid Number Labels
        for (int i = -10; i < 11; i++)
        {
        UnityEditor.Handles.Label(new Vector3(0f, i, 0f), "" + i);
        }

        for (int i = -20; i < 21; i++)
        {

            UnityEditor.Handles.Label(new Vector3(i, 0f, 0f), "" + i);
        }

        // Labels of Objects
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y, circle.position.z), "POSI: " + circle.transform.position);
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y - 0.5f, circle.position.z), "DIFF: " + differenceToCapsule);
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y - 1f, circle.position.z), "Magnitude: " + magnitude);

        UnityEditor.Handles.Label(new Vector3(capsule.position.x + 1, capsule.position.y, capsule.transform.position.z), "POSI: " + capsule.transform.position);
        UnityEditor.Handles.Label(new Vector3(capsule.position.x + 1, capsule.position.y - 0.5f, capsule.transform.position.z), "DIFF: " + differenceToCircle);

        // The line
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(circle.position, capsule.position);
    }
}

As I mentioned above I do like mathy things in Unity. Here are some of the other one’s I’ve done which are a bit more complicated. I find them really really useful to cement those ideas that I should know back to front but always forget exactly how they work.

Unity: Circular Movement with Triangles

My son asked to do some game programming with me last week. I was super excited. We did an eyeball rolling around in a circular motion (see image below). He did everything from working in Unity to making the assets and materials and I was very proud. I helped with the function to make it circle. It got me thinking how to explain circular movement and the use of Pythagorean triangles and cos and sin functions. They are simple maths but kind of hard to explain without a picture so hence this project about making circles with triangles.

Rolling Eyeballs!

The Basic Idea

To simplify I’ll work in 2D so we only got two Axis. X and Y. The Center of our circle will be on the 0 (zero, zero). So we could think of this as a co-ordinate system with two planes. If we were mathematicians we would call this a Cartesian Plane after Rene Descartes but being a Game Developer with Unity I’m going to call it the Scene view in 2D.

Our circle is defined by it’s Radius (ie. the distance from the Center).

On each Update() event our GameObject gets a new X,Y position on the Radius of the circle.

The Angle of Change

To start with we work out what the angle of difference is between this Update and the last one. The time passed during that period is known as the DeltaTime. Our “speed” is how many Radians we are travelling around the circle in that time. A radian is the length of the radius laid around the circumference of the circle. We multiply the Speed (how fast we are ticking through the radians) by the DeltaTime (time passed since last Update) to tell us that angle size.

# angleInRadians += RotateSpeed * Time.deltaTime;

Convert Polar to Cartesian

After working out the angle we have what is called a Polar Coordinate. That is we define our location by how far away it is (the distance – in this case as it’s a circle it’s always the same i.e. the radius), and what the angle (θ) is. Now we need to convert between that definition of a location in the Scene View to another one we can use in the Unity move function.

This is where Pythagoras and right angled triangles comes in – to convert from Polar Coordinates (r,θ) to Cartesian Coordinates (x,y) we use cos and sin :
x = r × cos( θ )
y = r × sin( θ )

In Unity there is already a function that does this:

newPosition = new Vector2(Mathf.Cos(angleInRadians), Mathf.Sin(angleInRadians)) * Radius;

But what does this really mean? Cos and Sin are just numbers. They represent the relationship between two sides of the triangle.

For example cos is the value of the relationship between the side of the triangle that is adjacent to the angle (the side next to the angle) and the hypotenuse (the long side on the other side of the angle). We are going to use cos to find the x value (how far along the horizon it is) in our Vector2 position. The same way we use Sin to find the y position (how far up or down).

In the image below see how there are squares built off each side of the triangle. Pythagorean theory for right angled triangles states that the area/volume of the two squares built of the two smaller sides will be equal to the area/volume of the big square built off the longest (hypotenuse) side of the triangle.

The big blue square has a volume of 1 because the radius of the circle is 1 unit on the Cartesian plane and One Squared is One. The volume of the other two squares (Magenta and Yellow) will always add up to one. Their volumes added together will always equal the volume of the blue square. This is the Pythagorean theory of right angled triangles in action. The length of a side of the Yellow box is the x value and the length of a side on the Magenta box is the y value. That’s our current position in (x, y) format which we can pass to the transform.position function.

The two images below show two Updates() that are reasonably close together so you can see in detail and in “freeze frame” what’s going on between the Updates() with all the variables. You can see the angle go from about 8 degrees to 20 degrees and the changing values for sin and cos which result in changing (x,y) values and volumes of the squares.

One Position on Update()
The next position on Update()

That’s basically it apart from some modifications to the values for being on the negative sides of the circle.

The script attached to the moving object is below but I’ve also put it on github here: https://github.com/zuluonezero/MoveInACircleWithTriangles

I’m finding it deeply satisfying to watch the triangles and squares get used to define a circle going round over and over again.

If this sort of thing floats your boat I’ve done some other posts on making curves using intersecting lines:

http://www.zuluonezero.net/2019/06/04/unity-2d-curves-using-triangles/

Also on using sin to use curves in movement: http://www.zuluonezero.net/2018/06/20/fly-birdy-fly-2d-curved-movement-in-unity/.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

public class CircleMe : MonoBehaviour
{

    public float RotateSpeed = 5f;      // How fast we move through the radians
    public float Radius = 0.1f;         // How "deep" the circle is

    public Vector2 centreOfCircle;      // The Centre of our circle
    public float angleInRadians;        // The angle (in radians) between our position between one update and the next
                                        // A radian is the angle created if the length of the radius 
                                        // is laid along the circumference of the circle (about 57.2958 degrees)
    public Vector2 newPosition;         // The new position for every new Update event

    public Text displayText;
    public float angleInDegrees;
    private LineRenderer triLine;
    private Vector3 centre;
    private Vector3 yLoc;
    public float angle4Display;
    private Vector3 sq1;
    private Vector3 sq2;
    private Vector3 sq3;
    private Vector3 sq4cyan1;
    private Vector3 sq4cyan2;
    public Vector2 slopecyan;
    public Vector2 p1cyan;
    public Vector2 p2cyan;

    private void Start()
    {
        centreOfCircle = transform.position;
        // (0, 0) but could be anywhere
        centre = transform.position;
    }

    private void Update()
    {

        angleInRadians += RotateSpeed * Time.deltaTime;
        if (angleInRadians > 6.28319f)
        {
            angleInRadians = (angleInRadians % 6.28319f);
        }
        // eg.       0 += 5 * 0.25  (answer is 1.25)    // if deltaTime was 0.25 of a second
        // and our initial angle was 0 radians. 
        // Remember += is short for x = the current value of x plus itself (x = x + x)

        // we need to convert the angle and radius into an x, y position
        newPosition = new Vector2(Mathf.Cos(angleInRadians), Mathf.Sin(angleInRadians)) * Radius;
        //          = new Vector2(opposite/hypotenuse(1.25), adjacent/hypotenuse(1.25)) * Radius;
        // (x, y)   =            (0.9997, 0.0218) * 0.1
        // (x, y)   =            (0.09997, 0.00218)
        transform.position = centreOfCircle + newPosition;   // Adding Vectors
                                                             // (0.09997, 0.00218) = (0, 0) + (0.09997, 0.00218)
                                                             // this is our starting (x, y) position

        // Now do it again for the next Update - the code below has been duplicated for this example
        /*
        angleInRadians += RotateSpeed * Time.deltaTime;
        // eg.       1.25 += 5 * 0.25  (answer is now 1.25 + 1.25 = 2.5)    // if deltaTime was 0.25 of a second 
        newPosition = new Vector2(Mathf.Cos(angleInRadians), Mathf.Sin(angleInRadians)) * Radius;
        //          = new Vector2(opposite/hypotenuse(2.5), adjacent/hypotenuse(2.5)) * Radius;
        // (x, y)   =            (0.99904, 0.0436) * 0.1
        // (x, y)   =            (0.09990, 0.00436)
        transform.position = centreOfCircle + newPosition;   // Adding Vectors
        // (0.09990, 0.00436) = (0, 0) + (0.09990, 0.00436)
        */
        if (transform.position.x > 0)
        {
            yLoc = new Vector3(centre.x + Radius, centre.y, centre.z);
        }
        else
        {
            yLoc = new Vector3(centre.x - Radius, centre.y, centre.z);
        }

        angleInDegrees = angleInRadians * 57.2958f;
}

    public void OnDrawGizmos()
    {
        // Radius
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(centre, Radius);

        // Yellow square
        Gizmos.color = Color.yellow;
        sq1 = new Vector3((transform.position.x / 2), (transform.position.x / 2f), 0f);


        if (transform.position.x > 0)
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x / 2), -(transform.position.x / 2f), 0f), new Vector3((transform.position.x), (transform.position.x), (transform.position.x)));
                Handles.Label(new Vector3((transform.position.x / 2), -(transform.position.x / 2f), 0f), "Vol: " + (transform.position.x * transform.position.x));

            }
            else
            {
                Gizmos.DrawWireCube(sq1, new Vector3((transform.position.x), (transform.position.x), (transform.position.x)));
                Handles.Label(sq1, "Vol: " + (transform.position.x * transform.position.x));
            }

        }
        else
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawWireCube(sq1, new Vector3((transform.position.x), (transform.position.x), (transform.position.x)));
                Handles.Label(sq1, "Vol: " + (transform.position.x * transform.position.x));
            }
            else
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x / 2), -(transform.position.x / 2f), 0f), new Vector3((transform.position.x), (transform.position.x), (transform.position.x)));
                Handles.Label(new Vector3((transform.position.x / 2), -(transform.position.x / 2f), 0f), "Vol: " + (transform.position.x * transform.position.x));
            }

        }

        // Magenta square
        Gizmos.color = Color.magenta;
        sq2 = new Vector3((transform.position.y / 2), (transform.position.y / 2f), 0f);
        if (transform.position.x > 0)
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x + transform.position.y / 2), (transform.position.y / 2f), 0f), new Vector3((transform.position.y), (transform.position.y), (transform.position.y)));
                Handles.Label(new Vector3((transform.position.x + transform.position.y / 2), (transform.position.y / 2f), 0f), "Vol: " + (transform.position.y * transform.position.y));
            }
            else
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x + Mathf.Abs(transform.position.y) / 2), (transform.position.y / 2f), 0f), new Vector3((transform.position.y), (transform.position.y), (transform.position.y)));
                Handles.Label(new Vector3((transform.position.x + Mathf.Abs(transform.position.y) / 2), (transform.position.y / 2f), 0f), "Vol: " + (transform.position.y * transform.position.y));
            }

        }
        else
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x - transform.position.y / 2), (transform.position.y / 2f), 0f), new Vector3((transform.position.y), (transform.position.y), (transform.position.y)));
                Handles.Label(new Vector3((transform.position.x - transform.position.y / 2), (transform.position.y / 2f), 0f), "Vol: " + (transform.position.y * transform.position.y));
            }
            else
            {
                Gizmos.DrawWireCube(new Vector3((transform.position.x + transform.position.y / 2), (transform.position.y / 2f), 0f), new Vector3((transform.position.y), (transform.position.y), (transform.position.y)));
                Handles.Label(new Vector3((transform.position.x + transform.position.y / 2), (transform.position.y / 2f), 0f), "Vol: " + (transform.position.y * transform.position.y));
            }
        }   

        // Red Triangle
        Gizmos.color = Color.red;
        Gizmos.DrawLine(centre, new Vector3(transform.position.x, centre.y, centre.z));
        Gizmos.DrawLine(new Vector3(transform.position.x, centre.y, centre.z), transform.position);
        Gizmos.DrawLine(transform.position, centre);

        if (transform.position.x > 0)
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawLine(new Vector3(transform.position.x - 0.1f, 0f, 0f), new Vector3(transform.position.x - 0.1f, 0.1f, 0f));
                Gizmos.DrawLine(new Vector3(transform.position.x - 0.1f, 0.1f, 0f), new Vector3(transform.position.x, 0.1f, 0f));
            }
            else
            {
                Gizmos.DrawLine(new Vector3(transform.position.x - 0.1f, 0f, 0f), new Vector3(transform.position.x - 0.1f, -0.1f, 0f));
                Gizmos.DrawLine(new Vector3(transform.position.x - 0.1f, -0.1f, 0f), new Vector3(transform.position.x, -0.1f, 0f));
            }
            
        }
        else
        {
            if (transform.position.y > 0)
            {
                Gizmos.DrawLine(new Vector3(transform.position.x + 0.1f, 0f, 0f), new Vector3(transform.position.x + 0.1f, 0.1f, 0f));
                Gizmos.DrawLine(new Vector3(transform.position.x + 0.1f, 0.1f, 0f), new Vector3(transform.position.x, 0.1f, 0f));
            }
            else
            {
                Gizmos.DrawLine(new Vector3(transform.position.x + 0.1f, 0f, 0f), new Vector3(transform.position.x + 0.1f, -0.1f, 0f));
                Gizmos.DrawLine(new Vector3(transform.position.x + 0.1f, -0.1f, 0f), new Vector3(transform.position.x, -0.1f, 0f));
            }

        }

        // Cyan Square
        Gizmos.color = Color.cyan;
        if ((transform.position.x > 0 && transform.position.y > 0) || (transform.position.x < 0 && transform.position.y < 0))
        {
            slopecyan = new Vector2(transform.position.x, transform.position.y);
            p1cyan = new Vector2((transform.position.x - slopecyan.y), (transform.position.y + slopecyan.x));
            p2cyan = new Vector2((centre.x - slopecyan.y), (centre.y + slopecyan.x));
            Gizmos.DrawLine(transform.position, p1cyan);
            Gizmos.DrawLine(centre, p2cyan);
            Gizmos.DrawLine(p1cyan, p2cyan);
        }
        else
        {
            slopecyan = new Vector2(transform.position.x, transform.position.y);
            p1cyan = new Vector2((transform.position.x + slopecyan.y), (transform.position.y - slopecyan.x));
            p2cyan = new Vector2((centre.x + slopecyan.y), (centre.y - slopecyan.x));
            Gizmos.DrawLine(transform.position, p1cyan);
            Gizmos.DrawLine(centre, p2cyan);
            Gizmos.DrawLine(p1cyan, p2cyan);
        }

        Vector3 lbl = new Vector3((p1cyan.x / 2), (p1cyan.y / 2), 0f);
        Handles.Label((lbl), "Vol: " + (Radius * Radius));

        // Angle Marker
        if (transform.position.y > 0)
        {
            if (transform.position.x > 0)
            {
                angle4Display = angleInDegrees;
                Handles.DrawSolidArc(centre, Vector3.forward, yLoc, angle4Display, 0.25f);
            }
            else
            {
                angle4Display = -(angleInDegrees - 180f);
                Handles.DrawSolidArc(centre, Vector3.forward, transform.position, angle4Display, 0.25f);
            }
        }
        else
        {
            if (transform.position.x < 0)
            {
                angle4Display = (angleInDegrees - 180f);
                Handles.DrawSolidArc(centre, Vector3.forward, yLoc, angle4Display, 0.25f);
            }
            else
            {
                angle4Display = -(angleInDegrees - 360f);
                Handles.DrawSolidArc(centre, Vector3.forward, transform.position, angle4Display, 0.25f);
            }
        }



        // Labels
        Handles.color = Color.blue;
        Handles.Label(centreOfCircle, angle4Display.ToString());
        Handles.color = Color.white;
        Handles.Label(transform.position, "X: " + System.Math.Round(transform.position.x, 2) + " Y: " + System.Math.Round(transform.position.y, 2));
        // sin opposite/hypotenuse
        Handles.Label(new Vector3(1.2f, 0.8f, 0f), "sin opposite/hypotenuse");
        Handles.Label(new Vector3(1.3f, 0.7f, 0f), "sin: " + Vector3.Distance(centre, (new Vector3(transform.position.y, 0f, 0f))) / Vector3.Distance(centre, (transform.position)) );
        // cos adjacient/hypotenuse
        Handles.Label(new Vector3(1.2f, 0.6f, 0f), "cos adjacient/hypotenuse");
        Handles.Label(new Vector3(1.3f, 0.5f, 0f), "cos: " + Vector3.Distance(centre, (new Vector3(transform.position.x, 0f, 0f))) / Vector3.Distance(centre, (transform.position)));
        // tan opposite/adjacient
        Handles.Label(new Vector3(1.2f, 0.4f, 0f), "tan opposite/adjacient");
        Handles.Label(new Vector3(1.3f, 0.3f, 0f), "tan: " + Vector3.Distance(centre, (new Vector3(transform.position.y, 0f, 0f))) / Vector3.Distance(centre, (new Vector3(transform.position.x, 0f, 0f))));

        Handles.Label(new Vector3(1f, -0.3f, 0f), "Next Position on Update()");
        Handles.Label(new Vector3(1f, -0.4f, 0f), "newPosition = new Vector2(Mathf.Cos(angleInRadians), Mathf.Sin(angleInRadians)) * Radius");
        Handles.Label(new Vector3(1f, -0.5f, 0f), "= new Vector2(opposite/hypotenuse(angleInRadians), adjacent/hypotenuse(angleInRadians)) * Radius");
        Handles.Label(new Vector3(1f, -0.6f, 0f), "" + Mathf.Cos(angleInRadians) + ", " + Mathf.Sin(angleInRadians) + " * "  + Radius + " = " + newPosition);

    }

}

/*
        Using Cartesian Coordinates we mark a point by how far along (x) and how far up (y) it is:
        Using Polar Coordinates we mark a point by how far away (magnitude or in this case as it's a circle always the radius is the same), and what angle (θ) it is:
        To convert from Polar Coordinates (r,θ) to Cartesian Coordinates (x,y) :
        x = r × cos( θ )
        y = r × sin( θ )

        Example: add the vectors a = (8,13) and b = (26,7)
        c=a+b
        c= (8,13) + (26,7) = (8+26,13+7) = (34,20)
*/

Unity Code Review and Style Guide

Hi Trixie here. It’s Code Review time! Yay. Time to clean up that code and start really looking at all the crap work you’ve done over the last months and clean that sh!t up!

I’ll post our in-house Style Guide (which of course goes out the window when you are coding in anger) at the bottom of the post. Hopefully someone else will follow it or find it useful.

This is how the code review works:

  • Make a list of all your scripts and the game objects they are attached to.
  • Go through all the variables/functions/iterators and make them follow the standard in the Style Guide (which includes making logical names sensible to humans).
  • Check all your Public references and make Private if you are really not using them.
  • Trawl the console output and clean up the extraneous debugging guff.
  • Start grouping your code into functions that do a similar thing and try and make them standardised.
  • If you are lucky you will find some optimisations in there as well to make your game run faster, better, more efficient, lighter.

Then at least it will be another few months before you muck it up again.

I know there are automated tools to do some of this work but I prefer to work though my own methods. I like to have complete control over the process. My favourite tool for analysis is the Unix Command line (I know weird right?). My workstation is Windows 10 but I have a Cygwin like utility called MobaXterm installed which allows me to interrogate the file system like a Unix machine and use grep and other commands on all the files that make up my scripts.

Basically I want to build a big spreadsheet of information that lists all my scripts and the Game Objects they attach to and the Public interfaces and variables etc.

This is one that I started for Endless Elevator:

Then I start extracting Game Objects and Code loops using the Unix Command Line so I can start to build a picture of what’s going on.

One of the outcomes of this process is that I want to be left with a map of how stuff works that I can connect the dots on in the future. It’s kind of like a Design Document in reverse.

Here are a few examples of the commands I’m using and how the information is extracted:

# grep bool *.cs # Getting out all my bools – I tend to use true or false tests a lot to explicitly define functions and events.

As you can see above the bool names are mostly descriptive and the variable name is mostly in camelCase. Those few like swingme will get updated to swingMe and the overly generic “yes” and “yesNow” (what was I thinking!) will be made more descriptive of what we are really agreeing to in that code.

This command looks at all the sort of functions I am using and where. It’s nice to know which one’s don’t have an Update() function and where all my Collisions and Triggers are.

# egrep “{|}” AI.cs # I use something like this to collect an idea of the complexity of my loops and functions.

# egrep “{|}(|)|if|for|else” AI.cs # This is my favourite type of command. Like the command above I use it for working out the structure of a script. This is way easier than scrolling through lines and lines of code and comment and makes it really easy to spot areas where you have gone loopy crazy pants.

# grep GameObject *.cs # This one makes a good list of all the scripts that I reference another Game Object in. It helps build a visual map of what dependencies there are between objects.

# grep script *.cs # This one does a similar thing in that it grabs all the times that I am calling something from within another script attached to another Game Object..

The command line is also a nice quick way to check how many lines of code you have written.

There are some good Style Guides out there.

I like this one for it’s organised folder structure and naming convention for files: https://github.com/stillwwater/UnityStyleGuide

I like the Microsoft C# one for layout and block style: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions

I like this for it’s use of camelCase and Capitals and it’s preferred block style: https://github.com/raywenderlich/c-sharp-style-guide

As a basic guide though I always try and mimic the Unity API manual. For example this page for OnCollisionEnter:

// A grenade
// - instantiates an explosion Prefab when hitting a surface
// - then destroys itself
using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour
{
    public Transform explosionPrefab;
    void OnCollisionEnter(Collision collision)
    {
        ContactPoint contact = collision.contacts[0];
        Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
        Vector3 position = contact.point;
        Instantiate(explosionPrefab, position, rotation);
        Destroy(gameObject);
    }
}

The comments are appropriate. The Class name uses a Capital for each word. The curly brackets are on a new line and indented block space. Varables are in camelCase.

Here is our minimal Style Guide. This is the basics of how we roll things:

Thanks for reading. See you all in the New Year… Trixie.

Unity Audio vs Wwise

To start with I wanted to do a general investigation into Wwise the integrated audio package for Unity by AudioKinetic. When I started working through it I figured it would be more interesting to look at Wwise in comparison to Unity’s own audio API and mixer components which have been around since Unity 5.

To do that I’m going to compare a game in three different builds. Build one is it’s original state with simple scripts that run an AudioSource.Play() method. The Second build I will add another layer of complexity by using the Unity built in Mixer and see if there are any differences or advantages. Lastly I’ll redo the project with the Wwise API and investigate how that impacts build size and project complexity and weigh it up against the previous two builds. Mostly I’m looking for difference in performance between the three builds, build size and complexity, and weighing that up against ease of implementation and flexibility.

I refreshed an old project called “MusicVisualiser”that I started for my Five Games in Ten Weeks Challenge. The game is like a singing solar system. There is a bunch of “planets” in the night sky that play a set piece of music when clicked. It’s a really simple concept and project but I think it will work for this comparison as the parameters can be limited to just a few audio tracks but we can play with spacing and roll-off and other advanced audio features.

Let’s have a look at the game first.

These “planets” are simple native Unity sphere meshes with an Audio Source component and a particle system that’s triggered when it’s clicked. You can see in the Audio Source that we are not using a Mixer for Output and all the Audio sources compete for resources and play at their default volume and priority.

The PlayMe script just takes in the AudioSource and plays it:

   public AudioSource my_sound;
        
  if (Input.GetMouseButtonDown(0))
  {
      RaycastHit hitInfo;
      target = GetClickedObject(out hitInfo);
      if (target != null && target.name == my_name)
      {
          _mouseState = true;
          screenSpace = Camera.main.WorldToScreenPoint(target.transform.position);
          offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));

          my_sound.Play();   // This is the Audio Component!
          var expl1 = GetComponent<ParticleSystem>();
          expl1.Play();
      }
  }

Pretty simple right. This is what the project looks like in the Profiler when it’s running and being actively engaged with. At that point we are looking at two Audio Sources are playing:

This is the build size from the Editor Log with our Audio Files broken out:

Build Report (Audio.Play)
Uncompressed usage by category:
Textures 0.0 kb 0.0%
Meshes 0.0 kb 0.0%
Animations 0.0 kb 0.0%
Sounds 547.5 kb 1.4%
Shaders 188.0 kb 0.5%
Other Assets 1.4 kb 0.0%
Levels 38.3 kb 0.1%
Scripts 941.9 kb 2.4%
Included DLLs 3.9 mb 10.2%
File headers 9.3 kb 0.0%
Complete size 38.6 mb 100.0%

Used Assets and files from the Resources folder, sorted by uncompressed size:
204.3 kb 0.5% Assets/SomethingLurks_AAS.wav
164.5 kb 0.4% Assets/Step2Down_AAS.wav
136.9 kb 0.3% Assets/Underwater_AAS.wav
41.8 kb 0.1% Assets/M1_M12_37_ThumPiano_Aflat1.wav

Unity Audio with Mixer

Now we add in the Mixer component to the project:

Then add a couple of Channels to the Mixer to split the audio between left and right. Then the Audio Sources are dropped into one or another of the Mixer Channels:

Adding the Mixer as the Output source

Next for bit more interest I added some effects in the Mixer. Here is where we see the advantages of using the Unity Mixer. Sounds can be manipulated in complex ways and the Audio Output chain be defined with presets and levels etc.

If we have a look at our Profiler while running with the new component we cannot really see any great differences. The ‘Others’ section of the CPU Usage is a bit higher and the Garbage Collector in the Memory is pumping regularly but the Audio Stats look pretty much unchanged:

Profiler Mixer

Mind you this is a fairly low utilising game so we might get wildly different stats if we were really putting the system under the pump but I’m not performance testing here just comparing run states between the two builds.

Next if we build the game and have a look at the Editor Log the only thing that’s changed here is that the “Other Assets” size is a KB higher (Complete size has not been changed):

Build Report (Mixer)
Uncompressed usage by category:
Textures 0.0 kb 0.0%
Meshes 0.0 kb 0.0%
Animations 0.0 kb 0.0%
Sounds 547.5 kb 1.4%
Shaders 188.0 kb 0.5%
Other Assets 2.3 kb 0.0%
Levels 38.3 kb 0.1%
Scripts 941.9 kb 2.4%
Included DLLs 3.9 mb 10.2%
File headers 9.3 kb 0.0%
Complete size 38.6 mb 100.0%

Unity with Wwise

Next we are going to add Wwise to the Project. This is the basic workflow. In the Wwise Launcher we register our project and on the first tab we are presented with three Hierarchies.

Project Audio Explorer in Wwise

The Master-Mixer Hierarchy – does what it says.
The Actor-Mixor Hierarchy – where most of your game audio develops (use the SoundSFX defaults).
Interactive Music Hierarchy – other stuff we won’t get into.

Events Tab

The next tab along is the events tab where you link your audio to game events. You can define your event here (use the default work unit).
Once you got the event there you can associate the event with the audio in the Action List.

SoundBank Tab – this is the bit that get’s imported into your project.

Next you generate a SoundBank with Wwise that includes your audio and the code for the API calls to trigger sounds. You export that SoundBank into your game engine and link up the calls in your code.

To Get Started with Wwise

To get started make an account with Audiokinetic and download the Wwise Launcher. The Integration package for Unity can be downloaded and installed directly from the WWise Launcher.

In the Wwise Launcher there is a WWISE tab that you can install and start the application from. Once you open it up you need to register your project within the launcher so Wwise can track you 🙂 ( click on the key icon next to your Wwise project and select ‘Register your Project to obtain a License’). Wise will run in Trial mode which restricts the SoundBank content to 200 media assets and cannot be used for Commercial purposes. Pricing for licensing is on their site but this is not a sales piece so if you want it you can look it up.

There are a bunch of plugins by Audiokinetic and their partners available and also Community offerings like AudioRain a dedicated rain synth with 60 procedurally generated presets for rain. What’s not to love about that!

There is a Wwise SDK for authoring your own plugins and a Wwise API which allows you to integrate into any engine, tool or application.

Audiokinetic do certifications that covers audio integration workflows,
mixing virtual soundscapes, working with sound triggering systems, and performance optimisation :
https://www.audiokinetic.com/learn/certifications/

Basically in Wwise you let the Launcher do all the setting up for you. You will install the Wwise binaries from here and manage your platform versions. Projects can be integrated here and if you don’t have the necessary plugins installed the Wwise Launcher will install them for you.

Integrating the MusicVisualiser project with Wwise.
This is how big the Wwise Integration packages and binaries are.
Applying…
Done!

That’s basically it for the set up of Wwise and Integration with your Project. Next up we will have a look at what this has done to the Unity Console.

Wwise in Unity

First thing we see is a bunch of errors that can be safely ignored. As we did not perform any configuration of our project in Wwise with audio files and events there was no SoundBank to generate yet.

Unity – Initial Errors can be ignored if you have not generated your SoundBank yet.

In the Unity Console we have a new tab in our editor. The Wwise Picker Tab contains all the elements of the Wwise project that have been imported with the project integration. There is also a Wwise Global Game Object in the Unity Hierarchy and all the Wwise folders in the Assets folder.

Unity Editor
The WwiseGlobal Game Object

Under the Component pull down there is a whole slew of Ak (AudioKinetic) options.

Wwise Components.
Wwise Configuration Settings.

I know there has been a lot of “show and tell” in this post but I’m going to keep going and show the process of importing the audio into the Wwise Project, creating Events, and Generating the SoundBank.

Working in Wwise

In the Wwise Project Explorer I right click on the Default Work Unit and import the audio files that were part of my project. (I’ve stripped the raw files out of my project for now and removed all the Mixer components and etc.).

Importing Audio Files into the Wwise Project.
This is what the files look like.
Right click on the file to create a new Event (which can be called in the Unity code).
Here is the event created for “Play”.
And all my “Play” events.

Finally a SoundBank is generated from which the Unity project can access the sound files through the AudioKinetic API.

Generating a SoundBank

Wwise Audio in Unity

When we go back to our Unity Editor and Refresh the Project and Generate SoudBanks we are presented with the following in the Wwise Picker. We can now access these files and and drag them on to our game objects directly. It’s that simple. Drag a sound from the Picker onto a Game Object and it automagically creates a component that is immediately accessible from within the editor.

The new audio imported into the Wwise Picker.

Below the Play_Underwater_AAS event and audio file has been added to the Sphere Game Object.

The Trigger, Actions, and Callbacks can all be configured and access through the API. In my case I easily integrated the functionality I wanted with only one line change to my attached PlayMe.cs script that we looked at above. So now instead of my audio coming from the AudioSource component referenced by my_sound the audio is played by the AKSoundEngine.PostEvent.

            //my_sound.Play();
            AkSoundEngine.PostEvent("Play_Underwater_AAS", this.gameObject);

Actually getting Wwise installed and set up and integrated with my Project was very very easy but not without bumps. It takes a very long time for packages to download and I had a bit of trouble upgrading my Wwise Launcher from an old version (it got stuck! and I had to remove it by hand and re-install). When I did have issues I got some very excellent help from AudioKinetic and after logging a case was emailed directly by a real person (which honestly was so surprising and wonderful to get that kind of support from a company when I’m on a trial license with no formal support agreement or rights).

So lets have a look at the differences in performance and package size. The first thing you notice with the Profiler below is that there is very little difference in performance but we can no longer see our audio stats as it’s been abstracted away from the Unity Engine. The Graph still shows the resources being used by Audio and the Total Audio CPU seems to be up to a third lower than the native Unity Audio statistics. It looks like it’s being clamped at just over 1.2. MB instead of regular peaks over 3 MB.

Profiler with Wwise Audio running.

The Build Report is only a couple of MB larger for the total project size:

Build Report
Uncompressed usage by category:
Textures 0.0 kb 0.0%
Meshes 0.0 kb 0.0%
Animations 0.0 kb 0.0%
Sounds 0.0 kb 0.0%
Shaders 188.0 kb 0.5%
Other Assets 7.3 kb 0.0%
Levels 38.5 kb 0.1%
Scripts 1.3 mb 3.1%
Included DLLs 3.9 mb 9.7%
File headers 13.2 kb 0.0%
Complete size 40.5 mb 100.0%

Basically a 2 MB difference! The Sounds have been extracted away as a file in the Build Report and we assume they are now part of “Other Assets” above.

I’m kinda blown away by how how little additional file size there is to the build considering the additional libraries code and available complexity that Wwise adds. There is literally a plethora of options and effects that we can play with in the Wwise package. It’s a bit like the excitement I got after the install of my first real Audio DAW. The scope is part boggling and part fantastical wonder at where we can go next. (Audio does get me unusually stimulated but that’s to be expected and tempered accordingly).

The questions I wanted to answer with this whole experiment was 1. Would including an audio middleware like Wwise make my Project more complex and difficult to manage? 2. Would the added Package make my build much larger? and 3. Would the performance of the Audio package be as good as the simple Unity Audio API? The answers are: No. No, and Yes. So I’m pretty happy with that and if the cost point of using the licensed version of Wwise is balanced out against the advantages of using it in the total cost of the Project then I would most definitely one hundred percent go for it.

Why Normalize()

I’ve been doing some work on the AI for enemy behaviours for an unreleased game Endless Elevator and have been delving into the book “Unity 2018 Artificial Intelligence Cookbook – Second Edition” by Jorge Palacios.

It uses the Normalize() function regularly to record the direction of an object in relation to another object and it got me thinking about the usefulness of this function.  You can see why something like knowing the direction of the Player could be good for an enemy AI behaviour but I wanted to investigate more deeply about how this worked and how I could use it.

One of the things I hadn’t consciously been aware of, but is obvious once you point it out, is that a Vector3 can define a location (ie. a point in space 0, 0, 0) but it can also define a direction if you have a starting position and a target position.

A good example of a Vector being used to denote a location and a direction in Unity is the Ray.
The Ray consists of two Vector3 data points. The first Vector3 is the source position the ray is taken from and the second Vector3 is the direction of the target.

When a direction Vector3 is Normalized it keeps it’s direction but it’s length (how far away one object is from another) is set to a point between 0 and 1.  In Unity they round down to 0 if the objects are very close to each other (anything under about 0.04f of a unit) anything above that gets Normalized up to 1.

As an example we can simplify a bit by limiting ourselves to a Vector2.

A Vector2 can also be either a point or a direction depending on what you want to use it for.
If it’s (3, 0) then it could be either a point at x=3, y=0, or a direction along the x axis (one dimension) with a length of 3 and a slope of 0.
If you Normalize() that example the Vector2 becomes (1, 0). It will have a length of 1 but still be pointing in the x direction.
If you add the y value (dimension) into the picture where x=3, y=3 the Normalized value becomes (0.7, 0.7). So without going into the maths of why … you can see that the axis/dimensions impact on each other to define the Normalized direction.

We can extrapolate this into three dimensions without too much difficulty but I’m not going to describe that here as it’s not that necessary to understand. We only need to grasp what this means and what sort of use you can make of it in a Unity project.

For Example…

This is a simple use case of why you might want to use Normalize().

(Note that there is also a Normalized() function where the current vector is left unchanged and a new normalized vector is returned).

I’ve created an example project where the relationship between two objects (a Cube and a Sphere) will give us a direction (using Normalize()).  The direction is applied to a transform.Translate function on a third object (a Cylinder) which now moves in the given direction.

In this script below attached to our Green Cube (the unchanging point of origin – 0, 0, 0) we define the Pink Sphere as the target.  In the script we get the difference between the position of the sphere and the Cube and Normalize() it to make a direction. It doesn’t matter how far the Sphere is from the Cube the direction stays the same.

Also to extend the example I’ve added a Ray with the same origin as the Cube (0, 0, 0) that will always point to the Sphere.  This shows that the Normalized Vector3 is the same as the direction of the Ray pointing to the Sphere.

Normalizing Direction

The public Vector3 lineDirection is the difference between the position of the Cube and the Sphere. This is the variable we will pass to the Cylinder to move it in the required direction.

using UnityEngine;
using UnityEngine.UI;

public class HowToNormalizeDirection : MonoBehaviour {

    public Vector3 lineDirection; 
    public GameObject target;   
    public Text vector_text;
    public Text norm_text;
    public Text ray_direction;
    public Ray whatsTheRay;    

    // Use this for initialization
    void Start () {
        lineDirection = new Vector3();
        whatsTheRay = new Ray();
	}
	
	// Update is called once per frame
	void Update () {
        lineDirection = target.transform.position - transform.position;  
        vector_text.text = "Vector3 target.transform.position :" + lineDirection;
        lineDirection.Normalize();  
        norm_text.text = "Vector3 Normalized() :" + lineDirection;
        whatsTheRay = new Ray(transform.position, target.transform.position);
        ray_direction.text = "Ray : " + whatsTheRay.ToString();   
    }
}

Moving the Capsule in the Same Direction

In this script below attached to the Cylinder we get the direction from the lineDirection variable above but we could also have used the inbuilt Ray.direction function to return the same result.

using UnityEngine;

public class MoveCapsule : MonoBehaviour {

    public Vector3 direction;
    public float speed;
    // Use this for initialization
    void Start () {
        direction = Vector3.zero;
        speed = 0.50f;
	}
	
	// Update is called once per frame
	void Update () {
        var script = GameObject.FindWithTag("Cube").GetComponent();   
        direction = script.lineDirection;
        //direction = script.whatsTheRay.direction;  
        transform.Translate(direction * speed * Time.deltaTime); 
	}
} 

In the Video below you can see this demonstrated. The position of the Pink Sphere is being manually manipulated using the Transform on the right. The text areas expose the vector3 values being accessed by the objects and scripts.

So what’s going on in this video amateur hour?

The three lines of text at the top of the game scene represent:

1. The Vector3 Position of the Pink Sphere.

2. The Vector3 Normalized  direction which is the relationship between the position of the Green Cube (0, 0, 0) and the Pink Sphere (x, y, z).

3. The uses of Vector3 in the Ray.  First the position of the origin and then the Direction of the Ray which is exactly analogous to the Normalized Vector3 in point 2 above.

The uses for something like this could extend to a weapon aiming system, a custom controller or be passed into a path finding routine. This is not the only option of course there are other built in methods like transform.LookAt(target) available which may accomplish your programming goal.

During the research for this post I found the following links helpful.

https://docs.unity3d.com/ScriptReference/Vector3.Normalize.html
https://docs.unity3d.com/ScriptReference/Vector3-normalized.html (which is a different but related function)
https://forum.unity.com/threads/what-is-vector3-normalize.164135/
https://answers.unity.com/questions/52881/vector-normalization-question.html
https://www.dummies.com/education/math/calculus/finding-the-unit-vector-of-a-vector/ (for why you really don’t want to know about the math or do it by hand)
https://www.mathsisfun.com/algebra/vector-unit.html (best for simple vector explanation)
https://docs.unity3d.com/ScriptReference/Ray.html
https://docs.unity3d.com/ScriptReference/Ray-ctor.html