【问题标题】:Unity-How to move with touch input?Unity-如何通过触摸输入移动?
【发布时间】:2019-03-07 21:18:24
【问题描述】:

我在为我的代码实现触摸移动时遇到了一些问题。有人可以写信给我我需要做什么才能让它工作吗?

我想像 Input.GetAxis("Horizo​​ntal"); 那样移动

这是我随轴移动的工作代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speedY = 5f, speedX = 3f, boundX = 3f;
    
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float input = Input.GetAxis("Horizontal");
        print(input);
       
        }

    }
    void Move()
    {
        Vector2 temp = transform.position;
        temp.y += speedY * Time.smoothDeltaTime;
        temp.x += speedX * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
           
        transform.position = temp;
    }
}

这是我的解决方案,我认为它会起作用,但它不起作用......

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speedY = 5f, speedX = 3f, boundX = 3f;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float input = Input.GetAxis("Horizontal");
        print(input);
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.position.x > (Screen.width / 2))
            {
                Move();
                Debug.Log("Go right");
            }
            if (touch.position.x < (Screen.width / 2))
            {
                Debug.Log("justleft");
            }
        }
}


    }
    void Move()
    {
        Vector2 temp = transform.position;
        temp.y += speedY * Time.smoothDeltaTime;
        temp.x += speedX * Time.smoothDeltaTime * Input.touchCount;
        transform.position = temp;
    }
}

当我点击最后一段代码之类的代码时,我看不到调试。

有人可以给我写解决方案吗?或者帮我一些小费。

非常感谢

【问题讨论】:

  • 你的第一个代码不会工作,因为你从不调用 move 并且你有一个额外的 { in update 这是一个语法错误。同样在第二个代码中,为什么你有GetAxis 你已经使用了触摸。此外,您的 movemethod 也没有任何意义。你想向右走还是想向上向右走?因为这就是该方法目前所做的。
  • 我想左右走,我不想上去,因为它总会上去。我希望你能把你的答案作为答案。

标签: unity3d touch move


【解决方案1】:

如果我理解正确,你可以这样实现

void Update()
{
    //Because you always wanna move up
    transform.position += Vector2.up * Time.deltaTime;

    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        if (touch.position.x > (Screen.width / 2))
        {
            //Since i do not know how much right you wanna go
            // This will just go left or right as long as there is a touch 
            transform.position += Vector2.right * Time.deltaTime * speedX;
            Debug.Log("Go right");
        }
        if (touch.position.x < (Screen.width / 2))
        {
            transform.position += Vector2.left* Time.deltaTime * speedX;
            Debug.Log("justleft");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2011-10-02
    • 2016-07-19
    相关资源
    最近更新 更多