我正在尝试在增强 AR 对象后移动它。为此,我使用了 Lean touch,我使用的脚本是 LeanDragTranslate。
但现在我试图限制沿轴的运动,以使物体永远不会离开地面。我该怎么做?
我使用的脚本是
namespace Lean.Touch
{
/// <summary>This component allows you to translate the current GameObject relative to the camera using the finger drag gesture.</summary>
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanDragTranslate")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Drag Translate")]
public class LeanDragTranslate : MonoBehaviour
{
/// <summary>The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information.</summary>
public LeanFingerFilter Use = new LeanFingerFilter(true);
/// <summary>The camera the translation will be calculated using.\n\nNone = MainCamera.</summary>
[Tooltip("The camera the translation will be calculated using.\n\nNone = MainCamera.")]
public Camera Camera;
/// <summary>If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.
/// -1 = Instantly change.
/// 1 = Slowly change.
/// 10 = Quickly change.</summary>
[Tooltip("If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.\n\n-1 = Instantly change.\n\n1 = Slowly change.\n\n10 = Quickly change.")]
public float Dampening = -1.0f;
[HideInInspector]
[SerializeField]
private Vector3 remainingTranslation;
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually add a finger.</summary>
public void AddFinger(LeanFinger finger)
{
Use.AddFinger(finger);
}
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove a finger.</summary>
public void RemoveFinger(LeanFinger finger)
{
Use.RemoveFinger(finger);
}
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove all fingers.</summary>
public void RemoveAllFingers()
{
Use.RemoveAllFingers();
}
#if UNITY_EDITOR
protected virtual void Reset()
{
Use.UpdateRequiredSelectable(gameObject);
}
#endif
protected virtual void Awake()
{
Use.UpdateRequiredSelectable(gameObject);
}
protected virtual void Update()
{
// Store
var oldPosition = transform.localPosition;
// Get the fingers we want to use
var fingers = Use.GetFingers();
// Calculate the screenDelta value based on these fingers
var screenDelta = LeanGesture.GetScreenDelta(fingers);
if (screenDelta != Vector2.zero)
{
// Perform the translation
if (transform is RectTransform)
{
TranslateUI(screenDelta);
}
else
{
Translate(screenDelta);
}
}
// Increment
remainingTranslation += transform.localPosition - oldPosition;
// Get t value
var factor = LeanTouch.GetDampenFactor(Dampening, Time.deltaTime);
// Dampen remainingDelta
var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);
// Shift this transform by the change in delta
transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;
// Update remainingDelta with the dampened value
remainingTranslation = newRemainingTranslation;
}
private void TranslateUI(Vector2 screenDelta)
{
var camera = LeanTouch.GetCamera(Camera, gameObject);
// Screen position of the transform
var screenPoint = RectTransformUtility.WorldToScreenPoint(camera, transform.position);
// Add the deltaPosition
screenPoint += screenDelta;
// Convert back to world space
var worldPoint = default(Vector3);
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.parent as RectTransform, screenPoint, camera, out worldPoint) == true)
{
transform.position = worldPoint;
}
}
private void Translate(Vector2 screenDelta)
{
// Make sure the camera exists
var camera = LeanTouch.GetCamera(Camera, gameObject);
if (camera != null)
{
// Screen position of the transform
var screenPoint = camera.WorldToScreenPoint(transform.position);
// Add the deltaPosition
screenPoint += (Vector3)screenDelta;
// Convert back to world space
transform.position = camera.ScreenToWorldPoint(screenPoint);
}
else
{
Debug.LogError("Failed to find camera. Either tag your camera as MainCamera, or set one in this component.", this);
}
}
}
}