【问题标题】:Unity hold touch move horizontallyunity hold touch 水平移动
【发布时间】:2019-01-20 17:38:45
【问题描述】:

我不知道如何开始,我想在按住触摸时左右移动我的角色。

就像在这个游戏中一样:

Example Game - Stairs from Ketchapp

我只有检测屏幕左侧或右侧空间的脚本。

public float forwardSpeed = 5f;
public float sideSpeed = 5f;

private void Update()
{
    Vector3 deltaPosition = transform.forward * forwardSpeed;
    if (Input.touchCount > 0)
    {
        Vector3 touchPosition = Input.GetTouch(0).position;
        if (touchPosition.x > Screen.width * 0.5f)
            deltaPosition += transform.right * sideSpeed;
        else
            deltaPosition -= transform.right * sideSpeed;
    }
    transform.position += deltaPosition * Time.deltaTime;
}

【问题讨论】:

标签: c# unity3d mobile touch


【解决方案1】:

这个解决方案对我有用。它在一个简单的方块破坏游戏中用于向左或向右移动桨。

void Update () {
    if (Input.touchCount > 0){
        Touch touch = Input.GetTouch(0);

        int direction = (touch.position.x > (Screen.width / 2)) ? 1 : -1;
        MovePaddle(direction);
    }

}

void MovePaddle(int direction){
    float xPos = transform.position.x + (direction * Time.deltaTime * paddleSpeed);
    playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
    transform.position = playerPos;
}

【讨论】:

  • 感谢您的回复,但这是我已经拥有的东西。该脚本将左右两侧的屏幕减半,但与我发布的示例不同。
【解决方案2】:

我认为您想说的是仅在您按下屏幕时才移动,不是吗? 也许这可以帮助你:

public float forwardSpeed = 5f;
public float sideSpeed = 5f;

private void Update()
{
    Vector3 deltaPosition = transform.forward * forwardSpeed;
    if (Input.touchCount > 0)
    {
        Vector3 touchPosition = Input.GetTouch(0).position;
        if (touchPosition.x > Screen.width * 0.5f)
            deltaPosition += transform.right * sideSpeed;
        else
            deltaPosition -= transform.right * sideSpeed;
    }
    else{
            deltaPosition = sideSpeed;
    }
    transform.position += deltaPosition * Time.deltaTime;
}

pd:尚未测试,因为还没有

【讨论】:

  • 我认为这应该是公认的答案。
【解决方案3】:

我有一个不太顺利的解决方案

public float speed = 5;
public Rigidbody rb;

public void FixedUpdate()
{
    float h = Input.GetAxis("Horizontal");

    //Add touch support
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        Touch touch = Input.touches[0];
        h = touch.deltaPosition.x;
    }

    //Move only if we actually pressed something
    if (h > 0 || h < 0)
    {
        Vector3 tempVect = new Vector3(h, 0, 0);
        tempVect = tempVect.normalized * speed * Time.deltaTime;

        //rb.MovePosition(rb.transform.position + tempVect);

        Vector3 newPos = rb.transform.position + tempVect;
        checkBoundary(newPos);
    }
}

void checkBoundary(Vector3 newPos)
{
    //Convert to camera view point
    Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos);

    //Apply limit
    camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f);
    camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f);

    //Convert to world point then apply result to the target object
    Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint);
    rb.MovePosition(finalPos);
}

【讨论】:

    【解决方案4】:
    private void Start()
    {
        Application.targetFrameRate = 60;
    }
    
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch t = Input.GetTouch(0);
    
             transform.position = new Vector3(transform.position.x + t.deltaPosition.x * .02f, transform.position.y, transform.position.z );
        }
    }
    

    你可以使用它。简单:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多