查看: 1921|回复: 5
打印 上一主题 下一主题

[其他] Unity3D模仿龙之谷的相机

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

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

跳转到指定楼层
楼主
发表于 2014-6-26 20:07:38 |只看该作者 |倒序浏览

用法:先在你的Player里新建一个空的Object重命名为CameraTarget(你起什么名字都行),然后将脚本拖动到你的Player里,将你新建的 CameraTarget 拖动到脚本的Target属性,运行你就可以看到效果了
  1. using UnityEngine;
  2. using System.Collections;

  3. public class DragonHillCamera : MonoBehaviour
  4. {
  5.         public bool HideAndShowCursor = true;
  6.         public bool LockRotationWhenRightClick = false;
  7.         public bool UseBlurEffect = true;
  8.         public bool UseFogEffect = true;
  9.     public Transform target;
  10.    
  11.     public float targetHeight = 1.0f;
  12.     public float distance = 5.0f;

  13.     public float maxDistance = 20;
  14.     public float minDistance = .6f;

  15.     public float xSpeed = 250.0f;
  16.     public float ySpeed = 120.0f;

  17.     public int yMinLimit = -80;
  18.     public int yMaxLimit = 80;

  19.     public int zoomRate = 40;

  20.     public float rotationDampening = 3.0f;
  21.     public float zoomDampening = 10.0f;

  22.     private float x = 0.0f;
  23.     private float y = 0.0f;
  24.     private float currentDistance;
  25.     private float desiredDistance;
  26.     private float correctedDistance;
  27.         private bool grounded = false;

  28.         void Start ()
  29.     {
  30.                 Screen.lockCursor = true;
  31.         Vector3 angles = transform.eulerAngles;
  32.         x = angles.x;
  33.         y = angles.y;

  34.         currentDistance = distance;
  35.         desiredDistance = distance;
  36.         correctedDistance = distance - 0.2f;

  37.         // Make the rigid body not change rotation
  38.         if (rigidbody)
  39.             rigidbody.freezeRotation = true;
  40.         }

  41.         void LateUpdate ()
  42.     {
  43.             // Don't do anything if target is not defined
  44.         if (!target){
  45.                         GameObject go = GameObject.Find("layer");
  46.                         target = go.transform;
  47.                         transform.LookAt(target);
  48.             return;
  49.                 }
  50.         // If either mouse buttons are down, let the mouse govern camera position
  51.                 if(LockRotationWhenRightClick== false){
  52.                     x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  53.                             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  54.                         }
  55.         if (Input.GetMouseButton(0)){
  56.                         if(LockRotationWhenRightClick== false){
  57.                     x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  58.                             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  59.                         }
  60.         }
  61.         y = ClampAngle(y, yMinLimit, yMaxLimit);

  62.         // set camera rotation
  63.         Quaternion rotation = Quaternion.Euler(y, x, 0);

  64.         // calculate the desired distance
  65.         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
  66.         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
  67.         correctedDistance = desiredDistance;

  68.         // calculate desired camera position
  69.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));

  70.         // check for collision using the true target's desired registration point as set by user using height
  71.         RaycastHit collisionHit;
  72.         Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);

  73.         // if there was a collision, correct the camera position and calculate the corrected distance
  74.         bool isCorrected = false;
  75.         if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
  76.         {
  77.                         if(collisionHit.transform.name!=target.name){
  78.                                 position = collisionHit.point;
  79.                         correctedDistance = Vector3.Distance(trueTargetPosition, position);
  80.                         isCorrected = true;
  81.                         }
  82.         }
  83.         
  84.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
  85.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;

  86.         // recalculate position based on the new currentDistance
  87.         position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight - 0.05f, 0));

  88.         transform.rotation = rotation;
  89.         transform.position = position;
  90.                
  91.         }

  92.     private static float ClampAngle(float angle, float min, float max)
  93.     {
  94.         if (angle < -360)
  95.             angle += 360;
  96.         if (angle > 360)
  97.             angle -= 360;
  98.         return Mathf.Clamp(angle, min, max);
  99.     }
  100. }
复制代码
来源:http://www.oschina.net/code/snippet_913647_16294
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

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

沙发
发表于 2014-6-26 21:23:16 |只看该作者
Good to know ! ! ! !
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

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

板凳
发表于 2014-6-26 22:38:55 |只看该作者
感谢分享这个!
回复

使用道具 举报

wucnj    

1

主题

1

听众

3160

积分

中级设计师

Rank: 5Rank: 5

纳金币
1065
精华
0

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

地板
发表于 2014-6-27 13:21:23 |只看该作者
感谢分享!!!
回复

使用道具 举报

1

主题

2

听众

510

积分

初级设计师

Rank: 3Rank: 3

纳金币
49
精华
0

最佳新人

5#
发表于 2014-6-30 15:37:33 |只看该作者
来张图嘛
回复

使用道具 举报

0

主题

1

听众

823

积分

初级设计师

Rank: 3Rank: 3

纳金币
80
精华
0

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

6#
发表于 2014-7-1 14:22:29 |只看该作者
谢谢分享~~~~
回复

使用道具 举报

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

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-23 21:08 , Processed in 0.090490 second(s), 32 queries .

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

© 2008-2019 Narkii Inc.

回顶部