标签:
今天写一个demo,要用到鼠标键盘控制三维视角,因此写了个脚本用于控制。
该脚本可以用于即时战略类游戏的视角,提供了缩进,拉伸,旋转。同时按住鼠标右键不放,移动鼠标可以实现第一人称视角的效果。
1 using UnityEngine; 2 using System.Collections; 3 4 public class CameraController : MonoBehaviour { 5 6 7 public float near = 20.0f; 8 public float far = 100.0f; 9 10 public float sensitivityX = 10f; 11 public float sensitivityY = 10f; 12 public float sensitivetyZ = 2f; 13 public float sensitivetyMove = 2f; 14 public float sensitivetyMouseWheel = 2f; 15 16 17 void Update () { 18 // 滚轮实现镜头缩进和拉远 19 if (Input.GetAxis("Mouse ScrollWheel") != 0) 20 { 21 this.camera.fieldOfView =this.camera.fieldOfView - Input.GetAxis("Mouse ScrollWheel")*sensitivetyMouseWheel; 22 this.camera.fieldOfView = Mathf.Clamp(this.camera.fieldOfView, near, far); 23 } 24 //鼠标右键实现视角转动,类似第一人称视角 25 if (Input.GetMouseButton(1)) 26 { 27 float rotationX = Input.GetAxis("Mouse X") * sensitivityX; 28 float rotationY = Input.GetAxis("Mouse Y") * sensitivityY; 29 transform.Rotate(-rotationY, rotationX, 0); 30 } 31 32 //键盘按钮←和→实现视角水平旋转 33 if (Input.GetAxis("Horizontal")!=0) 34 { 35 float rotationZ=Input.GetAxis("Horizontal") * sensitivetyZ; 36 transform.Rotate(0, 0, rotationZ); 37 } 38 } 39 }
直接把脚本拖到摄像机上就可以使用了~
【Unity3D】使用鼠标键盘控制Camera视角(即时战略类游戏视角):缩进,拉远,旋转
标签:
原文地址:http://www.cnblogs.com/reachteam/p/4229740.html