I’m currently making a 2d game for ios with touch controls. I have a whole script set up for touch input that has always worked but I cant seem to add a jump to it. I have raycasts to detect if a touch hits the jump button but it doesnt seem to be firing. The touch controls work as usual but hitting the button does nothing. I have 2 different areas of code for if there is one touch on screen or 2. I can post the code for 2 touches if it is needed for clarification.
if (Input.touchCount == 1) {
RaycastHit hit = new RaycastHit ();
Touch touch = Input.GetTouch (0);
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
//If Ray hit anything
if (Physics.Raycast (ray, out hit)) {
//If it hit button
if (hit.collider.gameObject.tag == "arrow-up-icon") {
//First touch hit button
ballPlayer.rigidbody2D.velocity += Vector2.up * jumpSpeed;
thisJump.renderer.enabled = false;
StartCoroutine (WaitandNext (0.25f));
//Not Touching button
} else {
FirstTouchingButton = false;
}
//Not touching anything
} else {
FirstTouchingButton = false;
}
if (FirstTouchingButton == false) {
//Right of Screen
if (touch.position.x > Screen.width / 2) {
if (leftPlatform == 1) {
newDrag = 0f;
newGravity = 2.3f;
} else if (leftPlatform == 2) {
newDrag = 3.5f;
} else if (leftPlatform == 3) {
newDrag = 4.0f;
} else if (leftPlatform == 4) {
newDrag = 0.2f;
newGravity = 1.8f;
} else if (leftPlatform == 6) {
newDrag = 0f;
newGravity = 3.0f;
} else if (leftPlatform == 7) {
newDrag = 2.5f;
} else if (leftPlatform == 8) {
newDrag = 0f;
newGravity = 1.0f;
} else {
newDrag = 0f;
}
//Left Screen
} else if (touch.position.x < Screen.width / 2) {
if (leftPlatform == 1) {
newDrag = 3.5f;
} else if (leftPlatform == 2) {
newDrag = 0f;
newGravity = 2.3f;
} else if (leftPlatform == 3) {
newDrag = 0.2f;
newGravity = 1.8f;
} else if (leftPlatform == 4) {
newDrag = 4.0f;
} else if (leftPlatform == 6) {
newDrag = 2.5f;
} else if (leftPlatform == 7) {
newDrag = 0f;
newGravity = 3.0f;
} else if (leftPlatform == 8) {
newDrag = 0f;
newGravity = 1.0f;
} else {
newDrag = 0f;
}
}
}
//No longer Touching
} else {
newDrag = 1.0f;
newGravity = 1.0f;
FirstTouchingButton = false;
}
//If there are 2 Touches
}