纳金网

标题: Unity3D模仿龙之谷的相机 [打印本页]

作者: 狂风大尉    时间: 2014-6-26 20:07
标题: Unity3D模仿龙之谷的相机

用法:先在你的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

作者: hyui    时间: 2014-6-26 21:23
Good to know ! ! ! !
作者: HIDEOKOJIMA    时间: 2014-6-26 22:38
感谢分享这个!
作者: wucnj    时间: 2014-6-27 13:21
感谢分享!!!
作者: 52530740    时间: 2014-6-30 15:37
来张图嘛
作者: icerein    时间: 2014-7-1 14:22
谢谢分享~~~~




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5