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.

76 lines
2.2 KiB

2 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// modyified from https://blog.csdn.net/q764424567/article/details/90238382 2023.03.13
public class MouseControlModel : MonoBehaviour
{
public int yMinLimit = -20;
public int yMaxLimit = 80;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
private float x = 0.0f;
private float y = 0.0f;
Vector3 previousPos;
bool isMouseDown = false;
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 30));
if (!isMouseDown)
{
isMouseDown = true;
}
else
{
transform.position += (pos - previousPos);
}
previousPos = pos;
}
else if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
else if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
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
{
isMouseDown = false;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}