unity3d攝像機的效果代碼系列——帶緩沖的鏡頭拉
來源:
|
責任編輯:她。 |
發(fā)布時間: 2017-08-28 14:51 | 瀏覽量:
本節(jié)匯寶盆為大家?guī)淼氖牵簎nity3d攝像機的效果代碼系列——帶緩沖的鏡頭拉
果代碼系列——帶緩沖的鏡頭拉近效果!
大家應該見過,一些游戲中,當玩家發(fā)現(xiàn)了某樣東西,攝像機會馬上移動過去,但在快到的時候會減慢速度,這個代碼實現(xiàn)的就是那個效果。
var target : Transform;
var distance : float = 3.0;
var height : float = 1.0;
var damping : float = 5.0;
var smoothRotation : boolean = true;
var rotationDamping : float = 10.0;
var targetLookAtOffset : Vector3; // allows offsetting of camera lookAt, very useful for low bumper heights
var bumperDistanceCheck : float = 2.5; // length of bumper ray
var bumperCameraHeight : float = 1.0; // adjust camera height while bumping
var bumperRayOffset : Vector3; // allows offset of the bumper ray from target origin
function FixedUpdate() {
var wantedPosition = target.TransformPoint(0, height, -distance);
// check to see if there is anything behind the target
var hit : RaycastHit;
var back = target.transform.TransformDirection(-1 * Vector3.forward);
// cast the bumper ray out from rear and check to see if there is anything behind
if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, hit, bumperDistanceCheck)) {
// clamp wanted position to hit position
wantedPosition.x = hit.point.x;
wantedPosition.z = hit.point.z;
wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
var lookPosition : Vector3 = target.TransformPoint(targetLookAtOffset);
if (smoothRotation) {
var wantedRotation : Quaternion = Quaternion.LookRotation(lookPosition - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
} else {
transform.rotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
}
}
-
分享到:
相關文章
網(wǎng)友評論
全部評論:0條
推薦
熱門