- Home /
Can an object check if a raycast is no longer hitting it?
I want an object and its children to have a certain shader, but only while its being hit with a raycast by a player. Changing the shader initially on hit is easy enough using GetComponentsInChildren, but I'm not sure of an efficient way of changing it back to its original shader (diffuse) if its no longer being hit. Is this kind of task even up to an object to do for itself, or is it better to be set by the players raycasting script?
I was thinking something along the lines of storing the hit object as a variable ('hitObject'), and then using the 'else' part of the raycast check to say:
hitObject.GetComponent().material.shader = Shader.Find("Diffuse");
or something to that effect. The problem is that that would be player-side, not object-side, and always firing when the raycast isn't hitting something which seems like a waste. I'm pretty new at this, so I don't know what level of efficiency to be aiming for... but I feel it could be done simpler.
Answer by Loius · Jun 30, 2014 at 07:16 PM
Raycast isn't an event-driven action, so there's no way to listen for "not being hit by raycast". Whoever is casting the ray is responsible for notifying everyone of the ray's actions.
You'd just need to keep track of the last thing hit -
GameObject lastHitByRay;
void Cast() {
GameObject nowHitByRay = // raycast result
if ( lastHitByRay && lastHitByRay != nowHitByRay ) lastHitByRay.SendMessage("OnRayHitEnded");
lastHitByRay = nowHitByRay;
nowHitByRay.SendMessage("OnRayHitBegan");
}
Keep in mind that solution is a little dumb, but it hopefully gets the idea across.
Your answer
Follow this Question
Related Questions
How to Reset Shader Replacement in Scene View? 1 Answer
I'am new to shaders and I'am making games for mobile devices should I switch over to LWRP? 0 Answers
How to transform GLSL shaders into ShaderLab shades? 0 Answers
Does static batching work with replacement shaders? 0 Answers
How can i combine shaders? 0 Answers