纳金网

标题: 镜头平滑跟随代码(用于赛车游戏) [打印本页]

作者: 王者再临    时间: 2014-4-30 23:11
标题: 镜头平滑跟随代码(用于赛车游戏)
老外发布的一个摄像机跟随代码,主要用于赛车游戏中,比官方默认的那个效果要更强大些,下面是详细功能简介:

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
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CarSmoothFollow : MonoBehaviour {
  4.     public Transform target;
  5.     public float distance = 20.0f;
  6.     public float height = 5.0f;
  7.     public float heightDamping = 2.0f;
  8.     public float lookAtHeight = 0.0f;
  9.     public Rigidbody parentRigidbody;
  10.     public float rotationSnapTime = 0.3F;
  11.     public float distanceSnapTime;
  12.     public float distanceMultiplier;
  13.     private Vector3 lookAtVector;
  14.     private float usedDistance;
  15.     float wantedRotationAngle;
  16.     float wantedHeight;
  17.     float currentRotationAngle;
  18.     float currentHeight;
  19.     Quaternion currentRotation;
  20.     Vector3 wantedPosition;
  21.     private float yVelocity = 0.0F;
  22.     private float zVelocity = 0.0F;
  23.     void Start () {
  24.         lookAtVector =  new Vector3(0,lookAtHeight,0);
  25.     }
  26.     void LateUpdate () {
  27.         wantedHeight = target.position.y + height;
  28.         currentHeight = transform.position.y;
  29.         wantedRotationAngle = target.eulerAngles.y;
  30.         currentRotationAngle = transform.eulerAngles.y;
  31.         currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
  32.         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  33.         wantedPosition = target.position;
  34.         wantedPosition.y = currentHeight;
  35.         usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime);
  36.         wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
  37.         transform.position = wantedPosition;
  38.         transform.LookAt(target.position + lookAtVector);
  39.     }
  40. }
复制代码

作者: HIDEOKOJIMA    时间: 2014-5-1 00:39
Its not good, but thanks for sharing !
作者: dxxd    时间: 2014-5-4 13:09
感谢分享~~~




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