Hey guys. Making a small game on unity for android. All I want to do is make a gameObject with a 2D Collider open a new scene when I press it, but My current code does not work. Can anyone help me? I have been stuck with this for about a week and its humiliating considering its something as simple as touch. I have it shoot a raycast with the direction being Vector2.zero, but Can someone please explain it a bit better to me? Here is the code:
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
var rayHit : RaycastHit2D = Physics2D.Raycast(transform.position, Vector2(0,0), Mathf.Infinity, 0,-Mathf.Infinity, Mathf.Infinity);
if (rayHit) {
print("Touched: " + rayHit.collider.name);
} else {
print("Hit Nothing");
}
}
}
}
My output comes out really weird, All I do is touch along down the screen, and I have 4 buttons, so I should hit nothing a bit, and hit a button once or twice, but It Gives me wrong output, like I touch one button and it gives a different button as the output, or it just says hit nothing when I can clearly tapping the button. I don’t understand what I need to do to fix it…