Physics for ball on an incline: rolling up a hill if half way up
Ok I'm currently trying to program proper gravity physics on a ball.
Currently (in semi pseudo code here):
//in FixedUpdate()
if (isGrounded)
{
NetForce.x = mgForce * Math.Sin(A) * Math.Cos(A);
NetForce.y = mgForce * Math.Sin(A) * Math.Cos(A);
}
else
NetForce = mgForce
Debug.Log("Angle: " + A + " Force: " + NetForce;
player.AddForce(NetForce, ForceMode.Acceleration);
//in OnCollisionStay()
//using an int for now
angle = (int) Vector3.Angle(Vector.up, collision.contacts[0].normal);
Vector3 cross;
cross = Vector3.Cross(Vector3.up, collision.contacts[0].normal);
if (cross.z < 0)
angle = -angle;
For the most part this works, but if the ball is travelling up an incline (from external force or it's previous force of rolling down a hill) the ball will travel up.
The Log tells me at the perfect half way point of the ramp, the angle gradually becomes a reverse angle.
Is this because of the normal? I'm using a unity cube for the ramp to test and write this script with no bump map.I've attempted writing this code with ray casting instead but i only ever got a 90 degree back, should I use this instead if the collision normal is causing the problem?
Am I doing this completely wrong? is there a better way of scripting physics for a ball in Unity? As far as I could tell, the default gravity is a constant velocity downwards and doesn't act real on an incline whatsoever.