- Home /
The question is answered, right answer was accepted
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.
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);
}
}
}
Answer by Tibllec · Mar 10 at 04:15 AM
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
Answer by keroltarr · Mar 09 at 06:07 PM
one simple way to do it would be to set the current planet as the parent of the player in the hierarchy.
@keroltarr If i set the player as child the orbit script overwrites his movement, so the player can't use WASD