I’e been working on making a type of planetary gravity in my 2D game. I’ve made a center of gravity, and attached this script to the RigidBody2D that’s supposed to be attracted to this center. I’ve removed the ability for this RididBody2D to use the natural gravity from the Physics Engine.
//The gravity centre
public Transform ring;
public float gravityStrength;
Rigidbody2D myRB;
void Start()
{
myRB = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
attract();
}
void attract()
{
//Pulling the object down
Vector3 downDirection = (transform.position - ring.position).normalized;
myRB.AddForce(downDirection * gravityStrength);
Debug.Log(myRB.velocity);
//Keeping the object upright
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, downDirection) * transform.rotation;
transform.rotation = targetRotation;
}
However, for some reason, the object doesn’t seem to accelerate when it falls towards this centre of gravity (which is also slow for some reason), which isn’t very realistic for gravity. I want to fix this acceleration issue, but I can’t even debug the velocity of the rigidbody. It return 0 all the time. Any idea why?