Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Start coroutine on a target hit by a raycast ?

Discussion in 'Scripting' started by jackchovet, Oct 18, 2020.

  1. jackchovet

    jackchovet

    Joined:
    Feb 21, 2018
    Posts:
    3
    Hello there, I'm currently struggling with the following issue :

    I am using a raycast to interact with gameObjects in my game, basically saying "if they are in range and you press E interaction time"; however I am currently attempting to make it so that upon pressing interact, the object hit by the raycast, which will always have a certain script, start a coroutine.
    However I find myself unable to do so. Any help would be much appreciated.

    Is there perhaps a way for the gameobject to detect if he is being hit by a raycast ?

    If you need any further information in order to assist, feel free to ask them and I'll do my utmost to answer as best I can.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    On the object being hit:
    Code (CSharp):
    1. public void StartDoingSomething() {
    2.   StartCoroutine (MyCoroutine());
    3. }
    In the raycast code:
    Code (CSharp):
    1. if (Physics.Raycast(..., out RaycastHit hit)) {
    2.   hit.collider.GetComponent<MyScript>()?.StartDoingSomething();
    3. }
    Obviously replace the names of things with your actual script and method names.
     
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    How are you performing your raycast? When a raycast is performed, you have a parameter for a Raycasthit, that variable will provide information on what it has hit. From that, you can look for the class on the hit object and call a method on it if it is there.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You can output the results of a Physics.Raycast to a RaycastHit, which contains the Collider & Transform reference of the object it hit. From there, you can reference the GameObject by either using
    collider.gameObject
    or
    transform.gameObject
    , followed by
    GetComponent
    to get the script on that object you're looking for, and finally, starting a coroutine on that script.

    Example:
    Code (CSharp):
    1. public class SomeScript : MonoBehaviour {
    2.    public void DoTheThing() => StartCoroutine(TheThing());
    3.  
    4.    private IEnumerator TheThing() {
    5.       //etc...
    6.    }
    7. }
    Code (CSharp):
    1. public class SomeOtherScript : MonoBehaviour {
    2.    void Raycast() {
    3.       RaycastHit hit;
    4.  
    5.       if(Physics.Raycast(/*etc..*/)) {
    6.          if(hit.transform.gameObject.TryGetComponent(out SomeScript someScript)) {
    7.             someScript.DoTheThing();
    8.          }
    9.       }
    10.    }
    11. }
    (If you're not familiar with
    TryGetComponent
    , see the docs: https://docs.unity3d.com/ScriptReference/Component.TryGetComponent.html)
     
    Last edited: Oct 18, 2020
  5. jackchovet

    jackchovet

    Joined:
    Feb 21, 2018
    Posts:
    3
    Thank you very much for the answers, I'll be sure to try them out as soon as I can !