查看: 2071|回复: 8
打印 上一主题 下一主题

镜头平滑跟随代码

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

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

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

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

Asen    

867

主题

0

听众

1万

积分

外协人员

Rank: 7Rank: 7Rank: 7

纳金币
17488
精华
1
沙发
发表于 2011-9-17 10:52:45 |只看该作者
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-2-28 23:29:17 |只看该作者
路过……
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-4-13 23:25:29 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

5#
发表于 2012-5-23 23:20:00 |只看该作者
楼主收集的可真全哦
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

6#
发表于 2012-8-19 23:47:57 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

7#
发表于 2012-10-25 23:39:30 |只看该作者
不错哦,顶一下......
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2013-2-3 23:29:22 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

9#
发表于 2013-3-10 23:18:08 |只看该作者
呵呵,很好,方便罗。
回复

使用道具 举报

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

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

GMT+8, 2025-7-21 20:30 , Processed in 0.064359 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部