I have a different movement script that you may find useful, and two scripts to allow the camera to smoothly follow it from a distance determined by you.
The length of this answer may seem discouraging, but remember, most of it is simply code to be copied and pasted.
Apply this script to your character. It is called ‘Movement.js’.
var speed : float = 6.0;
var twiceSpeed : float = 10.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
//Grounded, so recalculate
//Move directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
//Rotation Code
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
//Animation Code
if (Input.GetKeyDown("w"))
{
animation.Play("Full Walk");
}
else if (Input.GetKeyUp("w"))
{
animation.Stop();
animation.Play("Idle");
}
}
//Apply Gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move Controller
controller.Move(moveDirection * Time.deltaTime);
}
These next two scripts are both applied to the camera.
CameraRotation.js:
#pragma strict
function Update () {
if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, -1);
if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, 1);
if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);
if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);
}#pragma strict
function Update () {
if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, -1);
if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, 1);
if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);
if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);
}
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);
}
I hope that this is of use to you.