纳金网
标题:
用鼠标调节游戏中摄像机的远近,以及摄像机的视角移动
[打印本页]
作者:
烟雨
时间:
2015-6-25 08:08
标题:
用鼠标调节游戏中摄像机的远近,以及摄像机的视角移动
public class CameraMove : MonoBehaviour {
public float minFov = 15f;
public float maxFov = 90f;
public float sensitivity = 10f;
private float speed = 400;//摄像机移动速度
//水平和垂直移动速度
private float horizontalMoveSpeed = 0.1f;
private float verticalMoveSpeed = 0.1f;
//屏幕上下左右标记
private int topTag = 8;
private int leftTag = 4;
private int rightTag = 1;
private int bottonTag = 2;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
transform.position -= Vector3.forward * vertical;
transform.position -= Vector3.right * horizontal;
//视野缩进
float fov = Camera.main.fieldOfView;
fov += -Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
Vector3 mousePos = Input.mousePosition;//获的当前鼠标坐标
//边界最小值
float widthBorder = Screen.width / 50;
float heightBorder = Screen.width / 50;
float x = 0f;
float y = 0f;
//当前鼠标位置标记
int posTag = 0;
if (widthBorder <= mousePos.x && mousePos.x <= Screen.width - widthBorder &&
heightBorder <= mousePos.y && mousePos.y <= Screen.height - heightBorder)
{
transform.Translate(x, y, y);
}
else
{
if (mousePos.y > Screen.height - heightBorder)
posTag = posTag | topTag;
if (mousePos.x < widthBorder)
posTag = posTag | leftTag;
if (mousePos.y < heightBorder)
posTag = posTag | bottonTag;
if (mousePos.x > Screen.width - widthBorder)
posTag = posTag | rightTag;
// posTag
//
// 1100 | 1000 | 1001
// 0100 | 0000 | 0001
// 0110 | 0010 | 0011
//
// 12 | 8 | 9
// 4 | 0 | 1
// 6 | 2 | 3
//
switch (posTag)
{
case 0:
break;
case 1:
x = horizontalMoveSpeed;
break;
case 2:
y = -verticalMoveSpeed;
break;
case 3:
x = horizontalMoveSpeed;
y = -verticalMoveSpeed;
break;
case 4:
x = -horizontalMoveSpeed;
break;
case 6:
x = -horizontalMoveSpeed;
y = -verticalMoveSpeed;
break;
case 8:
y = verticalMoveSpeed;
break;
case 9:
x = horizontalMoveSpeed;
y = verticalMoveSpeed;
break;
case 12:
x = -horizontalMoveSpeed;
y = verticalMoveSpeed;
break;
default:
break;
}
x *= speed * Time.deltaTime;
y *= speed * Time.deltaTime;
transform.Translate(x, y, y);
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5