在Unity开发中,用鼠标滚轮来实现摄像机的视野范围,鼠标左键拖拉控制摄像机的移动,代码如下:
1.俯视浏览效果
using UnityEngine;
using System.Collections; public class CameraCol : MonoBehaviour {
//控制视野缩放的速率
public float view_value;
//控制摄像机移动的速率
public float move_speed;
void Update () {
//放大、缩小
if( Input.GetAxis("Mouse ScrollWheel") != )
{
this.gameObject.transform.Translate(new Vector3(,,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*view_value));
}
//移动视角
if (Input.GetMouseButton ()) {
transform.Translate (Vector3.left * Input.GetAxis ("Mouse X") * move_speed);
transform.Translate(Vector3.up * Input.GetAxis("Mouse Y") * -move_speed);
}
}
}
2.LOL摄像机效果
鼠标移至屏幕边控向相应位置移动
//整体移动速度
var speed=; //关于鼠标滑轮的参数
var MouseWheelSensitivity = 0.0001f;
var MouseZoomMin = -2.4f;
var MouseZoomMax = 1.0f;
var normalDistance = -1.1; //水平和垂直的移动速度
var horizontalMoveSpeed = 0.1;
var verticalMoveSpeed = 0.1; //上左下右的标记
var topTag = ;
var leftTag = ;
var botTag = ;
var rightTag = ; function Start () { } function Update () {
if( Input.GetAxis("Mouse ScrollWheel") != )
{
this.gameObject.transform.Translate(new Vector3(,,Input.GetAxis("Mouse ScrollWheel")*Time.deltaTime*));
}
//获取cursor坐标
var msPos = Input.mousePosition; //边界最小值
var widthBorder = Screen.width/;
var heightBorder = Screen.height/; var x = 0.0f;
var y = 0.0f; //当前鼠标位置标记
var posTag = ; if(widthBorder<=msPos.x && msPos.x<=Screen.width-widthBorder &&
heightBorder<=msPos.y && msPos.y<=Screen.height-heightBorder)
{
transform.Translate(x,y,y);
Debug.Log("asd"+msPos.x+" "+msPos.y);
}
else
{
// posTag
//
// 1100 | 1000 | 1001
// 0100 | 0000 | 0001
// 0110 | 0010 | 0011
//
// 12 | 8 | 9
// 4 | 0 | 1
// 6 | 2 | 3
//
if(msPos.y>Screen.height-heightBorder)
posTag = posTag | topTag;
if(msPos.x<widthBorder)
posTag = posTag | leftTag;
if(msPos.y<heightBorder)
posTag = posTag | botTag;
if(msPos.x>Screen.width-widthBorder)
posTag = posTag | rightTag; switch(posTag)
{
case : break;
case : x = horizontalMoveSpeed; break;
case : y = -verticalMoveSpeed; break;
case : x = horizontalMoveSpeed; y = -verticalMoveSpeed; break;
case : x = -horizontalMoveSpeed; break;
case : x = -horizontalMoveSpeed; y = -verticalMoveSpeed; break;
case : y = verticalMoveSpeed; break;
case : x = horizontalMoveSpeed; y = verticalMoveSpeed; break;
case :x = -horizontalMoveSpeed;y = verticalMoveSpeed; break;
default: break;
} x *= speed*Time.deltaTime;
y *= speed*Time.deltaTime; //
transform.Translate(x, y, y);
} }