Answer the question
In order to leave comments, you need to log in
How to move an object in a circle using rigidbody?
Hello!
There was such problem: I do particle simulation, and it is necessary for me that on mouse click the particle if it is in the set radius, began to rotate around the mouse pointer. I found a solution where movement happens by changing transform.position:
if (Input.GetMouseButton (1)) {
timer += Time.deltaTime;
transform.position = new Vector3 (2*Mathf.Cos(Mathf.Deg2Rad*timer), 2*Mathf.Sin(Mathf.Deg2Rad*timer), 0);
}
Answer the question
In order to leave comments, you need to log in
First drag the particle towards the mouse, and when it is close enough, drag it along the tangent of the desired orbit.
var toMouse = mousePosition - rigidbody.position;
if (toMouse.sqrMagnitude > sqrRadius)
{
rigidbody.AddForce(toMouse);
}
else
{
var velocity = rigidbody.velocity;
Vector3.OrthoNormalize(ref toMouse, ref velocity);
rigidbody.AddForce(velocity);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question