Unity Trigonometric Function Visualization

Contra
2019-07-10
Unity Trigonometric Function Visualization

The [Coding Druid] series is my cross-platform programming practice notes. Each episode focuses on a theme (math, physics, electronics, graphics, sound...), implemented in several programming languages. Leveling up warrior, mage, healer, and rogue simultaneously — tanking, DPSing, healing, and escaping all at once.


Coding Druid - Math Chapter - Trigonometric Function Visualization: Unity Implementation

sine-unity-croped


In the previous three sections, we visualized trigonometric functions (sine and unit circle) using MaxMSP, JavaScript, and Python:

trig-max-visualization.gif

[Coding Druid - Math Chapter] MaxMSP Trigonometric Function Visualization


sine-js-croped.gif

[Coding Druid - Math Chapter] JavaScript (React) Trigonometric Function Visualization


sine python effect

[Coding Druid - Math Chapter] Python Trigonometric Function Visualization


This time we'll implement it in Unity.


The previous Python version used the data visualization library Matplotlib. For Unity, we won't use such a library — we'll draw directly using trig function algorithms.

First, let's get a dot moving along a Sine function curve.

Place a Sphere in the scene, increment its position.x uniformly, and set position.y = sin(position.x).

Vector3 posSine = sphere.localPosition; posSine.x += speed; posSine.y = Mathf.Sin(posSine.x); sphere.localPosition = posSine;

sine-unity-sineball

Next, draw a sphere moving along the unit circle.

Note: set the unit circle radius to 1 and the Sine period length to 2Pi, so the unit circle and Sine share the same coordinate system.

Circle drawing formula:

sphere's x = cos(t); sphere's y = sin(t);

Here, t is actually the Sine's x from the previous step. Then shift the circle center to the left so the unit circle's starting point aligns with Sine's starting point.

Vector3 posCircle = posSine; posCircle.x = Mathf.Cos(posSine.x) - 1f; posCircle.y = Mathf.Sin(posSine.x); circleDot.localPosition = posCircle;

sine-unity-circleball

Combined together:

sine-unity-sinecircleball

In previous sections, we manually drew the path trajectories for clarity, like in the Python version:

sine python effect

In Unity, let's take a shortcut — just add Trail Renderer to both spheres for automatic trails.

sine-unity-trail0

The default trail is a bit... gloriously chunky...

Let's fix that. Search for trail in the Assets Store and find a nice free trail effect:

unity-trail

After installation, pick a material that catches your eye:

sine-unity-trail-material

Attach the material to the Trail.

Final result:

sine-unity-croped

Note: In the published source code, I added the Trail Renderer to an empty GameObject that moves with the sphere, rather than directly on the sphere. No special reason — just a Unity newcomer doing practice.

Reference: Building a Graph Visualizing Math



Talk is cheap. Show me the code!

The code for this and most of the [Coding Druid] series is open-sourced here: https://github.com/avantcontra/coding-druid


Follow us on social media: code2art

Community resources & courses: https://ghc.h5.xeknow.com/s/hzkMX

code2art Intelligence Center (membership): https://ghc.h5.xeknow.com/s/2BCFuJ

Cheers🍻

Contra