unity3D實現鏡頭拉近拉遠及視角旋轉

鏡頭拉近拉遠的代碼(無限拉遠拉近)

 

代碼如下:

 

if( Input.GetAxis("Mouse ScrollWheel") != 0 )

{

this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*500));

}

 

上述代碼放在Update ()函數中,其中gameObject是攝像機或者物體對象,500是可以調節的參數。方法非常簡單。

 

 

上述方法雖然能實現,但是太簡單下面來個完整代碼如下(只要把這個腳本綁定到相機上就OK)

實現:右鍵轉動視角和鏡頭拉伸(拉動有範圍)

 

/// <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);
	}
}

 

上面的代碼只能限定上下的視角,所以我改進了一下,下面的代碼可以進行左右和上下視角的範圍限定。如下:

/// <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 XMinLimit; //X軸的限制
    public float XMaxLimit;

    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();
        if (x < XMinLimit)
        {
            x = XMinLimit;
        }

        if (x > XMaxLimit)
        {
            x = XMaxLimit;
        }
    }
	
	//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);
    //}

    float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章