- Home /
Getting object tag via raycast
So i have an object tagged as "Mover", and i want other game objects to know if they are over a mover or not, i've tried a few things but i'm a bit lost.
Basically the object recognizes it's over something and will print the first debug.log, but it won't recognize the tag, so it's not printing the second debug.log.
void Update () {
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast(transform.position, Vector3.forward, out hit , castRange)) {
Debug.Log ("I am on something...");
if (hit.collider.gameObject.tag == "Mover") {
Debug.Log ("Oh, I am on a Mover!");
}
Change your first Debug.Log statement to:
Debug.Log(hit.collider.tag+", "+hit.collider.name);
I don't know your geometry, but you are raycasting in the Vector3.forward direction which is always the world forward. You may want to add a Debug.DrawRay() to visualize your cast.
So i figured out the problem was i didn't tag the child objects. After doing that, my code worked fine.
Thanks!
Answer by jillytot · Oct 25, 2013 at 09:43 PM
I figured it out, i had to tag the children of the game object even though the game object was appropriately tagged, so my code worked fine.
Thanks!
Answer by Imankit · Oct 25, 2013 at 09:17 PM
Why dont you put a collider(say box collider) on the object mover with a height and make it as a trigger.. If an object triggers the collider you can easily find that..