- Home /
Creating Circular motion to Rigidbody using AddForce
Hi All!
I'm trying to add a bit of random drift to my rigidbody player controller to make my game project a little more challenging but for the life of me cannot figure out how to make a rigidbody move in a circular pattern just with addForce or even constantForce.
The closest I can get is movement that looks like a "3." It does half a circle correctly but instead of continuing to do the other half of the circle, the object repeats the same 180 degree arc. I'm sure this is probably something really simple but I cannot figure it out for the life of me. So if anyone has a solution using addforce or constantForce and wish to share I would be most happy. It'll spare me from pulling out the few hairs I have left.
My really simple code for the motion test is below.
public class MoveInCircle : MonoBehaviour
{
//Make sure you attach a Rigidbody in the Inspector of this GameObject
Rigidbody rb;
public float velocity_multiplier;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
rb = GetComponent<Rigidbody>();
//velocity_multiplier = 2.0f; //multiplier value to make circular motion wider CURRENTLY NOT USING
}
void FixedUpdate()
{
Vector3 vel = new Vector3(rb.velocity.z, 0, -rb.velocity.x); //getting vector perpendicular to current vector
rb.AddForce(Vector3.forward + vel, ForceMode.Force); //moving rigid body forward and then adding in perp
}
}