Exact Rotation Around Another Point Over Time

Hi, all!

I am very familiar with rotating an object over time, and using Lerp to make the end result exact. However, I want to rotate an object around another point. I see two options here, and they both have issues. Feel free to help me with whatever you choose.

  1. I could use transform.RotateAround(), but this has no leaping abilities and often yields unreliable results in a while loop
  2. I could use animation, however after my object spins around its point and returns to its idle animation (Which is just a simple up and down ‘floaty’ location change over like 100 frames), it stays in its changed position, but rotates back to how it was before (I set no rotation keyframes in Blender so I don’t know why this is happening).

These are my solutions. Please feel free to help with either as an answer, or if you have something better yourself feel free to share.

EDIT: This is the setup, with both ships parented to a sing empty in the middle. When a keystroke is made for rotating left or right, the proper ship should be used as a pivot and rotation should occur on the Z axis over 0.5-1.0 seconds. This complicates things because I don’t think switching around parenting like that is good practice, especially later when the ships will interact with the world.

alt text

Thanks! - YA

create new GameObject have positon is the point(that you want rotate around).

Set your game object is new gameobject child.

rotate new game object by lerp function.

Like dagon said, the simplest approach would be to set up a GameObject hierarchy to control this. So do something like:


→ ChildObject

‘PivotObject’ could then be rotated in any way and ‘ChildObject’ will follow. Place your rotation script on ‘PivotObject’ and you will most likely have better results with playing animations on ‘ChildObject’ without any transforms being overridden.

using UnityEngine;
public class RotateAroundPivot : MonoBehaviour 
	public float speed;

	void Update()
        // Use one or the other of these, not both! //
		// Rotation with Transform.RotateAround() //
		// transform.RotateAround(transform.position, transform.up, Time.deltaTime * speed);

		// Rotation with Quaternion.AngleAxis() //
		transform.rotation = Quaternion.AngleAxis(Time.deltaTime * speed, transform.up) * transform.rotation;

Try placing this script on PivotObject and commenting/uncommenting either the RotateAround() or AngleAxis() line. Either one should work just fine.

Figured it out myself. Feel free to use the code if you’d like.

This code will rotate an object around a given pivot over a period of time and then snap to that value for absolute accuracy.

	IEnumerator SpinMe( Transform spinAxis, float degrees, float inTime ){
		coRunning = true;
		Vector3 firstAxisPos = spinAxis.position;
		Vector3 firstPos = transform.position;
		Quaternion firstRot = transform.rotation;
		Vector3 firstRotVector = transform.eulerAngles;
		float radius = Vector3.Distance( spinAxis.position, transform.position );
		float rads = degrees*Mathf.Deg2Rad;
		float yPos = radius*Mathf.Sin( rads );
		float xPos = radius*Mathf.Cos( rads );
		Vector3 newPos = new Vector3( xPos, yPos, firstPos.z );
		Vector3 byAngles = new Vector3( 0.0f, 0.0f, degrees );
		Quaternion newRot = Quaternion.Euler ( transform.eulerAngles + byAngles );
		spinning = true;
		float perc = 0.0f;
		for( float i = 0.0f; i < inTime; i += Time.smoothDeltaTime ){
			if( (i + Time.smoothDeltaTime ) >= inTime ) i = inTime;
				perc = i/inTime;
				perc = perc*perc*(3f-2f*perc);
			transform.position = Vector3.Lerp ( firstPos, newPos, perc );
			transform.rotation = Quaternion.Lerp ( firstRot, newRot, perc );
			transform.position = Recalibrate ( firstAxisPos, spinAxis.position );
			yield return null;
		Vector3 degreeChange = Vector3.forward*degrees;
		transform.Rotate( (firstRotVector+degreeChange)-transform.eulerAngles );
		transform.position = Recalibrate ( firstAxisPos, spinAxis.position );
		spinning = false;

		deltaRotation += degrees;
		if (Mathf.Abs ( deltaRotation ) == 180.0f) FlipMissiles();


	Vector3 Recalibrate( Vector3 firstPos, Vector3 newPos ){
		float xMove = firstPos.x - newPos.x;
		float yMove = firstPos.y - newPos.y;
		return new Vector3( transform.position.x + xMove, transform.position.y + yMove, transform.position.z );