Hi.
I have a Rigidbody with collider moving across a small flat area:
void FixedUpdate () {
rigidbody.AddForce(transform.forward * 5);
(...)
I have a function that detects how close to the edge the object is. Basically just a straight down raycast that looks at the distance between my Rigidbody and the RaycastHit.collider.bounds.center variable.
So my problem is, that when my character gets close to the edge, I check if the distance is over 1.0f:
if(distance_to_center > 1.0f) {
float new_rotation = Mathf.Ceil(transform.eulerAngles.y + 180.0f);
transform.rotation = Quaternion.Euler(0, new_rotation, 0);
}
My problem is now, that FixedUpdate() updates too fast compared to the velocity of my Rigidbody. So one update goes by, and my conditions is fulfilled, the Rigidbody turns 180 degrees and starts moving the other direction. Then another updates arrives, and notices that my character has not yet had time to move out of the way, and then updates it again to go the other way.
Resulting in my rigidbody just going into spasm and moves back and forward constantly.
What do I do to avoid this? I have had this issue with quite many things.
Thanks.