Unity3D键盘+鼠标漫游脚本

两个代码都附在MainCamera上面,可以实现键盘加鼠标漫游


1、KeyMove.cs  键盘漫游

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class KeyMove : MonoBehaviour {  
  5.   
  6.     void Start () {  
  7.   
  8.     }  
  9.     void Update () {  
  10.         //前后左右  
  11.         if (Input.GetKey (KeyCode.A)) {  
  12.             transform.Translate (-25 * Time.deltaTime,0, 0 , Space.Self);  
  13.         }  
  14.         if (Input.GetKey (KeyCode.D)) {  
  15.             transform.Translate ( 25 * Time.deltaTime,0, 0 , Space.Self);  
  16.         }  
  17.         if (Input.GetKey (KeyCode.W)) {  
  18.             transform.Translate (0, 0, 25 * Time.deltaTime, Space.Self);  
  19.         }  
  20.         if (Input.GetKey (KeyCode.S)) {  
  21.             transform.Translate (0, 0 , -25 * Time.deltaTime,Space.Self);  
  22.         }  
  23.         //旋转  
  24.         if (Input.GetKey (KeyCode.Q)) {  
  25.             transform.Rotate (0, -25 * Time.deltaTime, 0, Space.Self);  
  26.         }  
  27.         if (Input.GetKey (KeyCode.E)) {  
  28.             transform.Rotate (0, 25 * Time.deltaTime, 0, Space.Self);  
  29.         }  
  30.         if (Input.GetKey (KeyCode.Z)) {  
  31.             transform.Rotate (-25 * Time.deltaTime,0, 0 , Space.Self);  
  32.         }  
  33.         if (Input.GetKey (KeyCode.C)) {  
  34.             transform.Rotate ( 25 * Time.deltaTime,0, 0 , Space.Self);  
  35.         }  
  36.         //升高降低镜头  
  37.         if (Input.GetKey (KeyCode.H)) {  
  38.             transform.Translate (0, 5 * Time.deltaTime, 0);  
  39.         }  
  40.         if (Input.GetKey (KeyCode.N)) {  
  41.             transform.Translate (0, -5 * Time.deltaTime, 0);  
  42.         }  
  43.     }  
  44. }  

2、【转发】鼠标漫游

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class FlyMove : MonoBehaviour {  
  5.     private float sensitivityX = 1F;        //X转动增量速度  
  6.     private float sensitivityY = 1F;        //y转动增量速度  
  7.     private float minimumY = -90F;          //Y轴转动限制  
  8.     private float maximumY = 90F;  
  9.     float rotationY = 0F;                   //y起始值  
  10.     private float MovingSpeed = 1f;         //移动屏幕的速度  
  11.     float delta_x,delta_y,delta_z;          //计算移动量  
  12.     float distance = 5;                       
  13.     float ZoomSpeed = 20f;                  //拉近拉远速度  
  14.     Quaternion rotation;  
  15.   
  16.     void Start () {  
  17.   
  18.     }  
  19.   
  20.     void Update () {  
  21.         if(Input.GetMouseButton(0)){//左键旋转屏幕  
  22.             {  
  23.                 float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;  
  24.   
  25.                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;  
  26.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);  
  27.   
  28.                 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);  
  29.             }  
  30.   
  31.         }  
  32.         if (Input.GetMouseButton(2)){  
  33.   
  34.         }  
  35.         if(Input.GetAxis("Mouse ScrollWheel")!= 0){//滚轴拉近拉远  
  36.             delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;  
  37.             transform.Translate(0,0,-delta_z);  
  38.             distance += delta_z;  
  39.         }  
  40.         if (Input.GetMouseButton (2)) {//滚轴中间移动屏幕  
  41.             delta_x = Input.GetAxis("Mouse X") * MovingSpeed;  
  42.             delta_y = Input.GetAxis("Mouse Y") * MovingSpeed;  
  43.             rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y,0 );  
  44.             transform.position =rotation * new Vector3(-delta_x,-delta_y,0)+transform.position;  
  45.         }  
  46.     }  
  47. }  

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值