Unity實現游戲中攝像機視角控制


using UnityEngine;
using System.Collections;

public class NewCamera : MonoBehaviour {

	public GameObject cameraFather;
	public Transform target;
	public float rotatespeed;
	public float scalespeed;
	public float min;
	public float max;
	public Transform obj;
	public float cameraSpeed;

	private bool isLock =false;
	private Vector3 fCurrentRotation;
	private float currentSacale;
	private Vector3 currentRotation;
	private Vector3 lastMousePosition;
	// Use this for initialization
	void Start () {
		fCurrentRotation = cameraFather.transform.eulerAngles;
		currentRotation = transform.eulerAngles;
		lastMousePosition = Input.mousePosition;
	}

	// Update is called once per frame
	void Update() {
		if (obj != null && Input.GetKeyDown (KeyCode.Space) && isLock == false) {
			isLock = true;
		} else if (Input.GetKeyDown (KeyCode.Space)) {
			isLock = false;
		}
		if (isLock) {
			cameraFather.transform.position = obj.position;
		} else if(!Input.GetMouseButton(0)){
			if (Input.mousePosition.x <= Screen.width && Input.mousePosition.x >= Screen.width - 60) {
				cameraFather.transform.Translate (new Vector3(1,0,0)*cameraSpeed*Time .deltaTime);
			}else if(Input.mousePosition.x >= 0 && Input.mousePosition.x <= 60){
				cameraFather.transform.Translate (new Vector3(1,0,0)*-cameraSpeed*Time .deltaTime);
			}
			if(Input.mousePosition.y >= 0 && Input.mousePosition.y <= 60){
				cameraFather.transform.Translate (new Vector3(0,0,1)*-cameraSpeed*Time .deltaTime);
			}else if(Input.mousePosition.y >= Screen.height-60 && Input.mousePosition.y <= Screen.height){
				cameraFather.transform.Translate (new Vector3(0,0,1)*cameraSpeed*Time .deltaTime);
			}
		}


		currentSacale = 0;
		//transform.LookAt (camerachild.position);
		Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
		lastMousePosition = Input.mousePosition;
		if (Input.GetMouseButton(0)) {
			fCurrentRotation.y += mouseDelta.x * rotatespeed * Time.deltaTime;
			currentRotation.x += mouseDelta.y * rotatespeed * Time.deltaTime;
			currentRotation.y += mouseDelta.x * rotatespeed * Time.deltaTime;
		}
		currentSacale = Input.mouseScrollDelta.y * scalespeed * Time.deltaTime;
		if(currentSacale != 0)
			currentSacale = Mathf.Clamp(currentSacale,min,max);
		Debug.Log (currentSacale);
		transform.position = target.position;
		cameraFather.transform.eulerAngles = fCurrentRotation;
		transform.eulerAngles = currentRotation;
		transform.Translate (new Vector3(0,0,currentSacale));
	}
}

 

1.創建一個空物體,將它(GameObject)拉到攝像機要觀察的對象文件夾里面,對象就是類似英雄聯盟的英雄。
2.將Transform欄的Position清0;
3.再將空物體拉出來放到與攝像機文件夾下,將Transform欄的Rotation清0;
4.將空物體拉出來到攝像機文件夾同層,將空物體Transform欄的Rotation的 X 清0;
5.將攝像機拉到空物體文件夾下。
6.把NewCamera.cs 添加到攝像機。
7.將空物體拉到CameraFather選項,將攝像機拉到Target,Rotateseed是旋轉速度,自己輸入一個正數,Scalespeed滾軸帶動攝像機移動速度,Min和Max都是Scalespeed的大小限制,Min一般是負值,Max一般取正值。(小於取Min值大於取Max值)。把觀察的對象拉到Obj選項。Cameraspeed是鼠標靠近屏幕邊緣的移動速度。

按鍵說明
按Space(空格)鍵鎖定視角
按住鼠標左鍵拖動旋轉視角
滾軸改變攝像機和物體距離

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM