Hi guys, In my game I want the player to tap a cage multiple times to set an elephant free.
I’ve set up the following code and included a counter. When the collision box is tapped I want the tap count to increase, then when the tap count gets to 5 I want the cage door to open.
Here is my c# code
using UnityEngine;
using System.Collections;
public class CageLv0 : MonoBehaviour {
public int tapCount; //how many times has the button been tapped?
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if (tapCount == 2)
{
Destroy(gameObject);
}
if (Input.touchCount == 1) //if the button is pressed
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); //sets the screen to worldpoint
Vector2 touchPos = new Vector2(wp.x, wp.y); //declare x and y of the world point
if (collider2D == Physics2D.OverlapPoint(touchPos)) //if the collider is touched
{
tapCount ++;
}
}
}
}
The weird thing is, if I tap the cage once, the door is deleted. Not sure how this happens, I’d expect nothing to happen if it didn’t work.
What am I missing?
Cheers guys