- Home /
Rocket game orbit problems *URGENT*
I'm making a rocket game. I have made faux gravity and the gravity works fine. So I went on to make gravity and drag get less as you go up. Theoretically, after you have 0 drag and a little bit of gravity you should orbit. But the rocket did not orbit?!!?. Please tell me how to make the rocket orbit. :)
Here is the Code for gravity and such:
public Transform _Planet;
public float Gravity;
public Rigidbody rocket;
public Transform rocketT;
public float DistanceEarth_;
public float atmosHeight;
public bool oneOver = true;
public bool twoOver = true;
public bool threeOver = true;
public bool fourOver = true;
public bool fiveOver = true;
public bool SphereOF_influ_out = true;
// Update is called once per frame
void Update()
{
DistanceEarth_ = Vector3.Distance(rocketT.position, _Planet.position) - 246601.54f;
rocket.AddForce((_Planet.position - transform.position).normalized * Gravity);
if (DistanceEarth_ < atmosHeight / 5)
{
Gravity = 30;
rocket.drag = 2;
}
if (DistanceEarth_ > atmosHeight / 5 &&DistanceEarth_ < (atmosHeight / 5) * 2 )
{
Gravity -= Gravity / 5;
rocket.drag -= rocket.drag / 5;
oneOver = false;
}
if (DistanceEarth_ > (atmosHeight / 5) * 2 && twoOver && DistanceEarth_ < (atmosHeight / 5) * 3)
{
Gravity -= Gravity / 4;
rocket.drag -= rocket.drag / 4;
twoOver = false;
}
if (DistanceEarth_ > (atmosHeight / 5) * 3 && threeOver && DistanceEarth_ < (atmosHeight / 5) * 4)
{
Gravity -= Gravity / 3;
rocket.drag -= rocket.drag / 3;
threeOver = false;
}
if (DistanceEarth_ > (atmosHeight / 5) * 4 && fourOver && DistanceEarth_ < (atmosHeight / 5) * 5)
{
Gravity -= Gravity / 3f;
rocket.drag -= rocket.drag / 3f;
fourOver = false;
}
if (DistanceEarth_ > atmosHeight && fiveOver && DistanceEarth_ < (atmosHeight / 5) * 6)
{
rocket.drag -= rocket.drag;
Gravity -= Gravity / 3f;
fiveOver = false;
}
if (DistanceEarth_ > atmosHeight * 50)
{
Gravity -= Gravity;
SphereOF_influ_out = false;
}
}
Hey there,
first it might be helpfull to give some more information on what is happening and what you are actually axpecting to happen. ("it should orbit" is a bit vague)
Assu$$anonymous$$g that i actually understood your problem: Your current code will naturally not result in a stable orbit. To achive that you should read into the calculation of centrifugal forces to actually hit the right value for your gravity and/or speed to achive a stable orbit. Just applying some random gravity value will result in undefined behaviour.
So in my rocket game you are piloting a 3D rocket. As you go up in the atmosphere, drag and gravity go down. Drag goes down quicker than gravity. So when you get to a point where there is zero drag and a little gravity you should orbit around the earth just like real life. $$anonymous$$y desire is for this script to give me that outcome.
I still don't get why I my script does not work for this. I don't totally understand the Wikipedia article either. Just applying some random gravity value will result in undefined behaviour. I dont get why I cant just put some random gravity value either?
Can you perhaps put a more detailed description (perhaps even with a picture) of the desired and the current behaviour up? Then it might be easier to understand what your definition of "orbit" and "not working" is.
Also you want to move the rocket.AddForce
to the FixedUpdate
. Applying force in Updates and then even without time compensation will result in unforseeable and performance related behaviour...
Okay I will put .AddForce() in FixedUpdate and Here is a model I made to show what I want and what I need:
And this is why rocket science is not this simple ;) The problem here is that just adjusting gravity will not be enough. When gravity is constant you will be bound to a loop that is given by your current velocity and position. If your speed and position matches the gravity given the formula for centrifugal/petal force then you will have a circular orbit. If it does not you will get an ellipse. This is just how it works. So basically your problem is not your concept of applying gravity but the issue that you have to adjust your speed at a specific time to a specific value (again here the calculation is needed) to achive a circular orbit.
@DCordoba did a nice job i think in simplifying your script but it will as far as i can see this still contain the same problem. You cannot simply say drag is none and gravity is constant and assume that you result in a circular orbit.
Okay I think I got it. I was looking at it too simply. thank you. I will try to implement @DCordoba script changes thanks.
Answer by DCordoba · Feb 15, 2019 at 01:18 PM
your planet is rotating right? so you can resume it on a single (simplified) calculus
//about Rocket
public Rigidbody rocket;
public float DistanceEarth_;
public float rocketMass;
public Transform rocketT;
//about Planet
public Transform _Planet;
public float GravityOnSurface;
public float surfaceToCenter = 246601.54f;//distance from center to surface
public bool SphereOF_influ_out = true;
//used to check rotation of active planet
public vector3 prevForward = Vector3.zero;
void Start(){
prevForward = _Planet.forward;
}
// Update is called once per frame
void Update()
{
Vector3 totalForce = vector3.zero;
//ditance to the center? otherwise change it to measure the distance to center on your scene
DistanceEarth_ = Vector3.Distance(rocketT.position, _Planet.position) - 246601.54f;
//if your planet rotation is constant, better store it on planet script, to not do it every frame
Vector3 tangentRot = (_Planet.forward-prevForward)*Time.DeltaTime;
float fCentripetal = (tangentRot.sqrMagnitude / DistanceEarth_)*rocketMass;
float fGravity = GravityOnSurface * (surfaceToCenter/DistanceEarth_)*(surfaceToCenter/DistanceEarth_);
//multiply the forces by their respective direction vectors
//(maybe this can be optimized on some way doing all together using vectors?)
Vector3 totalForce = fCentripetal*tangentRot.normalized + fGravity * (_Planet.position - rocketT.position).normalized;
rocket.AddForce(totalForce);
}
If I did all the calculus well, this should work, (Im not sure to add rocket mass there since rigidbody also add it)
you can ignore the gravity force when is very low, to evade unnecessary calculations (`fGravity` is reduced by squared proportional distance, fCentripetal
is linear, at some point you start just rotating, at other, far point from the first, planet just can't drag you enough to be measured)
source: http://hyperphysics.phy-astr.gsu.edu/hbase/orbv.html#eg
Your answer
![](https://koobas.hobune.stream/wayback/20220612191703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Orbit controlled by virtual joystick 1 Answer
Faux Gravity help 0 Answers
How do I implement orbits between two GameObjects with Physics? 0 Answers
Issue with navmesh and object graphics 0 Answers