【问题标题】:Rotate objects with touch in unity统一旋转触摸对象
【发布时间】:2020-01-20 18:38:13
【问题描述】:

我想用统一的方式旋转一个对象。只要用户按住触摸,我的触摸向左移动,对象就应该向左旋转,反之亦然。 只要触摸向左或向右移动,我的代码就会正确旋转对象。稍后,如果停止并按住触摸移动,则对象开始向右旋转。这是我尝试过的。

{
if (Input.touchCount > 0)
    {
        touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Began)
        {
            oldTouchPosition = touch.position;
        }
        else if (touch.phase == TouchPhase.Moved)
        {
            if (touch.deltaPosition.x > 10f)
            {
                rotateLeftward = false;
                rotateRightward = true;
            }

            else if (touch.deltaPosition.x < 10f)
            {
                rotateRightward = false;
                rotateLeftward = true;
            }
        }



        if(rotateLeftward == true )
        {
            RotateLeftWard();
        }
        else if (rotateRightward == true)
        {
            RotateRightWard();
        }
    }
}


void RotateLeftWard()
{
    transform.rotation = Quaternion.Euler(0f, 1 * keepRotateSpeed, 0f) * transform.rotation;
}

void RotateRightWard()
{
    transform.rotation = Quaternion.Euler(0f, -1 * keepRotateSpeed, 0f) * transform.rotation;
}

【问题讨论】:

  • 您应该直接使用移动​​的 deltaPosition 进行旋转或使用Transform.RotateTime.deltaTime .. 目前您只需将每一帧旋转大约一个固定的量,但前提是您爱得足够快.. ..

标签: c# unity3d


【解决方案1】:

我认为没有代码将两个旋转都设置为 false..

【讨论】:

    【解决方案2】:

    我能够使用下面的代码为这一切编写一个完美的解决方案 希望对你有用

    private Touch touch;
    private Vector2 oldTouchPosition;
    private Vector2 NewTouchPosition; 
    [SerializeField]
    private float keepRotateSpeed = 10f;
    
    private void Update()
    {
         RotateThings(); 
    }
    private void RotateThings()
    {
      if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                oldTouchPosition = touch.position;
            }
    
            else if (touch.phase == TouchPhase.Moved)
            {
                NewTouchPosition = touch.position;
            }
    
            Vector2 rotDirection = oldTouchPosition - NewTouchPosition;
            Debug.Log(rotDirection);
            if (rotDirection.x < 0 )
            {
                RotateRight();
            }
    
            else if (rotDirection.x > 0 )
            {
                RotateLeft();
            }
        }
    }
    
    void RotateLeft()
        {
            transform.rotation = Quaternion.Euler(0f, 1.5f * keepRotateSpeed, 0f) * transform.rotation;
        }
    
    void RotateRight()
    {
        transform.rotation = Quaternion.Euler(0f, -1.5f * keepRotateSpeed, 0f) * transform.rotation;
    }
    

    【讨论】:

      【解决方案3】:

      与 Gurbrinder Singh 发布的相同,但使用 deltaThreshold 有点先进

      private Touch touch;
      private Vector2 oldTouchPosition;
      private Vector2 NewTouchPosition;
      [SerializeField]
      private float keepRotateSpeed = 10f;
      
      [SerializeField]
      private float deltaThreshold = 5f;
      
      
      private void Update()
      {
          RotateThings();
      }
      private void RotateThings()
      {
          if (Input.touchCount > 0)
          {
              touch = Input.GetTouch(0);
              if (touch.phase == TouchPhase.Began)
              {
                  oldTouchPosition = touch.position;
                  NewTouchPosition = touch.position;
              }
      
              else if (touch.phase == TouchPhase.Moved)
              {
                  oldTouchPosition = NewTouchPosition;
                  NewTouchPosition = touch.position;
              }
      
              float delta = Mathf.Abs(oldTouchPosition.x - NewTouchPosition.x);
              if (/*touch.phase != TouchPhase.Stationary &&*/ delta >= deltaThreshold)
              {
                  Vector2 rotDirection = oldTouchPosition - NewTouchPosition;
                  Debug.Log(delta);
                  if (rotDirection.x < 0)
                  {
                      RotateRight();
                  }
      
                  else if (rotDirection.x > 0)
                  {
                      RotateLeft();
                  }
              }
          }
      }
      
      void RotateLeft()
      {
          transform.rotation = Quaternion.Euler(0f, 1.5f * keepRotateSpeed, 0f) * transform.rotation;
      }
      
      void RotateRight()
      {
          transform.rotation = Quaternion.Euler(0f, -1.5f * keepRotateSpeed, 0f) * transform.rotation;
      }
      

      【讨论】:

        【解决方案4】:

        如果你想要更多的控制权,这样的事情会起作用

        private float _lastTouchRot = float.NegativeInfinity;
        
        ...
        
        private void UpdateMobile()
        {
            if (UnityEngine.Input.touchCount == 2)
            {
                var touch0 = Input.GetTouch(0);
                var touch1 = Input.GetTouch(1);
        
                var touchRot = Vector2.SignedAngle(touch0.position - touch1.position, Vector2.right);
                if(this._lastTouchRot == float.NegativeInfinity) this._lastTouchRot = touchRot;
        
                var delta = this._lastTouchRot - touchRot;
        
                this._angle += delta;
        
                this._lastTouchRot = touchRot;
        
                if(touch0.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Ended)
                    this._lastTouchRot = float.NegativeInfinity;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-10
          • 1970-01-01
          • 2014-03-16
          • 1970-01-01
          • 2013-12-26
          • 2011-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多