【问题标题】:How can I make my character move when slide in Unity?在 Unity 中滑动时如何让我的角色移动?
【发布时间】:2019-02-13 16:13:45
【问题描述】:

我想让我的角色在滑动屏幕或触摸 Unity 3D 手机中的虚拟按钮时向左或向右移动,这是玩家用键盘键移动它的移动脚本,但是我希望它适用于移动设备。

void FixedUpdate ()
{
    // Add a forward force
    rb.AddForce(0, 0, forwardForce * Time.deltaTime);

    if (Input.GetKey("d"))  // If the player is pressing the "d" key
    {
        // Add a force to the right
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (Input.GetKey("a"))  // If the player is pressing the "a" key
    {
        // Add a force to the left
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (rb.position.y < -1f)
    {
        FindObjectOfType<GameManager>().EndGame();
    }

}

【问题讨论】:

    标签: c# android visual-studio unity3d


    【解决方案1】:

    处理这个问题的最基本方法是(我认为)类似于:

    float lastPosition;
    void OnSwipte(float delta)
    {
    // code your movement here
    }
    void Update()
    {
     if (Input.GetMouseButtonDown(0)) 
           lastPosition=Input.mousePosition.x; // store touch point
      else 
      if (Input.GetMouseButton(0)) 
        {
         OnSwipe(Input.mousePosition.x-lastPosition);
         lastPosition=Input.mousePosition.x; 
        }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      相关资源
      最近更新 更多