You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOrbit : MonoBehaviour
{
public float distance; // 摄像头与指定点的距离
public float xSpeed = 120.0f; // 水平旋转速度
public float ySpeed = 120.0f; // 垂直旋转速度
public bool canScroll = true;
private float x = 0.0f;
private float y = 0.0f;
private float z = 1.0f;
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
distance = Vector3.Distance(transform.position, new Vector3(0, z, 0));
}
void LateUpdate()
{
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + new Vector3(0, z, 0);
transform.rotation = rotation;
transform.position = position;
}
else if (Input.GetAxis("Mouse ScrollWheel") != 0 && canScroll)
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 100)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize += 0.5F;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView > 2)
Camera.main.fieldOfView -= 2;
if (Camera.main.orthographicSize >= 1)
Camera.main.orthographicSize -= 0.5F;
}
}
else if (Input.GetMouseButton(0))
{
var d = Input.GetAxis("Mouse Y") * ySpeed * 0.0005f;
z -= d;
Vector3 position = transform.position;
position.y -= d;
transform.position = position;
}
}
}