- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
老外发布的一个摄像机跟随代码,主要用于赛车游戏中,比官方默认的那个效果要更强大些,下面是详细功能简介:
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);
- }
- }
复制代码 |
|