Android Touch Движение

1

Я хочу знать, как перемещать объект влево или вправо, касаясь. Что-то вроде:

public float speed;

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

    Vector3 Movement = new Vector3 ( LeftRight, 0, 0);

    rigidbody.AddForce(Movement * speed);
}

Но только для прикосновения к экрану. Первая половина экрана слева и другая справа.

Теги:
unity3d
2d
unity3d-2dtools

1 ответ

1
Лучший ответ

Для типа ввода касания в android или ios используйте Input.GetTouch.
Идея состоит в том, чтобы получить позицию касания, а затем решить, касаются ли она левой или правой стороны экрана, получив ширину экрана с помощью Screen.width.

public float speed;

void FixedUpdate ()
{
    float LeftRight = 0;

    if(Input.touchCount > 0){
        // touch x position is bigger than half of the screen, moving right
        if(Input.GetTouch(0).position.x > Screen.width / 2)
            LeftRight = 1;
        // touch x position is smaller than half of the screen, moving left
        else if(Input.GetTouch(0).position.x < Screen.width / 2)
            LeftRight = -1;
    }

    Vector3 Movement = new Vector3 ( LeftRight, 0, 0);

    rigidbody.AddForce(Movement * speed);
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню