摄像机各种效果

/// <summary>
/// Mouse orbit.
/// This script use to control a main camera
/// </summary>


using UnityEngine;
using System.Collections;


public class MouseOrbit : MonoBehaviour {

[HideInInspector]
public GameObject target; //a target look at
    public float xSpeed; //speed pan x
public float ySpeed; //speed pan y
public float yMinLimit; //y min limit
public float yMaxLimit; //y max limit

    public float scrollSpeed; //scroll speed
public float zoomMin;  //zoom min
public float zoomMax; //zoom max

//Private variable
private float distance;
private float distanceLerp;
private Vector3 position; 
    private bool isActivated;
    private float x;
private float y;
// private bool setupCamera;


    // Use this for initialization
 
    void Start () {


//Warning when not found target
if(target == null)
{
target = GameObject.FindGameObjectWithTag("Player");

if(target == null)
{
Debug.LogWarning("Don't found player tag please change player tag to Player");
}
}

        
//Setup Pos
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
//摄像机的视野范围
CalDistance();
    }
 
 
 
    void LateUpdate () {
 
ScrollMouse();
RotateCamera();
 
}

//Roate camera method
//实现摄像机跟随和相机旋转效果
void RotateCamera()
{
if (Input.GetMouseButtonDown(1)){
 
isActivated = true;
 

 
// if mouse button is let UP then stop rotating camera 
if (Input.GetMouseButtonUp(1))
{
isActivated = false;

 
 
 
   if (target && isActivated) { 

 y -= Input.GetAxis("Mouse Y") * ySpeed;


 x += Input.GetAxis("Mouse X") * xSpeed;


       
 
     y = ClampAngle(y, yMinLimit, yMaxLimit);
 
 
      Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);


        position = rotation * calPos + target.transform.position;

       transform.rotation = rotation;

       transform.position = position;

 
} else
{
Quaternion rotation = Quaternion.Euler(y, x, 0);

Vector3 calPos = new Vector3(0, 0, -distanceLerp);


        position = rotation * calPos + target.transform.position;

 
       transform.rotation = rotation;

       transform.position = position;
}
}

  //Calculate Distance Method
//摄像机的视野范围
  void CalDistance()
{
distance = zoomMax;
distanceLerp = distance;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 calPos = new Vector3(0, 0, -distanceLerp);
   position = rotation * calPos + target.transform.position;
   transform.rotation = rotation;
   transform.position = position;
}

//Scroll Mouse Method
void ScrollMouse()
{
distanceLerp = Mathf.Lerp(distanceLerp,distance,Time.deltaTime * 5);
if (Input.GetAxis("Mouse ScrollWheel") != 0 && !GUI_Menu.instance.CheckHoverItemShop() && !GUI_Menu.instance.CheckHoverSkillWindow()) 
{
// get the distance between camera and target
 
distance = Vector3.Distance (transform.position , target.transform.position);
 
distance = ScrollLimit(distance - Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, zoomMin, zoomMax);
 
}
}

  //Scroll Limit Method
float ScrollLimit(float dist, float min, float max)
    {
        if (dist < min)
 
            dist= min;
 
        if (dist > max)
 
            dist= max; 
 
return dist;
    }


//Clamp Angle Method
float ClampAngle(float angle,float min,float max)
{
if(angle < -360)
angle += 360;
if(angle > 360)
angle -= 360;

return Mathf.Clamp(angle,min,max);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值