• 【unity3D】鼠标控制camera的移动、放大(俯视浏览效果、LOL摄像机移动效果)


    在Unity开发中,用鼠标滚轮来实现摄像机的视野范围,鼠标左键拖拉控制摄像机的移动,代码如下:

     1.俯视浏览效果

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class CameraCol : MonoBehaviour {
     5     //控制视野缩放的速率
     6     public float view_value;
     7     //控制摄像机移动的速率
     8     public float move_speed;
     9     void Update () {
    10         //放大、缩小
    11         if( Input.GetAxis("Mouse ScrollWheel") != 0 )
    12         {
    13             this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*view_value));
    14         }
    15         //移动视角
    16         if (Input.GetMouseButton (2)) {
    17             transform.Translate (Vector3.left * Input.GetAxis ("Mouse X") * move_speed);
    18             transform.Translate(Vector3.up * Input.GetAxis("Mouse Y") * -move_speed);
    19         }
    20     }
    21 }

     2.LOL摄像机效果

      鼠标移至屏幕边控向相应位置移动

     1 //整体移动速度
     2 var speed=5; 
     3 
     4 //关于鼠标滑轮的参数
     5 var MouseWheelSensitivity = 0.0001f; 
     6 var MouseZoomMin = -2.4f; 
     7 var MouseZoomMax = 1.0f;
     8 var normalDistance = -1.1; 
     9 
    10 //水平和垂直的移动速度
    11 var horizontalMoveSpeed = 0.1; 
    12 var verticalMoveSpeed = 0.1; 
    13 
    14 //上左下右的标记
    15 var topTag = 8; 
    16 var leftTag = 4; 
    17 var botTag = 2; 
    18 var rightTag = 1;
    19 
    20 function Start () {
    21     
    22 }
    23 
    24 function Update () {
    25     if( Input.GetAxis("Mouse ScrollWheel") != 0 )
    26         {
    27             this.gameObject.transform.Translate(new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*500));
    28         }
    29     //获取cursor坐标
    30     var msPos = Input.mousePosition;
    31     
    32     //边界最小值
    33     var widthBorder = Screen.width/50; 
    34     var heightBorder = Screen.height/50; 
    35     
    36     var x = 0.0f; 
    37     var y = 0.0f; 
    38     
    39     //当前鼠标位置标记
    40     var posTag = 0; 
    41     
    42     if(widthBorder<=msPos.x && msPos.x<=Screen.width-widthBorder &&
    43         heightBorder<=msPos.y && msPos.y<=Screen.height-heightBorder)
    44     {
    45         transform.Translate(x,y,y); 
    46         Debug.Log("asd"+msPos.x+" "+msPos.y);
    47     }
    48     else 
    49     {
    50     //  posTag
    51     //
    52     // 1100 | 1000 | 1001
    53     // 0100 | 0000 | 0001
    54     // 0110 | 0010 | 0011
    55     //
    56     // 12   |  8   |  9
    57     // 4    |  0   |  1
    58     // 6    |  2   |  3
    59     //
    60         if(msPos.y>Screen.height-heightBorder)
    61             posTag = posTag | topTag; 
    62         if(msPos.x<widthBorder)
    63             posTag = posTag | leftTag;
    64         if(msPos.y<heightBorder) 
    65             posTag = posTag | botTag;
    66         if(msPos.x>Screen.width-widthBorder)
    67             posTag = posTag | rightTag;
    68             
    69         switch(posTag)
    70         {
    71         case 0: break; 
    72         case 1: x = horizontalMoveSpeed; break; 
    73         case 2: y = -verticalMoveSpeed; break; 
    74         case 3: x = horizontalMoveSpeed; y = -verticalMoveSpeed; break; 
    75         case 4: x = -horizontalMoveSpeed; break;
    76         case 6: x = -horizontalMoveSpeed; y = -verticalMoveSpeed; break;
    77         case 8: y = verticalMoveSpeed; break;
    78         case 9: x = horizontalMoveSpeed; y = verticalMoveSpeed; break;
    79         case 12:x = -horizontalMoveSpeed;y = verticalMoveSpeed; break;
    80         default: break; 
    81         }
    82         
    83         x *= speed*Time.deltaTime;
    84         y *= speed*Time.deltaTime;
    85         
    86         //
    87         transform.Translate(x, y, y); 
    88     }
    89         
    90     
    91 }
  • 相关阅读:
    Python MongoDB使用介绍
    算法网站
    无限级树状图css实现
    无限级别分类嵌套格式抓取
    无限级别分类
    计算多维数组到底是几维的
    获取无限级别分类
    mysql 重启
    radio 控制器function用法
    php-fpm 重启 nginx单独配置 重启
  • 原文地址:https://www.cnblogs.com/zhengbin/p/4552799.html
Copyright © 2020-2023  润新知