-1

这是一个像弹弓一样的愤怒的小鸟,我需要检测触摸的开始和结束,脚本有效但是当我将手指从屏幕上抬起时出现的错误大约是第 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;
    }    
}
4

1 回答 1

0

需要换线

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

if (Input.touchCount > 0 && isDead == false)
于 2020-01-16T04:44:43.550 回答