I am building a space simulator.
I have made a FloatingOrigin script based on the one from the wiki, but I added a velocity reset too.
Basically, I have a scene with two container objects at the root : Userspace and Universe.
The Spaceship and the FirstPersonController are both direct children of the Userspace.
The Universe contains all the other objects (planets, other spaceships, etc).
- Userspace
---- FirstPersonController
---- Spaceship - Universe
---- Planet
---- Sun
---- SpaceStation
When controlling the spaceship, the FirstPersonController’s RigidBody is Kinematic and has a script to follow the spaceship :
void LateUpdate() {
character.transform.position = spaceship.transform.position;
}
To make the spaceship accelerate, I check for WASD inputs in FixedUpdate() on a script attached to the spaceship.
Exemple for forward :
if (Input.GetKey (KeyCode.W)) {
rigidbody.AddRelativeForce(0, 0, 10000f);
}
Here everything works as expected, except for a shaking effect due to floating point precision.
To fix that I have made a FloatingOrigin system which resets the spaceship to the origin and moves the Universe, both position and velocity.
My FloatingOrigin script (shortened) :
public float maxDistanceFromOrigin = 1000f;
public float maxVelocity = 1000f;
public GameObject universe;
public GameObject spaceship;
void LateUpdate() {
if (spaceship.rigidbody.velocity.sqrMagnitude > maxVelocity) {
universe.rigidbody.velocity -= spaceship.rigidbody.velocity;
spaceship.rigidbody.velocity = Vector3.zero;
}
if (spaceship.transform.position.magnitude > maxDistanceFromOrigin) {
universe.transform.position -= spaceship.transform.position;
spaceship.transform.position = Vector3.zero;
}
}
Now, the actual problem…
When accelerating very fast and very far, and the FloatingOrigin script already reset the position/velocity a few times, at some point, with the forward key input pressed, the spaceship starts rotating on it’s X axis for no reason.
When trying to accelerate sideways, it actually rotates on it’s Y axis.
It seams to be happening on a very random base, but mainly (or maybe only) when the FPS drops much lower than 20 FPS, which is about half my Fixed Timestep of 0.02 I’ve set in the project settings.
It seams to be caused by the velocity reset of the spaceship, doesn’t matter if the position is also being reset or not.
When I disable this FloatingOrigin script, everything works fine (except for that shaking effects of the floating point precision of course), it never rotates no matter how much I accelerate or how far I go from the origin.
Anyone has a clue on this issue ? Is it a bug in Unity or in the way I’m using it ?
Thanks for the help !
Edit : FirstPersonController does not use the built-in scripts. It’s homemade and it`s movement is disabled when attached to (controlling) the spaceship.
Also, this FirstPersonController does not collide with the spaceship as it’s position is forced in the middle of the spaceship (see script above), not touching the walls.
The spaceship used for this test is simply 5 stretched cubes surrounding the FirstPersonController.