zoukankan      html  css  js  c++  java
  • 【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 }

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

  • 相关阅读:
    case when in sql server's stored procedure
    Hadoop-2.2.0中国文献—— MapReduce 下一代 -- 公平调度
    cocos2d-x游戏循环和日程安排
    归并+高速分拣
    【Nginx】启动过程
    IOS线程操作(3)
    Android最方便的数据库--LitePal
    Android采用Application总结一下
    POJ 2151 Check the difficulty of problems (动态规划-可能DP)
    乞讨N!到底有多少0
  • 原文地址:https://www.cnblogs.com/reachteam/p/4229740.html
Copyright © 2011-2022 走看看