Unity 1: Hello

David Joiner, Kean University

Welcome to the first in a series of blog posts about my experiences using Unity as a modeling and visualization tool. The series will walk users through creating scientifically appropriate models, simulations, and visualizations in Unity Game Engine, driven by my experiences using modeling in Unity for outreach and recruiting activities with high school students and undergrads.

This post will cover a “hello world” style program for modelling in Unity, and will introduce the reader to creating a program in the unity interface, adding a game object to the scene, and using a script to control the game object’s position. It is written assuming an audience new to Unity and not experienced at programming.

Today’s post is going to be my “Hello World” for modeling in Unity. Regardless of what modeling tool, language, environment, or interface I am using, the first example I always create is the simple harmonic oscillator. The problem is a classic one in physics education, consider a mass on a spring moved some small amount x from equilibrium. There is a restoring force on the mass that is greater if the distance from equilibrium is greater, and greater if the spring is stiffer (as represented by a spring constant k). The restoring force F=-kx can be combined with Newton’s 2nd Law F=ma to produce an equation of motion a = -(k/m)x.

We will implement this, using a Leapfrog method solution, inside of a Unity scene, where a GameObject is scripted to follow the path dictated by the equation of motion.

The full walkthrough and downloadable examples can be found at https://joinerda.github.io/Hello-Unity/.

Additional blogs will focus on issues in using Unity for scientifically appropriate models in an efficient way. Key themes will be: the double/float duality when trying to use Unity for meaningful work – as a game engine Unity’s game object’s internal variables and libraries are all float, but meaningful calculation should be done in double precision; where to place calculation in Unity’s runtime loop: Update, FixedUpdate, and threading in C#/Unity; importing DLLs in other languages to extend Unity’s capabilities and offload calculations to optimized compiled code; working with the Unity UI; and building 3D visualization into simulations.

Unity 2 post 

Leave a Reply

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