unity3d Unity C#移动的控件左右滑动

oyt4ldly  于 2023-02-16  发布在  C#
关注(0)|答案(1)|浏览(384)

我用Unity和C#做了一个3D游戏。我不知道如何在移动的屏幕上用滑动来控制对象(玩家)的左右方向。
这是我的C#代码:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float forwardForce = 300f;
    public float sidewaysForce = 200f;

    // Update is called once per frame
    void FixedUpdate()
    {
      rb.AddForce(0, 0, forwardForce * Time.deltaTime);

      if (Input.GetAxis("Horizontal") > 0.1)
      {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
      }

      if (Input.GetAxis("Horizontal") < -0.1)
      {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
      }
    }
}
pokxtpni

pokxtpni1#

您应该了解一下Mobile Touch TutorialsMobile Touch ManualInput.GetTouch。简而言之:你必须得到触摸,存储初始位置,然后将其与当前位置进行比较。

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float forwardForce = 300f;
    public float sidewaysForce = 200f;

    private Vector2 initialPosition;    

    // Update is called once per frame
    void Update()
    {
        // are you sure that you want to become faster and faster?
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if(Input.touchCount == 1)
        {
            touch = Input.GetTouch(0);

            if(touch.phase == TouchPhase.Began)
            {
                initialPosition = touch.position;
            } 
            else if(touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
            {
                // get the moved direction compared to the initial touch position
                var direction = touch.position - initialPosition;

                // get the signed x direction
                // if(direction.x >= 0) 1 else -1
                var signedDirection = Mathf.Sign(direction.x);

                // are you sure you want to become faster over time?
                rb.AddForce(sidewaysForce * signedDirection * Time.deltaTime, 0, 0);
            }
        }
    }
}

注:这始终与初始触摸位置进行比较=〉您不会进入其他方向,直到您
1.将触摸沿另一方向移动到初始位置
1.因为你用的是AddForce,所以等待的时间和你向另一个方向加力的时间一样长
也许您应该只与先前的触摸位置进行比较,而不是与初始位置进行比较:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float forwardForce = 300f;
    public float sidewaysForce = 200f;

    private Vector2 lastPosition;    

    // Update is called once per frame
    void Update()
    {
        // are you sure that you want to become faster and faster?
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if(Input.touchCount == 1)
        {
            touch = Input.GetTouch(0);

            if(touch.phase == TouchPhase.Began)
            {
                lastPosition = touch.position;
            }
            if(touch.phase == TouchPhase.Moved)
            {
                // get the moved direction compared to the initial touch position
                var direction = touch.position - lastPosition ;

                // get the signed x direction
                // if(direction.x >= 0) 1 else -1
                var signedDirection = Mathf.Sign(direction.x);

                // are you sure you want to become faster over time?
                rb.AddForce(sidewaysForce * signedDirection * Time.deltaTime, 0, 0);

                lastPosition = touch.position;
            }
        }
    }
}

现在你仍然需要滑动相同的时间...它不考虑滑动的距离。所以也许你应该使用:

// get the moved direction compared to the initial touch position
var direction = touch.position - lastPosition ;

// are you sure you want to become faster over time?
rb.AddForce(sidewaysForce * direction.x * Time.deltaTime, 0, 0);

你划得越多,施加的力就越大

相关问题