免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1008 | 回复: 0
打印 上一主题 下一主题

Unity3d CarSmoothFollow [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览
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)

  1. using UnityEngine;
  2. using System.Collections;

  3. public class CarSmoothFollow : MonoBehaviour {
  4.     
  5.     public Transform target;
  6.     public float distance = 20.0f;
  7.     public float height = 5.0f;
  8.     public float heightDamping = 2.0f;

  9.     public float lookAtHeight = 0.0f;
  10.     
  11.     public Rigidbody parentRigidbody;

  12.     public float rotationSnapTime = 0.3F;
  13.     
  14.     public float distanceSnapTime;
  15.     public float distanceMultiplier;
  16.     
  17.     private Vector3 lookAtVector;

  18.     private float usedDistance;
  19.     
  20.     float wantedRotationAngle;
  21.     float wantedHeight;
  22.     
  23.     float currentRotationAngle;
  24.     float currentHeight;
  25.     
  26.     Quaternion currentRotation;
  27.     Vector3 wantedPosition;

  28.     private float yVelocity = 0.0F;
  29.     private float zVelocity = 0.0F;

  30.     void Start () {
  31.     
  32.         lookAtVector = new Vector3(0,lookAtHeight,0);
  33.         
  34.     }

  35.     void LateUpdate () {

  36.         wantedHeight = target.position.y + height;
  37.         currentHeight = transform.position.y;
  38.         
  39.         wantedRotationAngle = target.eulerAngles.y;
  40.         currentRotationAngle = transform.eulerAngles.y;

  41.         currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);

  42.         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

  43.         wantedPosition = target.position;
  44.         wantedPosition.y = currentHeight;
  45.     
  46.         usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime);
  47.         
  48.         wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);

  49.         transform.position = wantedPosition;
  50.         
  51.         transform.LookAt(target.position + lookAtVector);
  52.         
  53.     }
  54.     
  55. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP