123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CheckCanvas : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
-
- }
- //上下旋转最大角度限制
- 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;
- //缩放比例限制
- public float MinScale = 0.2f;
- public float MaxScale = 3.0f;
- //缩放比例
- private float scale = 1.0f;
- void Update()
- {
- if (Input.GetMouseButton(0))
- {
- //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
- 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;
- }
- if (Input.GetAxis("Mouse ScrollWheel") != 0)
- {
- scale += Input.GetAxis("Mouse ScrollWheel");
- scale = Mathf.Clamp(scale, MinScale, MaxScale);
- transform.localScale = new Vector3(scale, scale, scale);
- }
- }
- //角度范围值限定
- 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);
- }
- }
|