- Home /
Smooth transition for RotateAround
Hi everyone, i am making a 2D game where the player can fly through space. When he gets near a planet he is supposed to get pulled towards the planet and at the same time spin around him.
I solved this by disabling the standart unity gravity and add a Pointeffector2D to the planet. This PointEffector gets activated when the Player enters a Collider placed around the planet. Then a script with the RotateAround function gets activated.
public class PlanetGravity : MonoBehaviour {
public float speed;
public Transform target;
private Vector3 zAxis = new Vector3(0, 0, 1);
void FixedUpdate()
{
transform.RotateAround(target.position, zAxis, speed);
}
}
That works pretty good but I have the problem that the player instantly starts rotating from the position he is at the moment. Like that the player is performing like a weird corner.
I want the player to get in the orbit smoothly but i have got no idea how to get a smooth transition. I tried to use a delay but then the orbiting starts in some positions to late.
I would be very if someone could come up with an idea or code.
Answer by Hamburgert · Jan 28, 2018 at 08:17 AM
You would need to track the current speed/momentum of the craft. See my answer here, which may give you some ideas:
Thanks for your awnser @Hamburgert , but what am I supposed to do with the current speed of the player? The speed in space and while orbiting is the same.
Speed is not just a number of "distance per second". Speed also has a direction in space. Changing direction while maintaining the same "distance per second" is still acceleration, since you are changing speed in the worlds X-direction, Y-direction and Z direction individually.
A spacecraft in orbit is constantly changing its direction while under the influence of gravity. Weightlessness is simply the sensation of being accelerated in perfect synch with your immediate surroundings (spaceship hull, the contained atmosphere, every single part of your body). Speed is always relative to something, and in Unity3d it's usually relative to world space, or local space via parenting.
I just realized that I'm not really helping you use "RotateAround" which is what you're asking. I guess what I'm saying is that RotateAround may not be the best choice for your situation, I$$anonymous$$O.
What am I supposed to use ins$$anonymous$$d? I found no other possibility to get this done. I would be glad if you have a different opportunity to get this working :D