【问题标题】:Unity - continue object inertia after drag has stopped?Unity - 拖动停止后继续物体惯性?
【发布时间】:2019-04-13 01:09:31
【问题描述】:

好的,所以我有一个对象可以控制在 Unity 中使用 Mousedown 的旋转,如下所示:

使用 UnityEngine; 使用 System.Collections;

public class ObjectRotator : MonoBehaviour
{

    private float _sensitivity;
    private Vector3 _mouseReference;
    private Vector3 _mouseOffset;
    private Vector3 _rotation;
    private bool _isRotating;

    void Awake()
    {
        _sensitivity = 0.4f;
        _rotation = Vector3.zero;

    }


    //This include 
    void Update()
    {
        if (_isRotating)
        {

            // offset
            _mouseOffset = (Input.mousePosition - _mouseReference);

            // apply rotation
            _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;

            // rotate
            transform.Rotate(_rotation);

            // store mouse
            _mouseReference = Input.mousePosition;
        }

    }



    void OnMouseDown()
    {
        EndiTweens();
        // rotating flag
        _isRotating = true;

        // store mouse
        _mouseReference = Input.mousePosition;
    }

    void OnMouseUp()
    {
        // rotating flag
        _isRotating = false;
    }

}

这很好用,除了在 OnMouseUp() 之后,对象会完全停止。我希望它“滑动”一点,就好像它有惯性一样。我该如何实现?

试过这个来调整到一个停止值:

 //This include 
    void Update()
    {
        if(_isRotating && _isStopping)
        {
            // apply rotation
            t += Time.deltaTime * 1.1f;
            float rotDiff = Mathf.Lerp(2f, 0f, t); //some max
            _rotation.y = transform.rotation.y - rotDiff;


            print(_rotation.y);
            transform.Rotate(_rotation);

            if(t >= 1f)
            {
                _isRotating = false;
                _isStopping = false;
                t = 0f;
            }
        }
        else if (_isRotating)
        {

            // offset
            _mouseOffset = (Input.mousePosition - _mouseReference);

            // apply rotation
            _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;

            // rotate
            transform.Rotate(_rotation);

            // store mouse
            _mouseReference = Input.mousePosition;
        }

    }

    void EndiTweens()
    {
        iTween[] tweens = GetComponents<iTween>();
        foreach (iTween tween in tweens)
        {
            tween.time = 0;
            tween.SendMessage("Update");
        }

    }

    void OnMouseDown()
    {
        EndiTweens();
        // rotating flag
        _isRotating = true;

        // store mouse
        _mouseReference = Input.mousePosition;
    }

    void OnMouseUp()
    {
        // rotating flag
        _isStopping = true;
       _isRotating = false;
    }

【问题讨论】:

  • 您需要计算鼠标速度并将其应用于对象。类似 mousedelta/deltatime
  • 一旦你发现这个速度,你需要随着时间的推移降低它。我想你会发现你不应该在你的OnMouseUp 方法中设置_isRotating = false。之后你会想要继续更新。

标签: c# unity3d physics


【解决方案1】:

OnMouseUp 你正在设置_isRotating = false。不要将此标志设置为 false。添加另一个标志_isStopping 并计算每帧上减少的旋转速度。将最低速度速度设置为 0.001,然后将速度设置为 0 或设置 _isRotating = false

【讨论】:

  • 好的,请参阅我的问题,了解我的尝试。我没有速度,因为我直接应用旋转并且没有刚体。因此,我尝试将旋转差异值从最大值降至零,最终对象将停止。但现在这些都不起作用
  • @UserDude 第一个问题在OnMouseUp 你忘了删除_isRotating = false; 删除它。这可能是唯一的一个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多