I'll give you code for moving it in a circle relative to its starting position. This code works by using sine and cosine to find points on a circle.
You could try to get by with the tangent and moving only the incremental position, but I would worry that varying Update()
times would lead to integration errors and would eventually cause you to spiral out of position.
Make variables for how fast you want it to spin in Hertz and how large of a circle you want. Cache the starting position on Start()
and then update the position in Update()
:
using UnityEngine;
public class Rotator : MonoBehaviour
{
public GameObject bg;
public float rotationRateHz = 0.5f;
public float rotationRadius = 1.0f;
private Vector3 startingPosition;
private float phi;
void Start()
{
startingPosition = bg.transform.position;
}
void Update()
{
float omega = rotationRateHz * 2.0f * Mathf.PI;
phi += omega * Time.deltaTime;
float relativeX = rotationRadius * Mathf.Cos(phi);
float relativeY = rotationRadius * Mathf.Sin(phi);
Vector3 nextPosition = startingPosition;
nextPosition.x += relativeX;
nextPosition.y += relativeY;
bg.transform.position = nextPosition;
}
}