I want to move in a circular motion around a pivot, and it “kind of” works but it doesnt work perfectly. For some reason every time it runs the agent starts by moving backwards a few steps, why is this? I expect it to walk in a perfect circle around the pivot.
//In Start, I set the pivot and calculate the current angle:
pivotPoint = target.Value.transform.position;
Vector3 initialOffset = transform.position - pivotPoint;
angle = Mathf.Atan2(initialOffset.z, initialOffset.x);
// In Update, I move:
// Update the angle based on time and speed
angle += maxSpeed.Value * Time.deltaTime;
// Calculate the position offset from the pivot
float x = Mathf.Cos(angle) * (radius.Value / 2);
float z = Mathf.Sin(angle) * (radius.Value / 2);
// Set the character's position relative to the pivot
Vector3 newPosition = pivotPoint + new Vector3(x, 0f, z);
Vector3 movementDir = (newPosition - transform.position);
movementDir.y = 0;
// Move
characterController.Move(movementDir* maxSpeed.Value* Time.deltaTime);
Video (after each stop, the state is re-run):
RTNzQ7FiRnPjoNSzyx