Camara following the hero. 3rd person

Hi. First of all, I would like to say I’m starting with Unity3d, so I’m quite noob. Be patient pls.

I’m trying to develop a main camara which follows the hero in a 3rd person perspective. I managed to do it using translate with the distance between the camera and the hero, but the problem is, when my hero is not moving, the camera turns crazy and starts to translate at the same point, vibrating. I guess when I do the Normalize with movement vector, some float digits are lost and that’s why when I translate, every time, even if the hero is not moving, the result is diferent.

Here is the code I’m using:

function Update () {

var mov : Vector3 = Vector3(0, 0, 0);
var protaPos : Vector3 = player.transform.position;
var camPos : Vector3 = transform.position;

mov = Vector3(protaPos.x - camPos.x, (protaPos.y + camHeight) - camPos.y, (protaPos.z - camDistance) - camPos.z);	
mov.Normalize();
transform.Translate(mov*camSpeed*Time.deltaTime);
}

Any tips? And a bit lost here. Really thanks.

This script is avalible in the Standard Assets scripts folder.

SmoothFollow (JS)

/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")


function LateUpdate () {
	// Early out if we don't have a target
	if (!target)
		return;
	
	// Calculate the current rotation angles
	var wantedRotationAngle = target.eulerAngles.y;
	var wantedHeight = target.position.y + height;
		
	var currentRotationAngle = transform.eulerAngles.y;
	var currentHeight = transform.position.y;
	
	// Damp the rotation around the y-axis
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

	// Convert the angle into a rotation
	var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
	transform.position -= currentRotation * Vector3.forward * distance;

	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
}

Just add the camera as a child of the character object in the hierarchy, then add the script to the camera. Hope this helped.