Jump to content
  • Advertisement
Sign in to follow this  
Marat Zahidylin

Unity Slow rotate from point A to point B?

This topic is 653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all.

Inside Unity engine - what is the best way to rotate camera (slow) from point A to point B....A and B are Vec3 points in world coordinates?

I want to create empty game object tempObj , and Lerp it position from point A to point B and call cameraTransform.LookAt(tempObj ) in update.

 

6iJl0Mp.jpg

Edited by zahidulinmarat

Share this post


Link to post
Share on other sites
Advertisement

You can use Vector3.Slerp to calculate the intermediate look direction of the camera. Just remember that the vector parameters are directions rather than points in space. When you have the intermediate direction you can use Quaternion.SetLookRotation to calculate the rotation of the camera transform.

Alternatively you can add an empty GameObject to the scene that you lerp between the two world positions, and then simply call Transform.LookAt, passing in the object as the target. Do note that this does not necessarily give you a constant rotation speed of the camera transform.

Share this post


Link to post
Share on other sites

Not tested coroutine, but there is no reason for it not to work.

public IEnumerator RotateTowards(Transform transObj, Transform lookAt, float duration)
	{
                //Get current rotation
		Quaternion startRot = transObj.rotation;

                //Make a temporary gameObj with wanted rotation
		GameObject temp = new GameObject();
		Transform tempTrans = temp.transform;
		tempTrans.position = transObj.position;
		tempTrans.rotation = transObj.rotation;
		tempTrans.LookAt(lookAt);
		Quaternion endRot = tempTrans.rotation;

                //Lerp with wanted duration
		float timeElapsed = 0f;
		while (timeElapsed < duration)
		{
			timeElapsed += Time.deltaTime;
			transObj.rotation = Quaternion.Lerp(startRot, endRot, timeElapsed / duration);
			yield return new WaitForEndOfFrame();
		}
                //Destroy crunches
		Destroy(temp);
		}

Share this post


Link to post
Share on other sites

I've never tried coding this, but this sounds like a job for quaternion SLERP. I've not found a way to interpolate matrices without decomposing them, which is quite a few CPU cycles. I'm generally not a huge fan of quaternions, but I believe interpolation is where they really shine and outperform any other option. I believe interpolation is why they have their place in the game industry.

Most people don't understand quaternions at all. I could tell you about the years I was on a quest to understand them and how little I still know of them. But they are not at all difficult to understand as they are used in this problem. First, understand that game quaternions are a special case of quaternions known as unit quaternions. They are a subset of real quaternions. Unit quaternions are actually not that difficult to understand even though quaternions are hyper dimensional complex (a combination of imaginary and real) numbers.

 

You can think of unit quaternions as 3 dimensional arrows that point in a specific direction, much like vectors. The difference is that quaternions have a specific rotation around themselves, which vectors do not. For this reason, they are more like orientation matrices than vectors. You load them up much like a matrix with an orientation. Quaternions here are very similar to a 3 by 3 matrix. A 4 by 4 matrix can store position and orientation. A 3 by 3 matrix only stores orientation. Similarly, a quaternion only stores orientation and can't store position. So, you have to store the position and any scale value separately.

 

Don't try and understand their math. Besides the world of imaginary numbers scrambling your brain and short circuiting it, it won't really help. Think of them as a black box that you load with an orientation. Load it, do all your rotations or whatever needs to be done, and then read the orientation out of it when you are done. (More accurately, you're probably going to convert it to a 4 by 4 matrix and invert it for a View matrix.) In other words, let the quaternions know what the orientation is, you as the programmer don't need to know what's inside the quaternion at any point.

 

As for LERP, that stands for Linear inERPolation. That will work, but SLERP (Spherical Linear intERPolation) is a better choice for this problem I believe. I believe LERP will un-naturally accelerate the rotation points you are interpolating between the two rotations. You can google the difference between LERP and SLERP for more detail on that, but I think SLERP will give you a smoother and more regular rotation in this case.

 

The way Unity does things will probably confuse the math here a bit. You would probably want to either understand that the View matrix (camera) works backwards for things like rotations and translations and/or Invert it to make it work like an object's world-matrix (position and orientation). Then you do your rotation after the inversion and invert it back to once again turn it into a View matrix. In the case of Unity, however, you probably have an object rotation function that will accept a quaternion as input. Don't be surprised if you have to rotate the camera in the opposite direction you would a standard game object.

 

Dakila's code looks like it would probably work, but look into SLERP as a replacement for LERP. Not sure what the options for that in Unity are.

Edited by BBeck

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!