Optimising C# scripts in Unity 🏎

C# logoThe more I create C# scripts in Unity the more I learn about optimising those scripts. With code there are 100s of ways to solve a problem, not all of them efficient, so learning to check your codes performance seems like a must.

Things to look for

So far I’ve come across a few simple things to look out for on the projects I’ve been coding. I’ll post more as I come across them in future.

  • Anything that’s called every frame creates consistent load on the CPU, GPU or both. If what’s called is inefficient or large it will have a negative effect on frame rate. In VR this is particular bad given low frame rates can make your user physically sick. Be sure to look at what’s in the Update and FixedUpdate methods and do your best to optimise the code as much as possible.
  • Working on mobile we will almost always be maxing out the graphics capability of the device. This effects the performance of the Update method. On the other hand FixedUpdated runs in sync with Unity’s physics engine. This will make sure your physics will be the most accurate and consistent. FixedUpdate is called every “physics step”, so is regularly used for adjusting physics (rigid body) objects. Make sure you always put your physics code in FixedUpdate.
  • Avoid using GameObject.Find. It basically requires the system to cycle through all of the GameObjects in the game to find the right one, which obviously is expensive for the CPU. If you know what object we are using ahead of time, define the object at the top of the script and reference it that way. GameObject.Find is useful for testing out ideas, but that’s about it.

Adding spatial audio in VR 🎧

Part of my Night at the Museum project is adding audio descriptions to the objects on display. To do this in Unity I’m making use to the spatial audio capabilities of the Google Carboard SDK.

As with most things in Unity there’s a fairly reasoned logic to how this is done. You need three main things:

  1. An audio source. You can think of this as the media player, it is literally the source of the audio within your environment
  2. An audio clip. This is the actual audio file that will be played by your audio source. So think of this as the song that will be played by your media player
  3. An audio listener. This is effectivly your virtual ears within the virtual world. Normally this is attached to your main camera.

To give you an example of how I this works in practice. I first attached a GvrAudioListener to my main camera. This will allow my player to hear the sounds produced. Next add a GvrAudioSource to a GameObject you want to produce a sound. Now load your audio source (your media player) with an audio clip. There’s a few different supported formats, any old mp3 will work nicely. Just drag your MP3 into the audio click field of your GvrAudioSource. By default the audio will play on wake, so just hit play to enter game mode and you should hear your new audio right away.

For more information on the ins and outs of Google Cardboard Audio capabilities, check out the documentation.

Pro Tip:

If you find your audio doesn’t play, try clicking edit -> project settings -> audio and make sure the “Spatializer plugin” is set to Gvr.

More progress 👨‍💻

Managed to get everything to compile to the phone again. Also, I can move about the place now 🙂

The big issue was around the legacy Google Cardboard SDK junking up the project. Completely deleting all SDK related files, old and new, then reinstalling the new was the quickest solution.

Once that was done, recreating key items like the events system and camera using the supplied GVR SDK components made life relatively straightforward.

Big thanks to synthercat for his wise guidance.

Check out the video below.

Getting back on track 🔧

So that thing I broke, it’s getting fixed. Little by little, day by day.

So far I’ve managed to get:

  • VR working properly again (at least I think I have)
  • select objects again
  • the FPS back over 60

Unfortunately I still can’t compile to my phone but hopefully, the weekend sorts that. It’s funny how breaking this thing has been possibly the most educational part of the course so far. I’m not saying I’d make a habit of it, but it was a net positive overall.

Here’s the latest update:

Project: Night at the Museum 🏛

I’m working on a project for my studies with Udacity. The goal is to create a VR experience that demonstrates my research into a VR company. I’ll do a series of posts on this as I progress along the path to completion.

To start here’s my first crack at the initial documentation for the project:

Statement of purpose

The purpose of the "Night at the museum" application is educate the user on the HTC Vive VR solution. The experience is based on an exhibit style scene, which contains 5 "stations", each covering some aspect of the HTC Vive.

Persona

The target audience for this application is anyone interested in VR that would like to learn more about the HTC Vive VR solution. Here’s a basic persona from them:

S03e13_percival_1600x1200Name: Wade Watts
Age: 18
Role: Budding VR enthusiast  
VR Experience: Experience with google cardboard but limited beyond that
Quote: "VR is very exciting, I can't wait to learn more"
About this person: Wade has grown up with computers, game consoles, tablets and smart phones. He's a digital native that loves to escape into his devices. VR is the ultimate expression of that escape.

Concepts

I’ve started by looking at different museum and exhibit styles to select a something that fits the aesthetic I’m looking for. After looking at altogether too many museums (if that’s possible) I eventually settled on classic Greek style building with exhibits setup like sculptures on pedestals. I’m using the following images as inspiration for the design:

I like this style, it reminds me of some of my favourite museum tours, and I’ve also done some work in a previous project that shares a similar style. This should allow me to reuse some of the assets from that project, meaning I can move faster. Always important when you’re pressed for time.


Concept build

Having established a concept for the style of the experience a setup to make a concept build. Using the assets from previous work made putting something workable together relatively easy.

I added a terrain GameObject with a few mountains in the distance to give the scene a sense of depth. I dropped in a “temple” model that is a good fit for a museum and added a few items to flesh out the concept. For the actual stations I acquired an HTC Vive model from sketchfab user Eternal Realm.  I also kept an eye out for the scenes frame rate to make sure I wasn’t introducing anything at this early stage that had a big impact on performance. I still have a few more models to acquire but I’m happy with the direction the concept is heading in.

Screen Shot 2017-08-16 at 9.06.16 PM

A view from a potential starting position for the user.

Screen Shot 2017-08-16 at 9.06.29 PM

The users view approaching the entrance of the museum itself.

Screen Shot 2017-08-16 at 9.06.37 PM

A simple example of what a “station” may look like within the museum. A floating Vive headset over a stone platform.


Next steps

Now that I’ve got the basics in place the next thing on the agenda is the first round of user testing. I shouldn’t have to much trouble acquiring a suitable test subject to get things moving. I will be interesting to see how the scene changes and progresses over the course of development.

VR: Reshaping industries 🤔

As a bit of a thought experiment I’m taking a look at an industry and imagine a VR app might reshaping it.

Given my family’s always been in the construction industry (flooring), I thought it might be interesting to look into a segment of that market; medium to large scale commercial building construction.

To that end here’s a concept for an application that would help a site foreman and workers during a construction project to visualise the job to be done.

Statement of purpose

Create a virtual version of a construction site that shows the project at various stages of construction. Allowing the team to collectively understand what is to be done and how things should be at various stages. The application should also demonstrate hazards on site without exposing workers to those hazards.

Persona



Age: 30-65

Occupation: Site foreman

Name: Alex

A Quote:

"A 200 page job spec might be required reading, but no ones actually across it all. It would be great to be able to see the spec and show it to others to explain the detail"

2–3 sentences describing what motivates them:

Alex works long hours, and carries a lot of information and responsibility in their head. A key part of Alex's role is communication and having better tools to align everyone's activities on site are critical to a success project.

Staff safety is also a top priority. Giving people a clear understand of what's going on across the site keeps everyone safe.

Their experience level with VR:

Little to none

Q and A

Q: How accessible would each VR platform be to your target student in terms of price? Take into account location, age, and income.

A: The only platforms that are really accessible is Cardboard/Daydream or Gear VR style VR. A construction site can be a busy, dirty place. An expensive tethered full immersion rig really isn't practical on site.
Q: How interactive does your lesson need to be? For example, do I need to pick things up or could I get away with just looking at objects?

A: The user needs to be able to look at various objects, select them and have some details displayed to them. Movement would be required to view various scenes but does not require movement within a scene.
Q: How realistic do your visuals need to be in order to teach? For example, could I use 2D images and videos in a 3D Environment or do you need high poly 3D models.

A: images clear enough to show construction detail and highlight hazards is sufficient. I would also be useful in some cases to be able to show exploded views of constructions, but this could also be done via images or simple animations.
Q: Does my student need to feel like a participant in the experience or can they be a passive viewer? Could they be both?

A: Primarily they could be a passive viewer. The purpose of this app is to give the user a sense of what's going on, what needs to be done and what safety issues there are. There's little to no complex interaction required.
Q: Given the answers above, what are potential platforms you could use for your experience?

A: In this case I think any platform could work, providing the right conditions. For example a dedicated space would be required for a high immersion setup. Having said that it would be far more practical to use a mobile setup on a construction site.

The Daily 360 📰

So it turns out the New York Times has been doing daily 360 stories for some time now. You can view them all on their sub site, the aptly named “The Daily 360“.

Not every one of these is improved by being a VR experience, but many are. Below are three that really caught my eye.

Aftermath of a deadly Mumbai building collapse

Watching it in browser is one thing but with a VR headset on it’s almost like you’re there. given the nature of the content, the scene has a strange mix of hope and sadness to it. Being able to be “present” really makes this story feel real, personal almost. You’re there with the people involved. It’s items like this that make you see why VR is sometimes called an empathy machine.

Screen Shot 2017-08-06 at 5.54.30 PM

Aftermath of a deadly Mumbai building collapse

Paraglide Over Peru in 360

Hard to imagen how you could go wrong with a title like “Paraglide over Peru in 360”. It’s as beautiful as it sounds. One thing to be aware of though, simulator sickness. Experiences like this make you want to look about (sort of the point eh) but given the slow moving nature of the video, if your not careful it would be easy enough to make yourself sick watching this. Especially those who are susceptible to other forms of motion sickness. Having said that, as someone that once spent 45 mins sitting on the floor of a café bathroom following a bad VR session, I had no issue with this particular scene.

Screen Shot 2017-08-06 at 8.49.41 PM

Paraglide Over Peru in 360

A ride for the red planet

This one’s a bit of a mixed bag. Large chunks of this basically put you on the sidelines at the museum showcasing the concept mars vehicle. Given the vehicle is the only thing of interest in the scene, it’s not much of an advantage being in VR over standard video. The part that really works though is when the scene changes to be inside the vehicle. It gives you a sense of what it might be like to be piloting this thing. Which let’s face it is very cool.

Screen Shot 2017-08-06 at 5.59.32 PM

A ride for the red planet