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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Write ray hit behaviour on gameObject being hit

Discussion in 'Scripting' started by acasta, Jun 26, 2022.

  1. acasta

    acasta

    Joined:
    Feb 10, 2018
    Posts:
    8
    Hello,

    I'm doing a classic gun shooting a ray for a game. I want a way to handle the hit objects differently depending on their type, (NPC, dynamic objects, environment etc), which leads to a lot of code on the gun class.

    I am looking for a way to simply detect if the ray hit something, then invoke a function on that object, with the RaycastHit information passed as a parameter.

    I've added a 'handler' script that objects being hit will have, and that will point to a specific method on the object.

    The issue I have is that I can't pass the raycasthit as a parameter using Invoke(), and I can't use a coroutine as I get this error in the HitHandler:
    Non-invocable member 'name' cannot be used like a method.


    On the gun
    Code (CSharp):
    1.     public void Shoot ()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(barrelPoint.transform.position, barrelPoint.transform.forward, out hit))
    5.         {
    6.             try
    7.             {
    8.                 hit.transform.gameObject.GetComponent<HitHandler>().CallHitMethod(); // Want to pass hit as parameter here, which works.
    9.  
    10.             }
    11.             catch (Exception e)
    12.             {
    13.                 Debug.Log(e);
    14.             }
    15.         }
    My hit handler. The method to be called is inserted via the editor.

    Code (CSharp):
    1. public class HitHandler : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     UnityEvent OnHit;
    5.  
    6.     public void CallHitMethod()
    7.     {
    8.         OnHit.Invoke(); // Want to pass hit as parameter here, which doesn't work. Also tried coroutine.
    9.     }
    10. }
    The above handler would point to this class & function, where I'd handle the hit.

    Code (CSharp):
    1. public class Target : MonoBehaviour
    2. {
    3.     void OnBulletHit ()
    4.     {
    5.         Debug.Log("Ouch");
    6.     }
    7. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    Have I got something for you my friend... you are gonna LOVE this stuff. In fact, when I first learned about this stuff, I made the same exact gesture you are making in your avatar: pew pew pew!

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
    acasta likes this.
  3. acasta

    acasta

    Joined:
    Feb 10, 2018
    Posts:
    8
    Kurt-Dekker, you beauty. That worked great! Thanks.
     
    Kurt-Dekker likes this.