Reputation: 45
This is ment to be an angry birds like slingshot, i need to detect the begining and end of a touch, the script works but when i lift my finger off the screen The error shows up is about line 24 (I put dots next to it). The error only appears if I dont touch the screen, if i touch it it goes away, it comes back when i lift my finger.
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;
}
}
Upvotes: -1
Views: 987
Reputation: 257
Need to change the line
if (Input.touchCount >= 0 && isDead == false)
to
if (Input.touchCount > 0 && isDead == false)
Upvotes: 0