【问题标题】:smoothly move object on mouse drag using lerp使用 lerp 在鼠标拖动时平滑移动对象
【发布时间】:2016-06-06 20:23:27
【问题描述】:

这是使用 lerp 和 mouseDrag 事件移动我的对象的代码,但它运行得不是很顺利。

//Camera Point smooth movment 
if (mouseWorking)
{
   if (Physics.Raycast(ray, out hit, Camera.main.farClipPlane)) //,LayerMask.NameToLayer("Gola")))
   {
      if (hit.transform.gameObject.name == "CameraElasticPoint")
      {
         return;
      }
      else
      {
         //working
         transform.position = Vector3.Lerp(transform.position, new Vector3(hit.point.x, hit.point.y + 1, hit.point.z), Time.deltaTime * 3); //0.5000001f

         //StartCoroutine(SmoothMove());
         hitPoint = Input.mousePosition;
       }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可能会发现Vector's Lerp 更流畅。

    public class ExampleClass : MonoBehaviour {
        public Transform startMarker;
        public Transform endMarker;
        public float speed = 1.0F;
        private float startTime;
        private float journeyLength;
        void Start() {
            startTime = Time.time;
            journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
        }
        void Update() {
            float distCovered = (Time.time - startTime) * speed;
            float fracJourney = distCovered / journeyLength;
            transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
        }
    }
    

    【讨论】:

    • 它是关于我移动物体时的旋转
    • @MohammadFaizanKhan 对不起,是的。更新为来自 Unity 文档的链接,该链接提供了一种很好的实现方式。
    • 如果您查看他们的文档,他们提供了一种添加平滑因子的启动/更新技术,我将更新注释以提高可见性。
    • @MohammadFaizanKhan 我的回答对您解决问题有帮助吗?
    • 不行,跳跳跳,不流畅
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多