- Home /
,Rotate a gameobject around another while being attracted by its gravity
I have a game object ( satellite ) which is it is being attracted by another object(Sun). the satellite rotates (clockwise ) by holding mouse click, and on space key press(Jump), a force pushes the satellite against the gravity. if satellite rotates around the sun whiling jumping, releasing the mouse button causes the satellite to rotate a bit back.
how to fix this?
is there any better way to achieve this gravitational rotation? Tnx in advance.
void FixedUpdate()
{
if (Input.GetMouseButton(0))
{
transform.RotateAround(center.transform.position, Vector3.back, 50 * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space))
{
direction= transform.position - center.transform.position;
GetComponent<Rigidbody2D>().AddForce(direction* 100 * Time.deltaTime);
}
axis = center.transform.position - transform.position;
GetComponent<Rigidbody2D>().AddForce(axis * 100 * Time.deltaTime, ForceMode2D.Force);
}
,
I think the force of the last few frames continue to affect the current frame. So if you release your mouse, the object still has force from before applied to it, which is causing the extra bounce. Try using either Force$$anonymous$$ode2D.Impulse or resetting the Rigidbody.velocity
I checked either setting velocity to 0 or using Force$$anonymous$$ode2d.Impulse on mousebuttonUp , but the problem still remains. maybe I'm doing it in a wrong way
it's calculated is below
axis = center.transform.position - transform.position ;
So you're constantly setting axis in update?
Answer by xxmariofer · Jan 15, 2019 at 10:25 AM
Im not sure if your question is how to make the satellite once you stop holding go straightforward to the sun, if thats the case you can set satellite velocity to vector3.zero when Input.GetMouseButtonUp(0) for making sure it doesnt continue previouse speed.
also save the rigidbody in a var in the start since you are accessing in fixedupdate and will cause performace issues.