0
\$\begingroup\$

TL;DR: My ship floats and jitters when it lands on a sphere.

To elaborate on the title: I'm experimenting with a 3D space game. I have created a sphere, which in turn has a larger sphere overlapping it, within which, gravity will take place. However, once my player's ship (currently using an arrowhead) lands on the planet, it doesn't actually make contact with the surface of the sphere, and then the ship begins to rotate erratically and jitter. Code below.

private void OnTriggerStay(Collider other)
{
    if (other.CompareTag("SoI"))
    {
    inSoI = true;
    }
}

This trigger checks to see if the ship is in the larger overlapping sphere. I have another identical block for OnTriggerExit which sets inSoI to false. I also have a OnCollisionEnter which sets another variable, landed to true, and a OnCollisionExit which sets it to false.

private void FixedUpdate()
{
    if (inSoI && !landed)
        {
            gravityVector = (planet.transform.position - transform.position);
            rb.AddForce(gravityVector.normalized * planetrb.mass * rb.mass / gravityVector.sqrMagnitude);
        }
}

Simply, if in the larger sphere, then accelerate towards the planet. I tried disabling gravity if in contact with the sphere. It removed a lot of the jitter, however it still floats, and it still rotates side to side. The sphere by the way, is the standard Unity sphere, scaled 10000x in every axis. I don't need code (though it would help), any solution is fine, even if it means abandoning the project.

\$\endgroup\$
  • \$\begingroup\$ The 10000x might be part of your problem. At that range from the origin, floating point positions have a resolution of about half a millimeter, which may be coarse enough to observe jerkiness or vibration at the scales you're using.This imprecision could easily be even greater if your sphere is itself a long way from the origin. Have you tried arranging your scene so the interaction happens close to the origin? \$\endgroup\$ – DMGregory Apr 17 '18 at 1:38
  • \$\begingroup\$ Ah, thank you, this got my mind going. Turns out using an isosphere with a large size at 1x scale then using a mesh collider solved it. I made the isosphere in blender. Turns out is also has a few other pros. Thanks \$\endgroup\$ – Devintage Apr 17 '18 at 15:07
1
\$\begingroup\$

Can you describe, how did you set up the collision system? In which script are you checking your OnCollisionEnter and OnTriggerEnter? Which of your colliders are set to isTrigger? If the collider on your ship has the isTrigger checked, it does not fire the OnCollisionEnter on the inner sphere, hence, you never going to have your landed true.

If you have your the collder on your ship NOT set to isTrigger, then it is going to fire your OnCollisionEnter, when it has a RigidBody on and collides with another collider, which is not a trigger.

This should clear up things: https://docs.unity3d.com/Manual/CollidersOverview.html

I tried out a simple project, with a plane, and 10000x scaled up sphere, and I only got the jitter with the sphere, so it must be behind the unwanted behaviour of your ship.

\$\endgroup\$
  • \$\begingroup\$ I decided to use a static collider for my planet and just use a public variable for the mass, this also means I'm not limited by the max mass a rigidbody can have. I did some debugging and noticed that landed was never set to true, with or without a rigidbody, and the player is non-kinematic so the collision detection should work. Any idea why this might be happening? \$\endgroup\$ – Devintage Apr 17 '18 at 15:17
  • \$\begingroup\$ Is your the collider on your player set to isTrigger? \$\endgroup\$ – formatc2013 Apr 17 '18 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.