查看: 3414|回复: 2
打印 上一主题 下一主题

[其他] 镜头平滑跟随代码(用于赛车游戏)

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-4-30 23:11:58 |只看该作者 |倒序浏览
老外发布的一个摄像机跟随代码,主要用于赛车游戏中,比官方默认的那个效果要更强大些,下面是详细功能简介:

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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-5-1 00:39:10 |只看该作者
Its not good, but thanks for sharing !
回复

使用道具 举报

dxxd    

4

主题

10

听众

4225

积分

中级设计师

Rank: 5Rank: 5

纳金币
12
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-5-4 13:09:42 |只看该作者
感谢分享~~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-10 11:47 , Processed in 0.092132 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部