Creating A Custom Ellipse In Unity, Part 2

In this post following Part 1, I will modify my ellipse generation code to make it 3D, and more suitable for planetary orbits. The change is small, but not as straight-forward as you might expect.

Today’s Objective: Make the Ellipse generation able to make 3D ellipses.

Currently:

Using a Vector2 variable for the X and Y scale,

calculating the X and Y position, but setting the Z to always be zero,

results in a Ellipse that can only be 2D.

To make it 3D:

This is surprisingly not as simple as just adding a Z value to the calculation, because:

Using “float z = Mathf.Sin(_angle) * xyzScale.z” only controls the horizontal points, and using “float z = Mathf.Cos(_angle) * xyzScale.z” only controls the vertical points. The last method, Tan (“float z = Mathf.Tan(_angle) * xyzScale.z”) creates:

Separately, none of these are perfect.

So we need to combine them. Specifically the Sin and Cos results, because we want to control all the points in the ellipse smoothly.

Add the Sin result to the Cos result

You may have noticed that the above code doesn’t use the existing xyScale variable.

I could turn it into a Vector3 and use that Z value, but I want control over both the horizontal and vertical points on the Ellipse combined, as that allows more control over the “rotation” of the Ellipse.

So I use a Vector2 for the X and Y scale combined (xyScale) and a separate Vector2 for the 2 values I want to use in the Z calculation (zScale).

This results in a 3D ellipse which can be scaled and rotated in any way I want, to fit my planetary orbit needs.

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer since 8yo (18 years). Currently looking for games industry employment