105 lines
3.5 KiB
C#
105 lines
3.5 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class AquariumCameraMove : MonoBehaviour
|
|
{
|
|
int layerMask;
|
|
|
|
//屏幕拖动比例
|
|
float dragCameraScale = 0.03f;
|
|
private Vector3 initialRotation;
|
|
Vector3 _curMousePos;
|
|
Vector3 _offsetEuler;
|
|
Quaternion targetRotation;
|
|
Vector2 cameraOffset;
|
|
Quaternion startRot;
|
|
float duration = 0.65f;
|
|
FishingAquariumAct fishingAquariumAct;
|
|
GameObject curClick;
|
|
private void Awake()
|
|
{
|
|
layerMask = 1 << LayerMask.NameToLayer("fish");
|
|
startRot = transform.rotation;
|
|
}
|
|
public void Init(Vector2 cameraOffset, FishingAquariumAct fishingAquariumAct)
|
|
{
|
|
this.fishingAquariumAct = fishingAquariumAct;
|
|
this.cameraOffset = cameraOffset;
|
|
}
|
|
|
|
float CheckAngle(float value)
|
|
{
|
|
float angle = value - 180;
|
|
|
|
if (angle > 0)
|
|
return angle - 180;
|
|
|
|
return angle + 180;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (Input.GetMouseButtonDown(0) && fishingAquariumAct.CanDragMove())
|
|
{
|
|
transform.DOKill();
|
|
_curMousePos = Input.mousePosition;
|
|
_offsetEuler = Vector3.zero;
|
|
initialRotation = transform.rotation.eulerAngles;
|
|
initialRotation.x = CheckAngle(initialRotation.x);
|
|
initialRotation.y = CheckAngle(initialRotation.y);
|
|
targetRotation = transform.rotation;
|
|
curClick = SelectTarget();
|
|
}
|
|
else if (Input.GetMouseButton(0) && fishingAquariumAct.CanDragMove())
|
|
{
|
|
Vector3 ver = Input.mousePosition - _curMousePos;
|
|
if (ver.magnitude > 5)
|
|
{
|
|
_offsetEuler.x -= ver.y * dragCameraScale;
|
|
_offsetEuler.y += ver.x * dragCameraScale;
|
|
var targetEulerAngles = initialRotation + _offsetEuler;
|
|
|
|
// Clamp the rotation angles
|
|
targetEulerAngles.x = Mathf.Clamp(targetEulerAngles.x, -cameraOffset.x, cameraOffset.x);
|
|
targetEulerAngles.y = Mathf.Clamp(targetEulerAngles.y, -cameraOffset.y, cameraOffset.y);
|
|
targetEulerAngles.z = 0;
|
|
_offsetEuler = targetEulerAngles - initialRotation;
|
|
|
|
targetRotation = Quaternion.Euler(targetEulerAngles);
|
|
//transform.rotation = targetRotation;
|
|
_curMousePos = Input.mousePosition;
|
|
}
|
|
transform.rotation = Quaternion.Angle(targetRotation, transform.rotation) > 0.1f
|
|
? Quaternion.Lerp(transform.rotation, targetRotation, 0.2f)
|
|
: targetRotation;
|
|
}
|
|
else if (Input.GetMouseButtonUp(0))
|
|
{
|
|
transform.DORotateQuaternion(startRot, duration);
|
|
GameObject localClick = SelectTarget();
|
|
if (localClick != null && localClick == curClick)
|
|
{
|
|
var fish = curClick.GetComponentInParent<AquariumFishBase>();
|
|
fishingAquariumAct.OnClickFish(fish.slotData.index);
|
|
curClick = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
private GameObject SelectTarget()
|
|
{
|
|
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
if (Physics.Raycast(ray, out var hit, 100, layerMask))
|
|
{
|
|
//Debug.Log($"<color=#e94242>hit: {hit.collider.gameObject.layer}</color>");
|
|
|
|
//if (hit.collider.gameObject.layer == layerMask)
|
|
//{
|
|
//Debug.Log($"<color=#e94242>hit position: {hit.point}</color>");
|
|
return hit.collider.gameObject;
|
|
//}
|
|
}
|
|
return null;
|
|
}
|
|
}
|