Unity 镜头拉近效果(带缓冲)

 

 

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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值