94 lines
2.2 KiB
C#
94 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
//上帝视角摄像机
|
|
|
|
public class GodCamera : MonoBehaviour
|
|
{
|
|
|
|
//相机
|
|
Camera godCamera;
|
|
//初始位置
|
|
public Vector3 startPosition = new Vector3(0f, 0f, 0f);
|
|
//最大x
|
|
public float maxPosX = 100f;
|
|
//最小x
|
|
public float minPosX = -100f;
|
|
//最大y
|
|
public float maxPosY = 50f;
|
|
//最小y
|
|
public float minPosY = 10f;
|
|
//最大z
|
|
public float maxPosZ = 100f;
|
|
//最小z
|
|
public float minPosZ = -100f;
|
|
|
|
|
|
//速度
|
|
public float speed = 20;
|
|
|
|
|
|
|
|
|
|
//bool isGameStart = true;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
godCamera = transform.GetComponent<Camera>();
|
|
transform.position = startPosition;
|
|
godCamera.fieldOfView = 60;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
//移动
|
|
Move();
|
|
//视野缩放
|
|
ScaleView();
|
|
|
|
}
|
|
private void Move()
|
|
{
|
|
//当鼠标左键按下的时候,0表示鼠标左键
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
var x = Input.GetAxis("Mouse X");//鼠标在两帧之间的水平偏移量
|
|
var y = Input.GetAxis("Mouse Y");//竖直的偏移量
|
|
if (x != 0 || y != 0)
|
|
{
|
|
Vector3 target = this.transform.position + new Vector3(-y, 0, x) * speed;
|
|
if (target.x > maxPosX || target.x < minPosX || target.z > maxPosZ || target.z < minPosZ)
|
|
{
|
|
// 如果超出范围,则不更新位置
|
|
return;
|
|
}
|
|
this.transform.position = Vector3.Lerp(this.transform.position, target, speed * Time.deltaTime);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ScaleView()
|
|
{
|
|
//获取到鼠标滚轮的值
|
|
float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
|
|
if (mouseScrollWheel != 0)
|
|
{
|
|
godCamera.fieldOfView += mouseScrollWheel * speed;
|
|
if (godCamera.fieldOfView <= 38)
|
|
{
|
|
godCamera.fieldOfView = 38;
|
|
}
|
|
else if (godCamera.fieldOfView >= 95)
|
|
{
|
|
godCamera.fieldOfView = 95;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|