Continuing my rewind for Unsettled World:
Time to give some detail on the foundations of my game environment. The planet I've created and how it mechanically works within Unity.
Here is a 2d view of the editor interface with the "canvas" highlighted sitting at 0,0,0. The planet object has a 30000m radius and it is sitting @ 0,-30000,0.
The object parentage looks like this. Generic GameObject "PlanetHolder", non-rendered mesh w/collider "Planet" (used by the procedural terrain system mostly), followed by the camera, player, water sphere and the actually visible surface mesh. Now, this arrangement may change somewhat, I may figure out a way to combine the "Planet" with the visible surface, but it is how it is for now. And it works.
The PlanetController script is responsible for keeping the player and camera close to Origin, 0,0,0. This is necessary because once your player/camera cross the 10000m distance boundary the underlying floating point mathematics loses enough precision that your meshes begin jumping around the screen. The PlanetController script contains this logic:
public class PlanetController : MonoBehaviour {
public GameObject Planet;
public GameObject Player;
Rigidbody rb;
void Start()
{
rb = Player.GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (rb.velocity.magnitude < 1)
{
Planet.transform.localRotation = Quaternion.FromToRotation((Player.transform.localPosition - Planet.transform.localPosition).normalized, Vector3.up);
}
}
}
Keep in mind I omitted a lot of extra stuff from that code, for example, it doesn't actually run EVERY FixedUpdate, but as a timed event inside the fixed update, every few seconds or so.
Also Important to note, I still have a minor amount of vertical jitter that is caused by the planet sitting at 0,-30000,0. I am actively working at ways to reduce this, in fact, as I type I'm having more ideas on how to fix the issue.. So keep posted.
^Solved and NOT related to the size/positioning within the 3D space, see comment below.
The visual look of the planet(Before the procedural code kicks off and repaints it with textures/objects). Is a split up version of the planet mesh(will be split up MUCH more than this eventually).. And the water is a simple sphere object(also broken up and non-visible sections removed) with a water texture.
My Gravity "system" is also very simple(Just a script, like this one, attached to the objects that need gravity applied to them):
public class GravControl : MonoBehaviour
{
public GameObject planet;
private Rigidbody rb;
public Vector3 upVector;
void Start()
{
if (planet == null)
{
planet = GameObject.Find("Planet");
}
rb = GetComponent<Rigidbody>();
Vector3 upVector = (transform.position - planet.transform.position);
transform.rotation = Quaternion.FromToRotation(transform.up, upVector) * transform.rotation;
}
void FixedUpdate()
{
upVector = (transform.position - planet.transform.position).normalized;
rb.AddForce(-upVector * 9.81f, ForceMode.Acceleration);
}
}
I may incorporate this into a "controller" type script that isn't attached to every object at some point for simplicity, but for now, it works just fine.
So that, just a few pics and sentences, well summarizes at least 2 or 3 solid weeks of 8+ hour days... hahaha!
As it turns out, the vertical jitter I thought I still had was caused by the fact I was continuously adding the acceleration force in my gravity script while the player character was already on the ground.
Solves the last of the jitters. "bool isGrounded" is provided by GetComponent<> to the character controller script. It maintains such data for numerous and hopefully obvious reasons.
The funny thing is, this was already included in previous versions of this code.. Gotta keep an eye on what I scrap when trying to fix other things or simplify.