About Raycast hit;

Hello,
I am having a hard time understanding the “logic” behind the following syntaxes:

Debug.Log("I hit this thing: " + hit.transform.name);
The line hit.transform.name reads like the ray hits a transform which has a name. This looks like the transform has a name, although the name belongs to a game object.

And this:
EnemyHealth target = hit.transform.GetComponent();
This kind of looks like the ray hits a transform which has a an enemy health component. In other words a transform component which has a enemy health component. From what I know, a component belongs to a game object and a component does not have another component. The enemy health component belongs to the enemy game object.

What is this transform doing between hit and name, and between hit and get component?

Thank you for the clarification!

Hi Guybrush,

Have you already looked the parameter up in the API to figure out of what type hit is and what the class contains?

In Unity, many things are connected with one another, and we cannot apply any logic to the connections because they are not a natural law. Just because a component does not have a component itself, we are still able to call GetComponent on it to get another component which is attached to the same game object as the component on which you call the method. It was a design decision made by the creators of Unity many years ago.

Did this clear it up for you?


See also:

Hi Nina,

Thanks for the clarification. However I cannot find hit in the API. Whenever I try to look for it, I end up in Ray (struct) and RaycastHit (struct) and none of these structs contain any info on hit.

Can you please tell me the name of the class that you just mentioned which contains info on hit?

Thank you.

“hit” is just the name of a variable of the struct “RaycastHit2D” Here’s the page with all the info:

Unity - Scripting API: Physics.Raycast (unity3d.com)

Basically you just assign a variable name to the" hit" (collider) that you’ve collided with (In this case we call it ‘hit’) and from there, you can access all the properties of a gameObject as you normally would. You can check if it has a health component (say, is it something we can damage, is it an enemy, for example), you can check its tag to see if it marked as an item or an enemy, or a player, and really anything else you want.

Assigning the raycast information to the variable just basically means you can do all the checks you need to do on the “hit” aka the game object you have collided with.

So for example this line:

EnemyHealth target = hit.transform.GetComponent<EnemyHealth>();

Means, you’re assigning a new variable “target” of type EnemyHealth, accessing its “EnemyHealth” script. This assumes the hit actually has a component on it called EnemyHealth. If it doesn’t it will throw a null exception error, so it’s always good to check if it even has it before you assign it, in case the “hit” we collide with is NOT an enemy, for example!

Hi

Thanks a lot for the explanation. I appreciate it!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms