blob: e2f91048e2e1ef21eac7a790754e4b4186a822ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
using UnityEngine;
namespace Assets.MaterializeFX.Scripts.Utils
{
internal sealed class MouseOrbitController : MonoBehaviour
{
public Transform Target;
public float Distance = 5.0f;
public float XSpeed = 120.0f;
public float YSpeed = 120.0f;
public float YMinLimit = 20f;
public float YMaxLimit = 80f;
public float DistanceMin = .5f;
public float DistanceMax = 15f;
private float _x;
private float _y;
private void Start()
{
var angles = transform.eulerAngles;
_x = angles.y;
_y = angles.x;
}
private void LateUpdate()
{
if (!Input.GetMouseButton(0))
return;
_x += Input.GetAxis("Mouse X") * XSpeed * Distance * 0.02f;
_y -= Input.GetAxis("Mouse Y") * YSpeed * 0.02f;
_y = ClampAngle(_y, YMinLimit, YMaxLimit);
var rotation = Quaternion.Euler(_y, _x, 0);
Distance -= Input.GetAxis("Mouse ScrollWheel") * 5;
var negDistance = new Vector3(0.0f, 0.0f, -Distance);
var position = rotation * negDistance + Target.position;
transform.rotation = rotation;
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
}
|