- Home /
detecting reycast collisions
i've searched and searched for this but no luck for what i'm looking for... ok, if i have two objects... object 1 is casting a raycast... how can i (using java) detect that the raycast cast from object 1 has collided with object 2 through a java script attached to object 2?
thanks...
Answer by Adrian · Aug 28, 2011 at 02:47 PM
Raycasts don't produce a collisions, they are strictly one-way and only the script that did the raycast knows it happened and what the result was. if you want other objects to know of the Raycast then the script casting it has to send a message.
e.g.
var hit : RaycastHit;
if (Physics.Raycast(origin, direction, hit) {
// Call OnRaycastHit method on any script on the game object hit by the raycast
hit.collider.gameObject.SendMessage("OnRaycastHit", gameObject);
}
thats great thanks, i did think it would need to be a send message i just didn't know how to go about it... but thats worked, all i changed was the "if (Physics.Raycast(origin, direction, hit) { " because it came up with 2 errors regarding "origin" and "direction" so i used " if(Physics.Raycast(transform.position, forward, hit, ray)){ "
thanks i have it working!