unity3d摄像机的效果代码系列——带缓冲的镜头拉
果代码系列——带缓冲的镜头拉近效果!
大家应该见过,一些游戏中,当玩家发现了某样东西,摄像机会马上移动过去,但在快到的时候会减慢速度,这个代码实现的就是那个效果。
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);
}
}
上一篇:Unity3d中链接数据库常用脚本[ 01-17 ]
下一篇:关于unity内镜头视角的做法[ 01-17 ]