Search Unity

Can't get Raycast to detect a hit on a button

Discussion in 'Scripting' started by ianr620, Aug 31, 2014.

  1. ianr620

    ianr620

    Joined:
    Jun 2, 2014
    Posts:
    3
    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.
    Code (CSharp):
    1. if (Input.touchCount == 1) {
    2.                 RaycastHit hit = new RaycastHit ();
    3.                 Touch touch = Input.GetTouch (0);
    4.                    
    5.  
    6.                 if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
    7.  
    8.                     Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
    9.                     //If Ray hit anything
    10.                     if (Physics.Raycast (ray, out hit)) {
    11.                         //If it hit button
    12.                         if (hit.collider.gameObject.tag == "arrow-up-icon") {
    13.                             //First touch hit button
    14.                             ballPlayer.rigidbody2D.velocity += Vector2.up * jumpSpeed;
    15.                             thisJump.renderer.enabled = false;
    16.                             StartCoroutine (WaitandNext (0.25f));
    17.                             //Not Touching button
    18.                         } else {
    19.                             FirstTouchingButton = false;
    20.                         }
    21.                         //Not touching anything
    22.                     } else {
    23.                         FirstTouchingButton = false;
    24.                     }
    25.                     if (FirstTouchingButton == false) {
    26.                         //Right of Screen
    27.                         if (touch.position.x > Screen.width / 2) {
    28.                             if (leftPlatform == 1) {
    29.                                 newDrag = 0f;
    30.                                 newGravity = 2.3f;
    31.                             } else if (leftPlatform == 2) {
    32.                                 newDrag = 3.5f;
    33.                             } else if (leftPlatform == 3) {
    34.                                 newDrag = 4.0f;
    35.                             } else if (leftPlatform == 4) {
    36.                                 newDrag = 0.2f;
    37.                                 newGravity = 1.8f;
    38.                             } else if (leftPlatform == 6) {
    39.                                 newDrag = 0f;
    40.                                 newGravity = 3.0f;
    41.                             } else if (leftPlatform == 7) {
    42.                                 newDrag = 2.5f;
    43.                             } else if (leftPlatform == 8) {
    44.                                 newDrag = 0f;
    45.                                 newGravity = 1.0f;
    46.                             } else {
    47.                                 newDrag = 0f;
    48.                             }
    49.                             //Left Screen
    50.                         } else if (touch.position.x < Screen.width / 2) {
    51.                             if (leftPlatform == 1) {
    52.                                 newDrag = 3.5f;
    53.                             } else if (leftPlatform == 2) {
    54.                                 newDrag = 0f;
    55.                                 newGravity = 2.3f;
    56.                             } else if (leftPlatform == 3) {
    57.                                 newDrag = 0.2f;
    58.                                 newGravity = 1.8f;
    59.                             } else if (leftPlatform == 4) {
    60.                                 newDrag = 4.0f;
    61.                                
    62.                             } else if (leftPlatform == 6) {
    63.                                 newDrag = 2.5f;
    64.                                
    65.                             } else if (leftPlatform == 7) {
    66.                                 newDrag = 0f;
    67.                                 newGravity = 3.0f;
    68.                             } else if (leftPlatform == 8) {
    69.                                 newDrag = 0f;
    70.                                 newGravity = 1.0f;
    71.                             } else {
    72.                                 newDrag = 0f;
    73.                             }
    74.                         }
    75.  
    76.  
    77.  
    78.                     }
    79.                     //No longer Touching
    80.                 } else {
    81.                     newDrag = 1.0f;
    82.                     newGravity = 1.0f;
    83.                     FirstTouchingButton = false;
    84.                 }
    85.                 //If there are 2 Touches
    86.  
    87.  
    88.             }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    For performance purposes cache the Ray, RaycastHit and filter the raycast with a LayerMask.

    Physics.Raycast(ray, out hit, Mathf.infinity, layerMask)

    Then ensure your button is on the layer you are filtering through.

    Hope this helps.