==== Procedural (Script-based) Sun-Earth-Moon Model Creation ====
++++ Click here to see detailed step-by-step procedure to create a Sun-Earth-Moon model |
Remember that in Unity, the Y-axis is chosen to be the vertical axis unlike the astronomical convention, Blender, and even Unreal Engine.
The following C# code is to implement a rotation of a GameObject (e.g., planet). Just attach this script to an object and set two parameters (rotation w.r.t and speed).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public Transform target;
public int speed;
// Update is called once per frame
void Update()
{
transform.RotateAround(target.transform.position, target.transform.up, speed * Time.deltaTime);
}
}
This example creates a model of Sun-Earth-Moon with realistic surface textures and rotational/orbital motions.
- Create a new project from Unity Hub (Choose the Universal 3D Core [Not the one with Build-in Render Pipeline])
-
- Configure Build parameters: File -> Build Settings: Check if the platform is correct.
- Configure Project settings: Edit -> Project Settings;
* import 'TextMesh Pro' if you plan to use texts
* import 'XR Plugin' if you are building a VR model
- Import relevant assets: Window -> Package Manager, select 'My Assests' and import the following assets (these assets can be purchased for free in Unity Asset Store)
* Milky Way Skybox
* Planets of the Solar System 3D
* Simpler Kepler Orbits
- Create a folder to store C# scripts: In the Project > Assests, right-click on the background and create a folder named "Scripts".
- Simulation background setting: Click the "main camera", then Component > Rendering > Skybox. Then choose the Milky Way skybox in the "Custrom Skybox" panel.
-
- : Since objects in this example are automatically created through the script "JPLOrbitLoader.cs", we need to prepare a container and attach this script to the container.
- Create an empty, "SEM Container" and make it inactive (click the check mark off from the inspector panel).
- Create an empty under SEM_Container, "BodyTemplate", with two child objects: "Velocity" empty and "View" sphere mesh.
- Create an empty under SEM_Container, "ScreenSpaceOrbitLineTemplate".
- Attach "JPLOrbitLoader.cs" to SEM_Container and set the followings:
- set G-constant (6.67E-11)
- Scale_per_diameter (0.1)
- Body Template = 'BodyTemplate'
- Jason Data =
- Attch "TimeController.cs" and set default date/time
- Attach "ScreenSpaceOrbitsDisplay" and set
- Target Camera -> Main Camera
- Line Template -> "ScreenSpaceOrbitLineTemplate"
- Cam Distance = 0.7
- Attach "KeplerOrbitMover.cs" to "BodyTemplate" and set
- Velocity Handle = "Velocity" <-- the empty under BodyTemplate
- Time Scale = 1
- Lock Orbit Editing = True
- Attach "KeplerOrbitLineDisplay" to "BodyTemplate" and set
- Line Renderer Reference = "ScreenSpaceOrbitLineTemplate"
- Select desired Gizmo options
- Create UI canvas and panel with Input fields for YYYY/MM/DD HH:MM:SS, buttons (reset and set_time_now), and timescale input field and slider.
- Attach "TimeControlPanel.cs" to panel and set corresponding items to the UI objects.
- Check the Canvas Scaler setting: Resolution = 1280x1024
- Click "Play" and you will see an animated Solar System objects showing orbital motions.
- Drag and drop 'Rotate.cs' on Sun Empty: set target (Sun Empty) and speed.
Future Improvements:
- Add "Rotate.cs" to a certain script so that we can set the rotation of each object (with correct tilt of the rotation axis)
- Add realistic texture material for each object.
- Add additional camera views
- top-view camera
- Earth-following camera (same orbital speed but slightly displace from the Earth so that one can see the shadowing of the Earth or Moon (i.e., eclipses)).
- Add shadow cones to see the relationship among Sun, Earth, and Moon's position and shadows
- Mars-tracking camera (to demonstrate the retrograde motion of Mars)
- Moon-tracking camera (to see the phase change and libration)
{{ tech:tutorials:earth_moon_simulation.mp4 }}
This is a screen recording of the rendered model from the above procedure.
And this is the link to [[{{ tech:tutorials:sun_earth_moon.unitypackage |the Unity package file for this model}}.