Raycasting Question?

Hello guys,

I was about to try something but before I started on it and have it not working and tearing my hair out from it not working and wondering what i had stuffed up I thought I run it by you to see if it will work beforehand.

Now what I am trying to do is get the cross_hair to change colour when an “enemy” crosses into the target zone - eg the raycast. Except it won’t be an enemy because I don’t have one yet, or know how to do it - yet! I would suppose it’s just an imported fully rigged model - I have some of those. So as a stand-in it will be a marble block tagged as an “enemy” :slight_smile: - proof of concept I suppose.

Now the way I figure it ‘could’ work would be to set an OnCollisionEnter/OnCollisionExit on the “enemy” object that would send a bool variable to the script that sets the crosshair, so, it would change colour. Now would that work? You’ve got to remember I am a noob. Or is there a better way to approach this?

As always a big thankyou in advance to anyone who can help!

Raycasting seems like a good option, something like this should work.
Make sure your enemy object has some sort of collider on it, so the raycast has something to hit!

RaycastHit hit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000f);
if(hit.collider.gameObject.tag == "Enemy") {
    // Raycast hit an object tagged as enemy
}

Oh, ok, never thought of doing it that way! I think that should work and might be a little bit more efficient than what I was thinking.

Thanks greatly for that :sunglasses:.