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.
ORBIT
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);
}
}
Gravity
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 = Vector3.zero;
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);
}
}
}