kaisyu
3/20/2014 - 7:49 AM

Converted from the JavaScript source code in http://imdaunity.blogspot.kr/2011/02/camera-follow-script.html

Converted from the JavaScript source code in http://imdaunity.blogspot.kr/2011/02/camera-follow-script.html

using UnityEngine;
using System.Collections;

public class CameraFollowPlayer : MonoBehaviour {
    public Transform target;
    public float distance = 3.0;
    public float height = 3.0;
    public float damping = 5.0;
    public bool smoothRotation = true;
    public float rotationDamping = 10.0;

    void Update () {
        wantedPosition = target.TransformPoint(0, height, -distance);
        transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

        if (smoothRotation) {
            wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
            transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
        }
        else
            transform.LookAt (target, target.up);
    }

}