UnityEngine.Input.GetTouch (индекс System.Int32)

Это означает быть злыми птицами, такими как рогатка, мне нужно определить начало и конец касания, сценарий работает, но когда я убираю палец с экрана, появляется ошибка примерно в строке 24 (я ставлю точки рядом с ней). Ошибка появляется только в том случае, если я не касаюсь экрана, если я касаюсь его, она исчезает, она возвращается, когда я поднимаю палец.

public class BallShooting : MonoBehaviour
{
    public Rigidbody2D rb;
    private bool isPressed = false;
    public bool isDead = false;
    public bool hasShoot = false;
    public int Damage;
    public int BaseDamage = 1;

    public BoxCollider2D DeathZone;
    public Transform startingSpot;

    public GameObject platform;

    public float releaseTime = .15f;
    void FixedUpdate()
    {

        if (Input.touchCount >= 0 && isDead == false)
        {
    ...        if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
               // Debug.Log("TouchBegan");
                isPressed = true;
                rb.isKinematic = true;
            }
            if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
              //  Debug.Log("TouchEnded");
                isPressed = false;
                rb.isKinematic = false;

                StartCoroutine(Release());
            }
        }

        if(isPressed && !hasShoot)
        {
            Touch touch = Input.GetTouch(0);
            Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            rb.position = touchPosition;
        }


        if (isDead)
        {
            this.transform.position = new Vector3(startingSpot.position.x, startingSpot.position.y, startingSpot.position.z);
            isDead = false;
            DeathZone.enabled = true;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Ground" && hasShoot == true)
        {
            isDead = true;
            Debug.Log("Dead");
            DeathZone.enabled = false;
        }
    } 
    IEnumerator Release()
    {
        yield return new WaitForSeconds(releaseTime);
        GetComponent<SpringJoint2D>().enabled = false;
        hasShoot = true;
    }    
}

1 ответ

Решение

Нужно изменить строчку

if (Input.touchCount >= 0 && isDead == false)

к

if (Input.touchCount > 0 && isDead == false)
Другие вопросы по тегам