- Home /
move a 2d item in a circle around a fixed point
I have a simple problem for which I can't appear to find any answer (maybe it's just too simple??)
I have a GameObject in a 2D environment which I want to move, in a circular motion, around a given point. Let's say, for example, point Vector3(0,0,0) at a given radius (let's say 1!) at a given speed -to give a "running in a circle", or "running around the clock face" effect. I don't need to rotate the item as it's circularly symmetrical, I just need to update the position around the given axis.
Answer by Pleng · Apr 02, 2016 at 07:04 AM
Answer, adapted from https://www.reddit.com/r/Unity2D/comments/34qm8v/how_to_move_an_object_in_a_circular_pattern/
public class Target : MonoBehaviour
{
private float RotateSpeed = 5f;
private float Radius = 0.1f;
private Vector2 _centre;
private float _angle;
private void Start()
{
_centre = transform.position;
}
private void Update()
{
_angle += RotateSpeed * Time.deltaTime;
var offset = new Vector2(Mathf.Sin(_angle), Mathf.Cos(_angle)) * Radius;
transform.position = _centre + offset;
}
}
I guess usually it's just "void Start()" without the private
I have a very similar use case to the OP, and this worked perfect for me! I just changed the Radius and RotateSpeed as needed. Thank you kindly!
Answer by Inok · Apr 01, 2016 at 11:33 AM
Hope this video give you right solution: Unity3D - Circular Motion
not really. It's orbiting 3D objects. Which is not what the question asked