I’ve been trying to create a proof of concept for a facial animation system, and want to add viewport controls to it. For some reason, when my raycasting returns true the first time, it remains true forever. I’ve tried everything I know how (which isn’t much) to force it to return to false, but nothing seems to work.
How do you get a raycast to return false again once it’s returned true?
using UnityEngine;
using System.Collections;
public class Carla_Face_Selection : MonoBehaviour {
public Collider mouthCollider;
public Collider eyesCollider;
private bool mouthActive;
private bool eyesActive;
private bool currentlyHitting;
public Camera camera;
public RaycastHit whatDidIHit;
private Ray ray;
private Vector3 rayDirection;
// Use this for initialization
void Start () {
currentlyHitting = false;
mouthActive = false;
eyesActive = false;
}
// Update is called once per frame
void Update () {
currentlyHitting = false;
mouthActive = false;
eyesActive = false;
// Find the center of the screen
rayDirection = new Vector3 (0, 0, 1);
// Using screen center and our direction (found above), create a ray. Essentially a vector 6
ray = new Ray(camera.transform.position, rayDirection);
//test the ray
Debug.DrawRay(camera.transform.position, rayDirection * 10, Color.cyan);
//Returns true if a collider is hit
if (Physics.Raycast(ray, out whatDidIHit))
{
currentlyHitting = true;
}
else
{
currentlyHitting = false;
}
if (whatDidIHit.collider == mouthCollider && currentlyHitting == true)
{
mouthActive = true;
}
else if (whatDidIHit.collider == eyesCollider && currentlyHitting == true)
{
eyesActive = true;
}
else if (currentlyHitting == false)
{
mouthActive = false;
eyesActive = false;
}
print("We are currently hitting: " + currentlyHitting+ ".");
}
}
For reference, I’ve double checked that each of my public colliders and my camera are correctly associated with my script through the inspector. I really appreciate the help!