码迷,mamicode.com
首页 > 编程语言 > 详细

Unity实现游戏中摄像机视角控制

时间:2016-08-15 12:49:33      阅读:1005      评论:0      收藏:0      [点我收藏+]

标签:

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实现游戏中摄像机视角控制

标签:

原文地址:http://www.cnblogs.com/wowanyasuo/p/5772510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!