set rotation of object moving in circle

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;
}

3 Answers

3

Depending on the complexity of what you are trying to set up, theres always the option of making your car a child of a game object, offsetting its position, and rotating the root parent. The car will appear to be moving in a circle and you wont have to worry about the direction it should be facing.

That's the best option. You can even set it up dynamically in your code,

–

awesome 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 :)

–

actually 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.

–

It 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;

–

You can set either transform.up or transform.right to that targetPosition - transform.position vector depending on how your object is oriented.

cool thanks, this works with the original code too.

–

didn’t know rotateAround was a thing… this code seems to kinda work, however the front of the car is facing the center, rather than the side. any suggestions?

 transform.RotateAround(pivotPoint.position, Vector3.forward, Vector3.Angle(targetPosition, transform.position));

My answer should work, the sprite's up and right axes are on the x-y plane, so altering them rotates it around the z-axis. Just write: transform.up = targetPosition - transform.position

–

i see,... i was moving the object twice in the code, it was redundant. i didn't understand how rotateAround works. my bad. Thanks for answering! im actually rotating around the z because the sprites are drawn from a top down perspective, on a x - horizontal and y - vertical grid.

–