From Unify Community Wiki
Revision as of 15:34, 15 April 2011 by Trooper (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: David O'Donoghue (a.k.a Trooper from ODD Games)


Looks at a target with values to dampen height, rotation and distance (based on a rigidbody's velocity).

C# Script

<csharp>using UnityEngine; using System.Collections;

public class SmoothFollow : MonoBehaviour {

public Transform target; public float distance = 20.0f; public float height = 5.0f; public float heightDamping = 2.0f;

public float lookAtHeight = 0.0f;

public Rigidbody parentRigidbody;

public float rotationSnapTime = 0.3F;

public float distanceSnapTime; public float distanceMultiplier;

private Vector3 lookAtVector;

private float usedDistance;

float wantedRotationAngle; float wantedHeight;

float currentRotationAngle; float currentHeight;

Quaternion currentRotation; Vector3 wantedPosition;

private float yVelocity = 0.0F; private float zVelocity = 0.0F;

void Start () {

lookAtVector = new Vector3(0,lookAtHeight,0);


void LateUpdate () {

wantedHeight = target.position.y + height; currentHeight = transform.position.y;

wantedRotationAngle = target.eulerAngles.y; currentRotationAngle = transform.eulerAngles.y;

currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);

currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

wantedPosition = target.position; wantedPosition.y = currentHeight;

usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime);

wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);

transform.position = wantedPosition;

transform.LookAt(target.position + lookAtVector);



Personal tools