- Home /
How to get a rayhit to detect tags?
I can't figure it out. I am trying to get something along the lines of if(rayhit.Tag == "Bob")
Comment
Best Answer
Answer by AlucardJay · Mar 24, 2013 at 01:58 PM
a ray hits a collider
a collider is attached to a gameObject
a gameObject has the name and tag
.
Debug.Log( "Ray hit (name) : " + hit.collider.gameObject.name );
Debug.Log( "Ray hit (tag) : " + hit.collider.gameObject.tag );
example :
#pragma strict
function Start()
{
}
function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
CastRay();
}
}
function CastRay()
{
var hit : RaycastHit;
var castRayFrom : Ray = Camera.main.ScreenPointToRay( Input.mousePosition );
if ( Physics.Raycast( castRayFrom, hit, Mathf.Infinity ) )
{
Debug.DrawLine( Camera.main.transform.position, hit.point, Color.red, 0.5 );
Debug.Log( "Ray hit (name) : " + hit.collider.gameObject.name );
Debug.Log( "Ray hit (tag) : " + hit.collider.gameObject.tag );
}
}
Further information : http://www.youtube.com/watch?v=Sj4oxfQqzhA
Thanks! I tried something similar before but now it works.
Your answer
Follow this Question
Related Questions
Turret With Raycast Camera 1 Answer
What am I doing wrong with this collision code? 4 Answers
NullReference when checking tag via Raycast. How to solve this? 1 Answer
Raycasting and Tag Checking in C#? 1 Answer
Raycast on touch 3 Answers