Search Unity

Raycast only working once

Discussion in 'Scripting' started by 8r3nd4n, Nov 23, 2011.

  1. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    I have a scene where I have several different shapes. Attached to an object that is moved around by the player is a script containing the raycast stuff. When the ray intersects an object of a certain shape, I want it do show the shape that it has intersected.

    For some reason, It will work the first time but as soon as I move off a shape, the raycast seems to do nothing and it wont recognize that its hit again the second time. It has me stumped cause I have used raycasts before and not run into this problem. It appears that it only runs the update once rather than continuously.

    Help greatly appreciated

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RaycastTriangle : MonoBehaviour {
    5.  
    6.  
    7. // Use this for initialization
    8. void Start () {
    9.  
    10. }
    11.  
    12. void Update() {
    13. RaycastHit hit;
    14.     Vector3 up = transform.TransformDirection(Vector3.up);
    15.     if (Physics.Raycast(transform.position, up, out hit))
    16.     {
    17.        if (hit.collider.gameObject.CompareTag("Triangle"))
    18.        {
    19.          print("Triangle");
    20.        }
    21.  
    22.        else if (hit.collider.gameObject.CompareTag("Circle"))
    23.        {
    24.          print("Square");
    25.        }
    26.  
    27.        else if (hit.collider.gameObject.CompareTag("Circle"))
    28.        {
    29.          print("Circle");
    30.        }
    31.  
    32.        else
    33.        {
    34.          print("No Shape");
    35.        }
    36.     }
    37.     Debug.DrawRay(transform.position, up, Color.green);
    38.  
    39. }
    40. }
     
  2. Tazadar

    Tazadar

    Joined:
    Oct 1, 2011
    Posts:
    91
    Code (csharp):
    1.  else if (hit.collider.gameObject.CompareTag("Circle"))
    2.        {
    3.          print("Square");
    4.        }
    There is a mistake here it is not Circle but Square
     
  3. Denifia

    Denifia

    Joined:
    Nov 16, 2011
    Posts:
    32
    I dont think this will help, but try using FixedUpdate.
     
  4. Denifia

    Denifia

    Joined:
    Nov 16, 2011
    Posts:
    32
    Nice spot Tazadar - I fully didn't even see that.
     
  5. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Thanks guys but it will still only work once even with FixedUpdate
    And thanks for pointing out the typo Tazadar but it still didnt affect the behaviour
     
  6. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Damn it I was pulling my hair out for so long and as usual the answer is so simple I can only laugh at my stupidity.

    I was always checking the console for the hit detection but had the 'collapse' button toggled so it would only show it the first time.

    ha ha ha
     
    Little_daemon likes this.
  7. msleeper

    msleeper

    Joined:
    Feb 14, 2011
    Posts:
    86
  8. Kinnith7

    Kinnith7

    Joined:
    Jul 4, 2017
    Posts:
    84
    hahaha! I have been trying to figure this out on my own program for hours now. And I just read this and I was doing the same thing. Glad I stopped on this article. Thanks, 8r3nd4n for figuring this out. And the laugh at myself. :D
     
  9. magnanimous_productions

    magnanimous_productions

    Joined:
    Jul 11, 2020
    Posts:
    1

    A decade later and your headache is still helping, thanks
     
    mustafahtp3 likes this.