Search Unity

Question How To Detect When a Physics.Raycast Stops Hitting An Object

Discussion in 'Scripting' started by michaelpynes, Jul 10, 2021.

  1. michaelpynes

    michaelpynes

    Joined:
    Sep 12, 2019
    Posts:
    79
    I have a raycast that is used to detect whether an object has a certain tag. I can change the object when the raycast hits the object, but I want to change it again when it stops hitting the object.
    The statement looks like this. Also, the objects will have a different variable that I want to change.
    Code (CSharp):
    1. if (Physics.Raycast(ray, out hitData, 25))
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    A raycast doesn't really "stop" hitting an object.

    If it hit an object last frame and doesn't hit an object this frame, that could be one definition.

    To do that case, keep track of the fact that you DID hit a particular object last frame, and when you DO NOT hit it this frame, do something.
     
    dylooboss1314 likes this.