- Home /
Why won't Physics.Raycast return false after it's first return of true?
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!
Answer by oasisunknown · Jun 07, 2014 at 12:02 AM
I think fundamentally your using the raycast wrong. you have no distance deceleration on your ray which I dont think it would default to shoot out a ray forever.
also I think the way your finding the middle of the screen should work but possibly try.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
I have used that code to shoot out a ray from the camera to wherever my mouse is currently pointing. (when clicked).
if(Physics.Raycast(ray, out hit, rayDistance))// draws the ray defined above.
{
if(hit.transform.tag == "Obj")
{
//Do Something
}
}
Another thing to note is that I dont believe your whatAmIHitting persists as a variable outside of the raycast if statement so you probably have to move those if checks up and into a nested if statement.
I think thats all I am going to comment for now because you may have to restructure your code but to help you out with the raycast I am going to link you to a tutorial about it. the tutorial is in the Live training arhives and is called fun with explosions (its not really about explosions and more about AddForce) but I think it would have some good information that would help you out.
the relevent code to raycasting starts at around 13 minutes.
Unity Live Training Archive: Fun With Explosions
I hope this helps at least get you started in the right direction.
also the Raycast documentation.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Your answer
Follow this Question
Related Questions
Problem with converting RaycastHit to Vector3 2 Answers
Maintain object position when using raycasting to multiple height 2 Answers
Physics.Raycast sometimes does not work? 0 Answers
Raycast distance affected by momentum of character 0 Answers
What does direction do for a physics.raycast besides direction? (read details) 1 Answer