how to apply orbit to the player

he problem here is that I am able to apply the orbit to the planets in my game, but when the player enters the planet they start bouncing,(The reason seems to be that the planet keeps moving and the player takes a moment to follow.) I need a way to apply the orbit to the player when you enter a planet.


public class Orbit : MonoBehaviour
    public Transform center;

    public float radius;
    public float radiusSpeed;
    public float rotationSpeed;

    private Vector3 axis;
    private Vector3 desiredPosition;

    // Start is called before the first frame update
    void Start()
        transform.position = (transform.position - center.position).normalized * radius + center.position;
        axis = Vector3.up;

    // Update is called once per frame
    void Update()
        transform.RotateAround(center.position, axis, rotationSpeed * Time.deltaTime);
        desiredPosition = (transform.position - center.position).normalized * radius + center.position;
        transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);


public class GravityCtrl : MonoBehaviour
    public GravityOrbit Gravity;
    public Rigidbody Rb;

    public float RotationSpeed = 1000;

    void Start()
        Rb = GetComponent<Rigidbody>();

    void FixedUpdate()
        if (Gravity)
            Vector3 gravityUp =;

            gravityUp = (transform.position - Gravity.transform.position).normalized;

            Vector3 localUp = transform.up;

            Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * transform.rotation;
            Rb.GetComponent<Rigidbody>().rotation = targetRotation;

            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, RotationSpeed * Time.deltaTime);
            Rb.GetComponent<Rigidbody>().AddForce((-gravityUp * Gravity.Gravity) * Rb.mass);

one simple way to do it would be to set the current planet as the parent of the player in the hierarchy.

@keroltarr This almost fixes the problem, but if I want to switch to another planet, the orbit is the same as the other planet, so the player starts flying away

Found the solution, just write
Rb.transform.parent = Gravity.transform;
put it in the Fixed update, so when the player enter a new gravity you become a child of the planet gravity