Need help changing material back when raycast no longer hitting object.
Hi, I have a laser sight on a player which is also using a Raycast to hit an enemy and change its material to a different colour while you have the laser sight pointed at it. The idea being the enemy remains a certain colour while it's being targeted. But I'm not sure how to get the enemy material to change back to its original state when you move the laser sight off of it.
At the moment I'm using this script below to send a Raycast message to change the material in a script on the enemy.
void Update () {
laserSight.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if (enemyHealth != null)
{
enemyHealth.Targeted();
}
laserSight.SetPosition (1, shootHit.point);
}
else
{
laserSight.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
And then this is the section of script that's being called on the enemy.
public void Targeted ()
{
Material m = body.material;
body.material.color = Color.blue;
print ("targeted");
}
I'm not really sure how you change it back, as obviously once the laser sight goes off the enemy then the Targeted section is no longer called so I assume I don't do it there. I've tried using a bool in the enemy update function which is set to true when hit whilst starting a coroutine to reset it after a short time, but the problem is this keeps resetting the material whilst still targeting the so the enemy ends up flashing.
Answer by M4347 · Jan 27, 2016 at 11:09 PM
Ok I've worked out a way around this for any that are interested, though I'm sure there's a more efficient way to do this, so please still tell me if you know it.
At the moment the best way I can find to do this is by creating a small sphere set at the player position with the mesh renderer turned off so it's hidden, and set the collider to a trigger. I've then added a script to the sphere with OnTriggerStay to detect my target with a tag, then change the material in that. Then OnTriggerExit I use to set the material back to the original state. It's then just a case of setting the sphere position to the raycast hit point if the target has my enemyhealth script.
Ins$$anonymous$$d of making your material change based of a void you should do this: Use a bool as you said however ins$$anonymous$$d of simply setting a timeout make two variables. Put your timeout on one of these variables and write something like if(VariableB == true) {VariableA = true;} use your function to set VariableB to true. However ins$$anonymous$$d of simply saying wait make VariableA false you've created a buffer. The problem you had was your bool would always timeout and reset your material. Then your function would be called again and the process would repeat. If you set VariableB for your true and false you can prevent the change from ever happening (Check the status of VariableB before changing VariableA) this is a little confusing so think off it like this. You set VariableB to true every time you call your function. However you also reset your timer every time you call the function. Something like: int a = 0; void Update () { a++; If (a > 5) { b = false; //changes your material} } void Targeting (){ a = 0; b = true; //changes your material } This way you never actually change the material or set some kind of timeout unless Targeting isn't called for __ frames. Hope that helps despite it being so confusing!
Thanks for that, I have to admit I had to read the answer a few times before it sunk in! But I completely understand what you mean now! Seems I was along the right lines, just didn't think of having that second variable in the Update function to help with the timeout.
Your answer
Follow this Question
Related Questions
NullReferenceException while raycasting? 0 Answers
An probably very easy question about Raycast system... 1 Answer
Why isn't my ray working? 1 Answer
Can i use raycast for something like a keypad? 2 Answers
Raycast doesn't stay in one place 0 Answers