2015-04-15 2 views
2

сначала извините за мой плохой английский язык. Я делаю игру, основанную на единстве 2D-движка и использующем язык C# ... У меня проблема с моей сенсорной системой, и я не могу ее решить. Теперь у меня есть ключ для прыжка, но это не тот способ, который я хочу для прыжки. я просто хочу, чтобы мой игрок прыгнул, когда палец коснулся экрана и перетащил его вверх. вот мой код:единство 2D - как прыгать, перетаскивая палец на сенсорный экран?

// GUI textures 
public GUITexture guiLeft; 
public GUITexture guiRight; 
public GUITexture guiJump; 

// Movement variables 
public float moveSpeed = 5f; 
public float jumpForce = 50f; 
public float maxJumpVelocity = 2f; 

// Movement flags 
private bool moveLeft, moveRight, doJump = false; 

// Update is called once per frame 
void Update() { 

    // Check to see if the screen is being touched 
    if (Input.touchCount > 0) 
    { 
     // Get the touch info 
     Touch t = Input.GetTouch(0); 

     // Did the touch action just begin? 
     if (t.phase == TouchPhase.Began) 
     { 
      // Are we touching the left arrow? 
      if (guiLeft.HitTest(t.position, Camera.main)) 
      { 
       Debug.Log("Touching Left Control"); 
       moveLeft = true; 
      } 

      // Are we touching the right arrow? 
      if (guiRight.HitTest(t.position, Camera.main)) 
      { 
       Debug.Log("Touching Right Control"); 
       moveRight = true; 
      } 

      // Are we touching the jump button? 
      if (guiJump.HitTest(t.position, Camera.main)) 
      { 
       Debug.Log("Touching Jump Control"); 
       doJump = true; 
      } 
     } 

     // Did the touch end? 
     if (t.phase == TouchPhase.Ended) 
     { 
      // Stop all movement 
      doJump = moveLeft = moveRight = false; 
      rigidbody2D.velocity = Vector2.zero; 
     } 
    } 

    // Is the left mouse button down? 
    if (Input.GetMouseButtonDown(0)) 
    { 
     // Are we clicking the left arrow? 
     if (guiLeft.HitTest(Input.mousePosition, Camera.main)) 
     { 
      Debug.Log("Touching Left Control"); 
      moveLeft = true; 
     } 

     // Are we clicking the right arrow? 
     if (guiRight.HitTest(Input.mousePosition, Camera.main)) 
     { 
      Debug.Log("Touching Right Control"); 
      moveRight = true; 
     } 

     // Are we clicking the jump button? 
     if (guiJump.HitTest(Input.mousePosition, Camera.main)) 
     { 
      Debug.Log("Touching Jump Control"); 
      doJump = true; 
     } 
    } 

    if (Input.GetMouseButtonUp(0)) 
    { 
     // Stop all movement on left mouse button up 
     doJump = moveLeft = moveRight = false; 
     rigidbody2D.velocity = Vector2.zero; 
    } 
} 

void FixedUpdate() 
{ 
    // Set velocity based on our movement flags. 
    if (moveLeft) 
    { 
     rigidbody2D.velocity = -Vector2.right * moveSpeed; 
    } 

    if (moveRight) 
    { 
     rigidbody2D.velocity = Vector2.right * moveSpeed; 
    } 

    if (doJump) 
    { 
     // If we have not reached the maximum jump velocity, keep applying force. 
     if (rigidbody2D.velocity.y < maxJumpVelocity) 
     { 
      rigidbody2D.AddForce(Vector2.up * jumpForce); 
     } else { 
      // Otherwise stop jumping 
      doJump = false; 
     } 
    } 
} 

ответ

0

Использование

Input.GetTouch (0) .deltaPosition.

Он будет указывать, в каком направлении вы двигали рукой каждое обновление.

0

Существует еще одно состояние для сенсорных входов, которое не только начинается и заканчивается. Если я правильно помню, это TouchPhase.Moved. Итак, первое, что вам нужно сделать, это получить позицию касания, когда начнется касание (TouchPhase.Began), после чего вам нужно проверить положение касания на каждом кадре (TouchPhase.Moved). Тогда у вас будет 2 позиции касания, которые начинаются и текут. Так что давайте скажем, если разница между этими двумя x cooridinates больше чем чем-то (Зависит от того, какую чувствительность вы хотите), вы можете вызвать функцию прыжка или что еще вы хотите.

Смежные вопросы