Unity-как двигаться с сенсорным вводом? - PullRequest
0 голосов
/ 08 марта 2019

У меня есть некоторые проблемы с реализацией сенсорного движения в моем коде .. Может кто-нибудь написать мне, что мне нужно сделать, чтобы он заработал?

Я хочу двигаться как Input.GetAxis («Горизонтальный»);

Вот мой рабочий код для перемещения по оси

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;
    }
}

Я не вижу отладки, когда щелкаю по коду, как последний блок кода.

Может кто-нибудь написать мне решение?или помогите мне с некоторыми советами.

большое спасибо

1 Ответ

1 голос
/ 08 марта 2019

Вы можете просто реализовать это так, если я вас правильно понял

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");
        }
    }
}
...