修改捏线逻辑
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
@@ -14,8 +15,11 @@ namespace NBF
|
||||
private Rigidbody rb;
|
||||
|
||||
|
||||
private float maxCatchupDuration = 0.5f;
|
||||
private Transform targetTransform;
|
||||
private float originalSpring;
|
||||
private float pinchElapsedTime;
|
||||
|
||||
|
||||
public bool isPinched { get; private set; }
|
||||
|
||||
@@ -31,27 +35,37 @@ namespace NBF
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (isPinched && targetTransform != null)
|
||||
if (isPinched && !moveToTargetDone && targetTransform != null)
|
||||
{
|
||||
transform.position =
|
||||
Vector3.MoveTowards(transform.position, targetTransform.position, Time.deltaTime * _speed);
|
||||
if (!moveToTargetDone)
|
||||
pinchElapsedTime += Time.fixedDeltaTime;
|
||||
// transform.position =
|
||||
// Vector3.MoveTowards(transform.position, targetTransform.position, Time.deltaTime * _speed);
|
||||
rb.MovePosition(Vector3.MoveTowards(transform.position, targetTransform.position,
|
||||
Time.deltaTime * _speed));
|
||||
if (Vector3.Distance(transform.position, targetTransform.position) < 0.1f ||
|
||||
pinchElapsedTime >= maxCatchupDuration)
|
||||
{
|
||||
if (Vector3.Distance(transform.position, targetTransform.position) < 0.1f)
|
||||
{
|
||||
moveToTargetDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (moveToTargetDone)
|
||||
{
|
||||
transform.position = targetTransform.position;
|
||||
moveToTargetDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
SyncPosition();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
SyncPosition();
|
||||
}
|
||||
|
||||
private void SyncPosition()
|
||||
{
|
||||
if (!isPinched) return;
|
||||
if (!moveToTargetDone) return;
|
||||
rb.MovePosition(targetTransform.position);
|
||||
}
|
||||
|
||||
// 外部调用:开始捏住流程
|
||||
public void StartPinch(Transform fingerTransform, float speed = 3)
|
||||
public void StartPinch(Transform fingerTransform, float speed = 3, float _maxCatchupDuration = 0.3f)
|
||||
{
|
||||
_speed = speed;
|
||||
Rigidbody fingerRb = fingerTransform.GetComponent<Rigidbody>();
|
||||
@@ -61,6 +75,8 @@ namespace NBF
|
||||
return;
|
||||
}
|
||||
|
||||
maxCatchupDuration = _maxCatchupDuration;
|
||||
pinchElapsedTime = 0f;
|
||||
isPinched = true;
|
||||
rb.useGravity = false;
|
||||
rb.isKinematic = true;
|
||||
|
||||
Reference in New Issue
Block a user