撬棍逻辑
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a29396a6175045d488caf12e1d0b1c46
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,144 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using DG.Tweening;
|
||||
using UnityEngine.UI;
|
||||
using Yarn.Unity;
|
||||
|
||||
public class Crowbar : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public float rotationSpeed = 10f; // 拖动旋转的速度
|
||||
public float rotationThreshold = 30f; // 触发撬动完成的旋转阈值
|
||||
|
||||
public Transform crowbarVisual;
|
||||
//public Transform crowbarBox;
|
||||
public Transform crowbarInPosition;
|
||||
//public Transform crowbarOutPosition;
|
||||
public Transform crowbarSocket;
|
||||
public bool IsInDialogue { get; set; } = false;
|
||||
|
||||
private Vector3 startPosition;
|
||||
private Quaternion startRotation;
|
||||
private bool isInserted = false;
|
||||
private Vector3 lastMousePosition;
|
||||
private float progress = 0f;
|
||||
private Image image;
|
||||
private bool isPryingCompleted = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
image = GetComponent<Image>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// startPosition = transform.position;
|
||||
// startRotation = transform.rotation;
|
||||
}
|
||||
|
||||
public void MoveIn()
|
||||
{
|
||||
//transform.DOMove(crowbarInPosition.position, 0.5f);
|
||||
}
|
||||
|
||||
public void MoveOut()
|
||||
{
|
||||
//transform.DOMove(crowbarOutPosition.position, 0.5f);
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (IsInDialogue || isInserted) return;
|
||||
|
||||
image.raycastTarget = false;
|
||||
//crowbarSocket.Activate();
|
||||
|
||||
RectTransform canvasRectTransform = (RectTransform)transform.root;
|
||||
Vector3 worldPoint;
|
||||
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, eventData.position, eventData.pressEventCamera, out worldPoint))
|
||||
{
|
||||
transform.position = worldPoint;
|
||||
}
|
||||
lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (IsInDialogue || isPryingCompleted) return;
|
||||
|
||||
if (isInserted)
|
||||
{
|
||||
float deltaY = Input.mousePosition.y - lastMousePosition.y;
|
||||
progress = Mathf.Clamp01(progress+deltaY * Time.deltaTime);
|
||||
|
||||
// 计算插入旋转和插入旋转+35度之间的插值
|
||||
float startAngle = crowbarSocket.transform.eulerAngles.z;
|
||||
float endAngle = startAngle - 45f;
|
||||
float currentAngle = Mathf.Lerp(startAngle, endAngle, progress);
|
||||
|
||||
// 更新撬棍的旋转角度
|
||||
crowbarVisual.rotation = Quaternion.Euler(0f, 0f, currentAngle);
|
||||
|
||||
if (progress >= 1f)
|
||||
{
|
||||
// 触发撬动完成的逻辑
|
||||
Debug.Log("Crowbar action completed.");
|
||||
isPryingCompleted = true;
|
||||
//ResetCrowbar();
|
||||
ResetCrowbar();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
RectTransform canvasRectTransform = (RectTransform)transform.root;
|
||||
Vector3 worldPoint;
|
||||
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, eventData.position, eventData.pressEventCamera, out worldPoint))
|
||||
{
|
||||
transform.position = worldPoint;
|
||||
}
|
||||
}
|
||||
|
||||
lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
if (IsInDialogue) return;
|
||||
|
||||
if (isInserted)
|
||||
{
|
||||
ResetCrowbarProgress();
|
||||
}
|
||||
else
|
||||
{
|
||||
float snappingRange = 1f; // You might need to adjust this for WorldSpace Canvas
|
||||
if (Vector3.Distance(transform.position, crowbarSocket.transform.position) <= snappingRange)
|
||||
{
|
||||
transform.position = crowbarSocket.transform.position;
|
||||
crowbarVisual.rotation = crowbarSocket.transform.rotation;
|
||||
isInserted = true;
|
||||
//crowbarSocket.Deactivate();
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.DOMove(crowbarInPosition.position, 0.1f).OnComplete(() =>
|
||||
{
|
||||
//crowbarSocket.Deactivate();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetCrowbarProgress()
|
||||
{
|
||||
//isInserted = false;
|
||||
progress = 0f;
|
||||
crowbarVisual.rotation = startRotation;
|
||||
}
|
||||
private void ResetCrowbar()
|
||||
{
|
||||
isInserted=false;
|
||||
progress = 0f;
|
||||
crowbarVisual.rotation = startRotation;
|
||||
isPryingCompleted = false;
|
||||
transform.DOMove(crowbarInPosition.position, 0.1f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7f0d3b645b9cb60418d0059fad6b7cc6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user