纳金网

标题: 镜头平滑跟随代码 [打印本页]

作者: 晃晃    时间: 2011-8-12 10:47
标题: 镜头平滑跟随代码
老外发布的一个摄像机跟随代码,主要用于赛车游戏中,比官方默认的那个效果要更强大些,下面是详细功能简介:

Here's another smooth camera script. Designed to follow a car smoothly and give you the ability to zoom out when the car is moving. Hope someone makes use of it.

    target = self explanatory
    distance = Standard distance to follow object
    height = The height of the camera
    heightDamping = Smooth out the height position
    lookAtHeight = An offset of the target
    parentRigidbody = Used to determine how far the camera should zoom out when the car moves forward
    rotationSnapTime = The time it takes to snap back to original rotation
    distanceSnapTime = The time it takes to snap back to the original distance or the zoomed distance (depending on speed of parentRigidyBody)
    distanceMultiplier = Make this around 0.1f for a small zoom out or 0.5f for a large zoom (depending on the speed of your rigidbody)


Looks best when you place the target transform on the front axle


代码如下:
using UnityEngine;
using System.Collections;
public class CarSmoothFollow : 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);
    }
}


作者: Asen    时间: 2011-9-17 10:52

作者: tc    时间: 2012-2-28 23:29
路过……

作者: 菜刀吻电线    时间: 2012-4-13 23:25
很经典,很实用,学习了!

作者: 晃晃    时间: 2012-5-23 23:20
楼主收集的可真全哦

作者: tc    时间: 2012-8-19 23:47
响应天帅号召,顶

作者: 奇    时间: 2012-10-25 23:39
不错哦,顶一下......

作者: 晃晃    时间: 2013-2-3 23:29
我看看就走,你们聊!

作者: C.R.CAN    时间: 2013-3-10 23:18
呵呵,很好,方便罗。





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5