- Home /
Receiving information about getting hit by a Raycast
Good morning, afternoon, evening . . . night!
This may well be quite an inexperienced and foolish question, so bear with me here.
Though I'm currently considering the possibility of messing about with raycasting. And therein lies my question. as though it's possible to receive information about which objects the ray has undoubtedly collided with. Is it possible to receive information if your desired collider has been hit by a raycast?
Similar in sense that you can track/trigger generic collisions, is it possible to track raycast collisions outside of the script that initiated it?
Sorry, but no. You can just simply have the script that instantiated the raycast to toggle a bool or whatever to do so. But outside the instantiated script, it's kind of pointless unless you're not able to tell it that it got hit.
Answer by robertbu · Aug 03, 2014 at 07:45 AM
There's no standard feature for this functionality, but it is trivial for the Raycaster to inform a script on the object it hit that the hit occurred. It can be done using GetComponent() or SendMessage() in just a line of code or two assuming there is a script on the objects with the collider to 'receive' the information. In C# you could use Events and Delegates to inform any script that wanted to know what object(s) are hit.
Thank you so much for the response,
Though it's the answer I was secretly hoping for, it's definitely still a relatively beneficial answer all the same.
It's a shame no such standard feature yet exists, though this shouldn't (hopefully) be too much of an issue
No reason you can't extend physics to let you add your own raycast that does this. Then you can pretend its standard.
Answer by idlemurmurs · Aug 08, 2014 at 06:17 AM
Discovered/Designed a potential answer to my own mullings,
So here's merely how I've chosen to undertake that particular issue (for anyone who may need it):
private DesiredScriptName scriptCommunication;
// =============================================== //
// Designates A Permanent Raycast That Will Determine Whether The Desired GameObject Has Been Collided With
// Allows Direct Communication With The Specific "Desired" GameObject That Was Interacted With
if (Physics.Raycast (transform.position, directionRay, out collisionRay, distanceRay) )
{
if (collisionRay.collider.gameObject.tag == "Desired GameObject Tag" )
{
scriptCommunication = destinationTransform.gameObject.GetComponent <Desired Script> ();
scriptCommunication.designatedboolean = true;
}
}