【问题标题】:Restrict movements along an axis in LeanTouch LeanDragTranslate Unity在 LeanTouch LeanDragTranslate Unity 中限制沿轴的移动
【发布时间】:2019-12-13 08:06:26
【问题描述】:

我试图在增强后移动一个 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);
            }
        }
    }
}

【问题讨论】:

    标签: c# unity3d augmented-reality


    【解决方案1】:

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

    你可以这样做:

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

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

    【讨论】:

    • 嗨,这很好用。但我注意到的一件事是,它也限制了 z 轴。有解决方法吗?在翻译时,不知何故,对象变得越来越小
    • 嘿,我检查了,是的,似乎有问题。我尝试谷歌找出是否有任何其他解决方法,我找不到它。这里的问题是我们正在尝试将 2D 手指平移映射到 3D 空间,所以当您尝试向上拖动手指时,您不知道它是向上还是向后(当然,您不能在屏幕内移动手指) .我不确定你是否在使用 ARCore,但我不得不切换到 ARCore 对象操作演示,因为我也找不到任何其他解决方案。
    【解决方案2】:

    在检查员中要求的手指计数 = 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多