- Home /
Detect when object is no longer hit by Raycast
Hi All,
At first this seem like a simple idea, but I can't seem to figure out how to do it.
I have a raycast, and when it hits an object with tag "key", it sets a boolean in script on that object to TRUE. Easy. What I'd like is for when the the raycast stops hitting that same object, the boolean reverts to FALSE.
Here's what I got so far, amongst many other bizarre attempts.
private bool Key;
public Camera camera;
void Start() {
camera = GetComponent<Camera>();
}
void Update() {
Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
{
if ((Physics.Raycast(ray, out hit, 100)) && (hit.collider.tag == "Key"))
{
Key = hit.collider.gameObject.GetComponent<Activated>().active;
Key = true;
}
else if (Key != null)
{
Key = false;
}
}
}
}
put $$anonymous$$ey = false;
in in your Update first line. problem solved.
Thanks for the comment, but that will just lock that Boolean to FALSE if I have it at the start of the Update function.
But if your condition is correct it would make it true also.
Answer by HenryStrattonFW · Nov 17, 2015 at 06:04 PM
Try swapping your update to this.
void Update()
{
Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
{
if (Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.tag == "Key")
{
Key = hit.collider.gameObject.GetComponent<Activated>().active;
Key = true;
}
else
{
// Hitting something else.
Key = false;
}
}
else if (Key == true)
{
// not anymore.
Key = false;
}
}
}
This will ensure that the Key bool will get set to false in either of the two cases:
We didn't hit anything, therefore we didn't hit the key.
We hit something that isn't the key, therefore we aren't hitting the key.
This works perfectly ! Thanks a lot. In the meantime, I did find another way around it, buy resetting the $$anonymous$$ey
to null
in the if else
.
What I'm trying to add to this now though is to be able to keep the key on true
if I need to. So, for example, if you press the space bar while the object is getting hit by the raycast and $$anonymous$$ey is set to true
, then the key stays on true
even after you stop looking at it. I'm trying this, which always goes back to false for some reason:
private bool keyStop = false;
void Update()
{
Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
{
if (Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.tag == "$$anonymous$$ey")
{
$$anonymous$$ey = hit.collider.gameObject.GetComponent<Activated>().active;
$$anonymous$$ey = true;
if (Input.Get$$anonymous$$eyDown("space"))
{
$$anonymous$$eyStop = true;
}
}
else if (keyStop == false)
{
// Hitting something else.
$$anonymous$$ey = false;
}
}
else if (($$anonymous$$ey == true) && (keyStop == false))
{
// not anymore.
$$anonymous$$ey = false;
}
}
Answer by starikcetin · Nov 17, 2015 at 06:39 PM
private bool hitFlag = false;
void Update()
{
if(Physics.Raycast(...))
{
hitFlag = true
}
else if (hitFlag)
{
hitFlag = false;
// there you go. here we have object hitted on previous frame but not on this frame.
}
}