167 lines
4.9 KiB
C#
167 lines
4.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using System.Collections;
|
|
|
|
public class mouseMove : MonoBehaviour
|
|
{
|
|
public Transform target;
|
|
public float xSpeed = 200;
|
|
public float ySpeed = 200;
|
|
public float mSpeed = 10;
|
|
public float yMinLimit = -50;
|
|
public float yMaxLimit = 50;
|
|
|
|
public float distance = 2;
|
|
public float minDistance = 2;
|
|
public float maxDistance = 30;
|
|
|
|
public bool OnUI = false;
|
|
public bool OnOffUI = false;
|
|
|
|
public bool needDamping = false;
|
|
public float damping = 5.0f;
|
|
|
|
public float x = 0.0f;
|
|
public float y = 0.0f;
|
|
|
|
private Vector3 initialAngles;
|
|
|
|
void Start()
|
|
{
|
|
initialAngles = transform.eulerAngles;
|
|
x = initialAngles.y;
|
|
y = initialAngles.x;
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
onoffUI();
|
|
}
|
|
|
|
void onoffUI()
|
|
{
|
|
if (EventSystem.current.IsPointerOverGameObject() && OnOffUI)
|
|
{
|
|
OnUI = true;
|
|
}
|
|
else
|
|
{
|
|
OnUI = false;
|
|
Rotate();
|
|
}
|
|
}
|
|
|
|
void Rotate()
|
|
{
|
|
if (target == null) return;
|
|
|
|
bool inputDetected = false;
|
|
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
// Mouse input for rotation
|
|
x += Input.GetAxis("Mouse X") * xSpeed * .02f;
|
|
y -= Input.GetAxis("Mouse Y") * ySpeed * .02f;
|
|
inputDetected = true;
|
|
}
|
|
else if (Input.touchCount > 0)
|
|
{
|
|
Touch touch0 = Input.GetTouch(0);
|
|
if (Input.touchCount > 1)
|
|
{
|
|
Touch touch1 = Input.GetTouch(1);
|
|
// Handle rotation using two fingers
|
|
if (touch0.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved)
|
|
{
|
|
Vector2 touch0PrevPos = touch0.position - touch0.deltaPosition;
|
|
Vector2 touch1PrevPos = touch1.position - touch1.deltaPosition;
|
|
float prevTouchDeltaMag = (touch0PrevPos - touch1PrevPos).magnitude;
|
|
float touchDeltaMag = (touch0.position - touch1.position).magnitude;
|
|
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
|
|
|
|
// Adjust distance based on pinch gesture
|
|
distance += deltaMagnitudeDiff * 0.01f;
|
|
distance = Mathf.Clamp(distance, minDistance, maxDistance);
|
|
|
|
inputDetected = true;
|
|
}
|
|
// Handle rotation using one finger
|
|
else if (touch0.phase == TouchPhase.Moved)
|
|
{
|
|
x += touch0.deltaPosition.x * xSpeed ;
|
|
y -= touch0.deltaPosition.y * ySpeed ;
|
|
inputDetected = true;
|
|
}
|
|
}
|
|
else if (touch0.phase == TouchPhase.Moved)
|
|
{
|
|
x += touch0.deltaPosition.x * xSpeed ;
|
|
y -= touch0.deltaPosition.y * ySpeed ;
|
|
inputDetected = true;
|
|
}
|
|
}
|
|
|
|
if (inputDetected)
|
|
{
|
|
y = ClampAngle(y, yMinLimit, yMaxLimit);
|
|
}
|
|
|
|
// Adjust camera distance based on mouse scroll wheel or touch pinch
|
|
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
|
if (scrollInput != 0)
|
|
{
|
|
distance -= scrollInput * mSpeed;
|
|
distance = Mathf.Clamp(distance, minDistance, maxDistance);
|
|
}
|
|
|
|
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
|
|
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
|
|
Vector3 position = rotation * disVector + target.position;
|
|
|
|
if (needDamping)
|
|
{
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
|
|
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
|
|
}
|
|
else
|
|
{
|
|
transform.rotation = rotation;
|
|
transform.position = position;
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
public void MoveToCamPos(Transform camPos)
|
|
{
|
|
StartCoroutine(MoveToPosition(camPos));
|
|
}
|
|
|
|
private IEnumerator MoveToPosition(Transform camPos)
|
|
{
|
|
Vector3 startPosition = transform.position;
|
|
Quaternion startRotation = transform.rotation;
|
|
Vector3 endPosition = camPos.position;
|
|
Quaternion endRotation = camPos.rotation;
|
|
float duration = 1.0f;
|
|
float elapsedTime = 0.0f;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
transform.position = Vector3.Lerp(startPosition, endPosition, elapsedTime / duration);
|
|
transform.rotation = Quaternion.Lerp(startRotation, endRotation, elapsedTime / duration);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.position = endPosition;
|
|
transform.rotation = endRotation;
|
|
}
|
|
}
|