1

我正在尝试在增强 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);
            }
        }
    }
}
4

2 回答 2

1

查看代码,我认为您可能需要限制ScreenDelta变量。由于Translate函数使用 ScreenDelta 值。

你可以这样做:

ScreenDelta = new Vector3(ScreenDelta.x, Mathf.Clamp(ScreenDelta.y, 0f, 0f), ScreenDelta.y);

这会将“y”轴限制为零。

于 2019-12-24T12:59:40.073 回答
0

在检查员所需的手指计数 = 1

private void Translate(Vector2 screenDelta)
{
    // Make sure the camera exists
    var camera = LeanHelper.GetCamera(this._camera, gameObject);

    if (camera != null)
    {
        // Screen position of the transform
        var screenPoint = camera.WorldToScreenPoint(transform.position);

        // Add the deltaPosition
        Vector3 Position = new Vector3(screenDelta.x, 0f, screenDelta.y/100f);
        screenPoint += (Vector3)Position * Sensitivity;

        // 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);
    }
}
于 2021-05-13T10:28:11.737 回答