Smooth Follow Camera
SmoothFollow2
Author: Daniel
Improved Version
Author: Vasilis Christopoulos & Daniel Toliaferro
Contents[hide] |
Description
This is designed to make a camera smoothly follow a ship in space.
Usage
Place this script onto a camera.
JavaScript - SmoothFollow2.js
var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
function Update () {
var wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
var 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);
}
C# - SmoothFollow2.cs
using UnityEngine;
using System.Collections;
public class SmoothFollow2 : MonoBehaviour {
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public float rotationDamping = 10.0f;
void Update () {
Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion 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);
}
}
C# - SmoothFollow2.cs (Improved)
using UnityEngine;
using System.Collections;
public class SmoothFollow2 : MonoBehaviour {
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update () {
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion 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);
}
}
'Unity3D' 카테고리의 다른 글
Unity3d Android 해상도 설정하기 ( Screen.SetResolution ) (0) | 2012.11.14 |
---|---|
Unity remote 사용방법 (iPhone, Android) (0) | 2012.11.08 |
Timer()함수를 대신하는 Invoke()함수 (0) | 2012.11.05 |
캐릭터 컨트롤 하기 (0) | 2012.11.03 |
빌보드 관련 (0) | 2012.11.02 |