Lerping object movement

//////////////////////////////////////////////////////////////
// FollowTransform.js
// Penelope iPhone Tutorial
//
// FollowTransform will follow any assigned Transform and 
// optionally face the forward vector to match for the Transform
// where this script is attached.
//////////////////////////////////////////////////////////////

var FollowObject : Transform;		// Transform to follow
var RotatingObject : Transform;		// Transform to follow
var Degrees : float = 90.0;
var faceForward : boolean = false;		// Match forward vector?
private var thisTransform : Transform;
private var clicked = false;


function Start()
{
	// Cache component lookup at startup instead of doing this every frame
	thisTransform = transform;
}

function Update () 
{	
	thisTransform.position = FollowObject.position;
	if(Input.GetMouseButtonDown(0)) {
		clicked = true;
	}
	
	if (!clicked) {
		Debug.Log ("Waiting for click");
	}
	else {
		rotatecam ();
	}
	
}

function rotatecam ()
{
	transform.RotateAround (RotatingObject.position, RotatingObject.up, Degrees);
	clicked = false;
}

I modified the Penlope script for my new game, and all works fine, except the camera ‘snaps’ between the positions when you tap the screen. I think I could use lerp to have a smooth movement between each position, but I am not sure how?

Yes, that is what you need to do. The camera snaps because you are abruptly changing its position and rotation in the update, frame to frame. Here is nice script on the Unity wiki:

http://www.unifycommunity.com/wiki/index.php?title=SmoothFollow2

Lots of other cool stuff there as well.

Thanks for the script, but maybe I wasn’t too clear about what I am trying to do. Basically, I want the camera to animate moving from one position to the next in 90-degree increments when I tap the screen (or a button on the screen), while staying focused on the player. So, the player is in the center and the camera orbits around him (this I have working, the orbiting, I just need to get the cam to animate from one position to the next). See the video here for a perfect example of what I am trying to duplicate:

http://www.whitespacecg.com/Misc/Cam.mp4

Ah, I get it. I’m a visual learner :slight_smile:

For an constant orbiting rotation, you would do something like this:

myCamera.transform.RotateAround (myAvatar.transform.position, Vector3.up, camOrbitSpeed * Time.deltaTime);

myCamera.transform.LookAt(myAvatar.transform);

You’re rotating both the camera game object around the avatar and rotating the camera to look at the avatar.

So then, by changing the speed from 0 to a positive value, you would cause it to rotate and then stop at a certain point along its orbit. Use Mathf.SmoothStep to increase to increase and then decrease the rotation speed (smooth step from 0 to max speed while the rotation angle goes from 0 to 45 degrees and then max speed to 0 while the rotation angle goes from 45 to 0).

[I think I understand what you mean…but how would I implement it into my code above? Could you give me an example please?

And just to mention, there is no documentation on MathF.SmoothStep in the Unity iPhone docs (although I know it is a valid command…)

EDIT:

My roomate made a pretty good suggestion last night…so what I did was animate a dummy object in Maya rotating 90 degrees around a central point. I then made the camera a child of that object and placed a script on the cam to force it to always face the avatar, and trigger the camera rotation with an onscreen button. Worked like a charm, although I kind of would like to know how to achieve this programmatically.