- Home /
How do I implement orbits between two GameObjects with Physics?
I have made a script in my gravity-based spaceflight game; when the ship is aimed and launched by the player, it needs to be able to orbit around the planet. if it enters the planet's sphere of influence (shown by the misty sphere), provided it's not too close to crash, or too far away to be affected by gravity.
The following code below controls the gravity, enabling the ship (controlled by the player) to be affected by gravity within a specific range of the green planet (as determined by a child object's sphere collider). However, while the ship curves as a result of gravity, it does not orbit:
void FixedUpdate()
{
Collider[] cols = Physics.OverlapSphere(transform.position, gravRange);
List<Rigidbody> bodies = new List<Rigidbody>();
foreach (Collider c in cols)
{
Rigidbody collidingRB = c.attachedRigidbody;
if (collidingRB != null && collidingRB != rb && !bodies.Contains (collidingRB) && c.attachedRigidbody.CompareTag ("Player"))
{
bodies.Add (collidingRB);
Vector3 offset = transform.position - (c.transform.position);
Vector3 forceToApply = (offset / offset.sqrMagnitude * rb.mass);
collidingRB.AddForce (forceToApply);
transform.RotateAround(transform.position, c.transform.position, 0f);
///<summary>
///Automatically calculate Planet Mass from Gravity range, so the ship will be gradually drawn in (at the edge, gravity = 0, and increases into planet).
///To simulate gravity, I opted to change the mass of the planet, as I couldn't figure out how to change the gravitational values, and for the purposes of the game, this was considered to achieve the same end.
///</summary>
planetMass = 80 * gravRange - 140;
rb.mass = planetMass - (offset.sqrMagnitude * (Mathf.Pow(4, 2)*0.9f));
///This part was added as an attempt to control the strength of gravity to attempt to create an orbit by prevent the ship from crashing into the planet immediately. While it enabled the ship to be pulled in slightly once reaching the sphere of influence (rather than being negligible on the edges), the ship still wouldn't orbit. if (rb.mass <= 3) { rb.mass = 3; } if (rb.mass >= 50) { rb.mass = 50; } } } }
Is there a way for the ship to be able to orbit around the planet when fired into its sphere of influence (while allowing it to crash if it gets too close, or shoot off if too far away)?
Note that in my case, I used 3D physics for this game. I had issues with implementing 2D physics bodies when I implemented them, and I would need to redo the entire game to insert 2D physics.
Here is an example of what I'm aiming for. The red lines represent an example of how I would like the ship to orbit within the green sphere of influence.
$$anonymous$$aybe the ship is just going to fast to enter the orbit? I'm no expert on that but maybe a read on Orbital $$anonymous$$echanics could be helpful.
in addition, you're making a game right? So it would be wise to implement a certain angle/ speed correlation that forces the ship into a perfect orbit, without physics, as it would give the player a clearer feedback if he did it right. I'd needs to feel good, not be physically aqurat
Your answer
Follow this Question
Related Questions
How to stop sphere from clipping through cube edges aside from lowering Time.FixedDeltaTime? 1 Answer
Make objects stack in unity? 0 Answers
Player Car Runtime Scaling Issue 0 Answers
Ellipticall Orbit like Spaceflight simulator 1 Answer
How to go about filling the gaps between segmented objects connected with hinge joints? 0 Answers