123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using UnityEngine;
- using System.Collections;
- public class BFX_MouseOrbit : MonoBehaviour
- {
- public GameObject target;
- public float distance = 10.0f;
- public float xSpeed = 250.0f;
- public float ySpeed = 120.0f;
- public float yMinLimit = -20;
- public float yMaxLimit = 80;
- float x = 0.0f;
- float y = 0.0f;
- void Start()
- {
- var angles = transform.eulerAngles;
- x = angles.y;
- y = angles.x;
- }
- float prevDistance;
- void LateUpdate()
- {
- if (distance < 2) distance = 2;
- distance -= Input.GetAxis("Mouse ScrollWheel")*2;
- if (target && (Input.GetMouseButton(1)))
- {
- var pos = Input.mousePosition;
- var dpiScale = 1f;
- if (Screen.dpi < 1) dpiScale = 1;
- if (Screen.dpi < 200) dpiScale = 1;
- else dpiScale = Screen.dpi/200f;
- if (pos.x < 380*dpiScale && Screen.height - pos.y < 250*dpiScale) return;
- Cursor.visible = false;
- Cursor.lockState = CursorLockMode.Locked;
- x += Input.GetAxis("Mouse X")*xSpeed*0.02f;
- y -= Input.GetAxis("Mouse Y")*ySpeed*0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- var rotation = Quaternion.Euler(y, x, 0);
- var position = rotation* new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- else
- {
- Cursor.visible = true;
- Cursor.lockState = CursorLockMode.None;
- //Cursor.visible = true;
- //Screen.lockCursor = false;
- }
- if(Input.GetKey(KeyCode.RightArrow))
- {
- if (Time.timeScale < 0.5) x += 1f / 60f * 100;
- else x += Time.deltaTime * 3;
- var rotation = Quaternion.Euler(y, x, 0);
- var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- if (Input.GetKey(KeyCode.LeftArrow))
- {
- if(Time.timeScale < 0.5) x -= 1f / 60f * 100;
- else x -= Time.deltaTime * 3;
- var rotation = Quaternion.Euler(y, x, 0);
- var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- if (Time.timeScale > 0.5) Time.timeScale = 0;
- else Time.timeScale = 1;
- }
- if (Math.Abs(prevDistance - distance) > 0.001f)
- {
- prevDistance = distance;
- var rot = Quaternion.Euler(y, x, 0);
- var po = rot*new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
- transform.rotation = rot;
- transform.position = po;
- }
- }
- 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);
- }
- }
|