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(空格)键锁定视角
按住鼠标左键拖动旋转视角
滚轴改变摄像机和物体距离

 

Unity实现游戏中摄像机视角控制_第1张图片Unity实现游戏中摄像机视角控制_第2张图片Unity实现游戏中摄像机视角控制_第3张图片Unity实现游戏中摄像机视角控制_第4张图片Unity实现游戏中摄像机视角控制_第5张图片Unity实现游戏中摄像机视角控制_第6张图片Unity实现游戏中摄像机视角控制_第7张图片Unity实现游戏中摄像机视角控制_第8张图片Unity实现游戏中摄像机视角控制_第9张图片Unity实现游戏中摄像机视角控制_第10张图片Unity实现游戏中摄像机视角控制_第11张图片Unity实现游戏中摄像机视角控制_第12张图片Unity实现游戏中摄像机视角控制_第13张图片Unity实现游戏中摄像机视角控制_第14张图片

转载于:https://www.cnblogs.com/wowanyasuo/p/5772510.html

你可能感兴趣的:(Unity实现游戏中摄像机视角控制)