Identifying GameObject.Tag through raycasting

I want to use raycast to get the tag of the game object that is in front of the player and if the player is closer than .5 meter to it and the player presses the action key then do a certain action.

I looked through the scripting reference on raycast and raycasthit but I couldn’t figure out how to retrive the tag of the hit object.

It depends on which language you use and how you are performing this raycast.

In a general case, people create a ray that goes from the camera, through it to the mouse cursor. Then, they use Phyics.Raycast and feed this ray into it. That function has an ‘out’ param called hitInfo (you can call it what you want really as long as you declare it beforehand so you can access it after).

Here is an example C#

private RaycastHit hit;

Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hit, 1000f );

// Then you could find your GO with a specific tag by doing something like:
if(hit.gameObject.tag == "Player") {
    // Do stuff.
}

Thanks for the reply Digitalos,

I should have been more clear in my post. The game I am making is a third person orbit cam where the player can orbit the camera around the character, so the ray should come out of the player himself and go directly in front and not screen relative.

Also, I am using javascript.

ok, I used this code for javascript and my third person character that the user can orbit the camera around:

		var hit : RaycastHit;
	
	if (Physics.Raycast (transform.position, Vector3.forward, hit)) 
	{
	print (hit.collider.tag);
	Debug.DrawLine (transform.position, hit.collider.transform.position,Color.red);
	}

Now the problem is that the line is drawn always in front of the player, if the character rotates to the opposite direction the line remains where it was orginaly shot.

Also, the hit location is now the middle of the target object (so if you are in front of a thick wall the ray hits the middle of the brick and not the very first surface the player sees in front of him) [/code]

any suggestions for this?

I had a problem like this before, look at the Third PLatform tutorial, the main character has a attack script that you could get something from.

Hiya, it’s probably because you are raycasting with the world forward (z-axis) position. Convert it from world space to local and then cast with it. Check the script reference for “Transform”. It has the functions you need and also yes, check the 2D Platformer tutorial on the Unity site.

I tried the following:

relative = transform.InverseTransformDirection(transform.position);

if (Physics.Raycast (relative,Vector3.forward, hit)) 
{
print (hit.collider.tag);
Debug.DrawLine (relative, hit.collider.transform.position,Color.red);
}

so I replaced transform.position which I used earlier with a conversion of it. It draws a forward line for a second (same direction as before) then the line disappears.

I looked at the punch script in the platformer tutorial, you basically create a gizmo and check against its collisions, but I would really like to do this with Raycasting because I know I will be using raycasting for other similar features in the future.

any help on converting from world space to local space?

Just use transform.forward instead of Vector3.forward. It’ll use the forward vector for the model’s current rotation.

Physics.Raycast(transform.position, transform.forward, hit);

If the model isn’t the one holding the script, just give the camera script the variable var player : Transform;, drag the in-game character onto it, and then use player.forward.

If the player is created after the camera, then tag the player “Player” and set the variable like this: player = GameObject.FindWithTag(“Player”).transform;

using transform.forward instead of Vector3.forward worked fine. Thank you GargerathSunman.

3 years after this answear was created, and still helping ppl
<3

1 Like

C# shows issues btw:
Type UnityEngine.RaycastHit' does not contain a definition for gameObject’ and no extension method gameObject' of type UnityEngine.RaycastHit’ could be found. Are you missing an assembly reference?

1 Like