【Unity3D】使用鼠标键盘控制Camera视角(即时战略类游戏视角):缩近,拉远,旋转...

今天写一个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 }

 

直接把脚本拖到摄像机上就可以使用了~

转载于:https://www.cnblogs.com/reachteam/p/4229740.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值