- Home /
Something like OnTriggerExit for Raycasts?
Hello, I'm looking for a something like OnTriggerExit for the Raycast. I've made a script: when the Raycast touch the tagged object, his material change, but when the raycast leave it, the material stay... I want it to return to his anciant material :/
Some help?
Cordially.
Raycasts don't "leave" something. They are instantaneous, and either hit something or not.
i've find something but that's not very pretty... It's a problem :/
Answer by Tekksin · Jun 13, 2014 at 09:36 PM
I don't work with raycasts because they're taxing, but do you have an else statement following the if? IE:
if(raycasthit){ change color; } else{ return color; }
the process to return the color should be the same as when you applied it, so it shouldn't be hard. I imagine it's something like, "else if(raycast.hit == null)" or "else if(raycast.hit != this.gameObject" or something, but like I said, I don't go near em.
No because i use tags, so this way doesn't work (i've tested)
Answer by V-Jankaitis · Mar 27, 2015 at 04:40 PM
void Hit_Target() // put this in update function
{
Ray ray = Cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<Block_Piece>() != null) // If hit object has needed script (avoid highliting other objects)
{
if (Target != hit.collider.gameObject) // if I`m not already looking at it
{
if (Target != null) // if no object selected;
{
Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
}
Target = hit.collider.gameObject; // make object my target
Target.GetComponent<Block_Piece>().Hit(true); // call it`s script
}
}
else if (Target != null) //
{
Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
Target = null;
}
}
else if (Target != null) //
{
Target.GetComponent<Block_Piece>().Hit(false); // Reverse the old one back in here
Target = null;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rays and tags help? 1 Answer
NullReference when checking tag via Raycast. How to solve this? 1 Answer
3rd Person camera collision 2 Answers
Raycasting and Tag Checking in C#? 1 Answer