ive looked at the answers on these forums however there must be an easier way, im moving an object in a circle and am simply trying to rotate the object on the z axis to follow the circular motion. (a car traveling in circle) i tried subtracting the transform.position from the target position, to return a vector, however am unsure how to add that vector simply to the z axis of the object because the difference vector has values for x and y and 0 value for z. i bet the answer is really simple because the (local) rotation should remain the same the entire time its rotating around the circle, right?
public Transform pivotPoint;
public float speed = 1;
public float circumference = 4;
public float count = 0;
Vector3 previousPosition;
void Update()
{
count += Time.deltaTime * speed;
var x = Mathf.Cos(count) * circumference;
var y = Mathf.Sin(count) * circumference;
Vector3 targetPosition = new Vector3(x, y, 0);
transform.position = targetPosition + pivotPoint.position;
}
That's the best option. You can even set it up dynamically in your code,
– glantucanawesome thanks! there will be multiple cars moving at different speeds, in opposite directions, on different sized circular paths. rotateAround works, however the initial rotation upon entering the path doesn't change. the answer i accepted seems to work best in this situation because when a car enters the circular path it's local rotation snaps to the forward direction of movement along the path no matter what its previous rotation was, plus im able to change the speed and circumference of each individual car's path. However, all three answers work, thanks very much! Appreciate the help :)
– RomanLedactually i could use the first code only once when a car first connects to a circular path, setting the initial circumference of the path and rotation of the car only one time, then use either rotateAround or parent the car to the pivot point game object, to move it along the path and the car will keep its correct rotation. then i wont be creating new x and y variables every update, probably a good choice.
– RomanLedIt is not very logical to convert TimeManager to DateTime: DateTime dt = new DateTime(2012, 01, 01); TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0); dt = dt + ts; Also it's not good practice to name your variable exactly the same as it's type: public TimeManager TimeManager; It should be: public TimeManager timeManager;
– jmgek