Search Unity

How to stop raycast when hitting an object in Unity?

Discussion in '2D' started by drpelz, Apr 9, 2021.

  1. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    Hi,

    in my Unity game (top-down 2D shooter) there are some enemies flying along a path (via DOTween). My player casts a ray into the scene in order to get an enemy-object right in front of the player. So far so good.

    Now the problem is that I want only one enemy-object as a result, i.e. the raycast should stop when it hits an enemy-object for the very first time. How do I achieve this?

    I need only one enemy-object hit by the raycast because there is a crosshair in my game and when the raycast starts and the enemies fly along the path the crosshair jumps forth and back (and I don't want this - the crosshair should stay at the first enemy hit by the raycast). Any help, please?

    raycast-video

    This is my code (attached to the player):

    Code (CSharp):
    1. void FixedUpdate() {
    2.      
    3.         //crosshair: Cast a ray straight up.
    4.         float _size = 12f;
    5.             Vector2 _direction = this.transform.up;
    6.             RaycastHit2D _hit = Physics2D.Raycast(this.transform.position, _direction, _size);
    7.  
    8.         if (_hit.collider != null && _hit.collider.tag == "EnemyShipTag") {
    9.  
    10.           // We touched something!
    11.  
    12.           Vector2 target = new Vector2(_hit.collider.gameObject.transform.position.x, _hit.collider.gameObject.transform.position.y);
    13.  
    14.             const float moveTime = 0.1f;
    15.             float step;
    16.  
    17.           step = Vector2.Distance(crosshairGO.transform.position, target);
    18.             crosshairGO.transform.position = Vector2.MoveTowards(crosshairGO.transform.position, target, step / moveTime * Time.deltaTime);
    19.  
    20.             Vector2 _pos3 = new Vector2(this.transform.position.x, crosshairGO.transform.position.y);
    21.             crosshairGO.transform.position = _pos3;
    22.             crosshairBegin = false;
    23.        } else {
    24.           // Nothing hit
    25.  
    26.           Vector2 _pos2 = new Vector2(this.transform.position.x, 4.5f);
    27.             if (crosshairBegin) {
    28.                 crosshairGO.transform.position = _pos2;
    29.             } else {
    30.                 Vector2 _pos4 = new Vector2(this.transform.position.x, crosshairGO.transform.position.y);
    31.                 crosshairGO.transform.position = _pos4;
    32.             }
    33.        }
    34.  
    35.  
    36.     }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    My initial thought is to add a bool like "targetFound" or something and then make an IF condition that determines whether or not the raycast should be used. Then, you could use a timer or some other function to change that "targetFound" bool back to false and then the raycast would activate again until it finds another enemy.

    Code (CSharp):
    1. if(targetFound == false)
    2. {
    3.     RaycastHit2D _hit = Physics2D.Raycast(this.transform.position, _direction, _size);
    4. }
    Maybe something like that?
     
    drpelz likes this.
  3. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    Last edited: Apr 10, 2021
  4. Xarnowit

    Xarnowit

    Joined:
    Apr 8, 2014
    Posts:
    5
    I think you may be misunderstanding what is happening. To me it looks like problem is not that raycast hits multiple objects, but that there are moments when raycast doesn't hit front "row" of enemies so it goes through to back row, and so naturally your crosshair will move because enemy hit is in the back.

    So it's not so much problem with code but design issue. I think it would be easier to notice if you did debug drawing of rays / hitboxes
     
  5. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    @Xarnowit: Yes, there are gaps and then the ray hits the back row. I understand the problem now. Thanks for your help, dude!:)