Unity3D第三人称镜头脚本 可旋转,拉近



<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class TP_Camera : MonoBehaviour
{
 
   publicstatic TP_Camera Instance;
    
    publicTransform TargetLookAt;
    public float Distance = 15f;
    public float DistanceMin = 3f;
    public int DistanceMax = 50;
    public floa tDistanceSmooth = 0.05f;
    public float X_MouseSensitivity = 5f;
    public float Y_MouseSensitivity = 5f;
    public float MouseWheelSensitivity = 5f;
    public float Y_MinLimit  = -40f;
    public float Y_MaxLimit = 80f;
    public float X_Smooth = 0.05f;
    public float Y_Smooth = 0.1f;
    
    private float mouseX = 0f;
    private float mouseY = 0f;
    private float velY = 0f;
    private float velX = 0f;
    private float velZ = 0f;
    private Vector3 position = Vector3.zero;
    private float velDistance = 0f;
    private float startDistance = 0f;
    private Vector3 desiredPosition = Vector3.zero;
    private float desiredDistance = 0f;
    
    
    void Awake()
    {
      Instance = this;
      
    }
    
    void Start()
    {
      Distance =Mathf.Clamp(Distance,DistanceMin,DistanceMax);      //Clamps a value between a minimum float andmaximum float value.   
      startDistance =Distance;   
      Reset();
      
    }
    
    void LateUpdate()
    {
      if(TargetLookAt == null)
         return;
      
      HandlePlayerInput();
      
      CalculateDesiredPosition();
      
      CheckCameraPoints(TargetLookAt.position,desiredPosition);
      
      UpdatePosition();
      
    }
    
    void HandlePlayerInput()
    {
      var deadZone = 0.1f;
      
      if(Input.GetMouseButton(1))   //values are 0 for left button, 1 for rightbutton, 2 for the middle button.
      {
         mouseX += Input.GetAxis("Mouse X") *X_MouseSensitivity;       Get the mouse delta. This is not in therange -1...1

         mouseY -= Input.GetAxis("Mouse Y") *Y_MouseSensitivity;
      }
    //This iswhere we will limit mouseY
    mouseY =Helper.ClampAngle(mouseY, Y_MinLimit,Y_MaxLimit);
    
   if(Input.GetAxis("Mouse ScrollWheel")< -deadZone || Input.GetAxis("Mouse ScrollWheel")> deadZone)
    {
      desiredDistance = Mathf.Clamp(Distance -Input.GetAxis("Mouse ScrollWheel") *MouseWheelSensitivity,DistanceMin,DistanceMax);
      
    }
}
    void CalculateDesiredPosition()
    {
      //Evaluate distance
      Distance = Mathf.SmoothDamp(Distance,desiredDistance, ref velDistance,DistanceSmooth);
      
      //Calculate desired position
      desiredPosition = CalculatePosition(mouseY,mouseX, Distance);
         
    }
    
    Vector3 CalculatePosition(float rotationX,float rotationY,floatdistance)
    {
      Vector3 direction  = newVector3(0,0, -distance);
      Quaternion rotation =Quaternion.Euler(rotationX,rotationY,0);
      return TargetLookAt.position + rotation *direction;
      
    }
    
    float CheckCameraPoints(Vector3 from,Vector3 to)
    {
      var nearDistance = -1f;
      
      RaycastHit hitInfo;
      
      Helper.ClipPlanePoints clipPlanePoints =Helper.ClipPlaneAtNear(to);
      
      //Draw lines in the editor to make it easier tovisulize
      Debug.DrawLine(from,to + transform.forward *-camera.nearClipPlane,Color.red);
      Debug.DrawLine(from,clipPlanePoints.UpperLeft);
      Debug.DrawLine(from,clipPlanePoints.LowerLeft);
      Debug.DrawLine(from,clipPlanePoints.UpperRight);
      Debug.DrawLine(from,clipPlanePoints.LowerLeft);
      
      Debug.DrawLine(clipPlanePoints.UpperLeft,clipPlanePoints.UpperRight);
      Debug.DrawLine(clipPlanePoints.UpperRight,clipPlanePoints.LowerRight);
      Debug.DrawLine(clipPlanePoints.LowerRight,clipPlanePoints.LowerLeft);
      Debug.DrawLine(clipPlanePoints.LowerLeft,clipPlanePoints.UpperLeft);
      
      return nearDistance;
    }
      
    void UpdatePosition()
    {
      var posX = Mathf.SmoothDamp(position.x,desiredPosition.x ,ref velX,X_Smooth);
      var posY = Mathf.SmoothDamp(position.y,desiredPosition.y ,ref velY,Y_Smooth);
      var posZ = Mathf.SmoothDamp(position.z,desiredPosition.z ,ref velZ, X_Smooth);
      position = new Vector3(posX,posY,posZ);
      
      transform.position = position;
      
      transform.LookAt(TargetLookAt);
      
    }
    
    public void Reset()
    {
      mouseX = 0;
      mouseY = 10;
      Distance = startDistance;
      desiredDistance = Distance;
    }
    
    public static void UseExistingOrCreateMainCamera()
    {
      GameObject tempCamera;
      GameObject targetLookAt;
      TP_Camera myCamera;
      
      if(Camera.main != null)
      {
         tempCamera = Camera.main.gameObject;
      }
      else
      {
         tempCamera = new GameObject("Main Camera");
         tempCamera.AddComponent("Camera");
         tempCamera.tag = "MainCamera";
      
      
      tempCamera.AddComponent("TP_Camera");
      }
      myCamera = tempCamera.GetComponent("TP_Camera")as TP_Camera;
      
      targetLookAt = GameObject.Find("targetLookAt") asGameObject;
      
      if(targetLookAt == null)
      {
         
         targetLookAt = newGameObject("targetLookAt");
         targetLookAt.transform.position =Vector3.zero;
      }
      
      myCamera.TargetLookAt =targetLookAt.transform;
    }
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值