Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ykantbobreed · Jun 06, 2014 at 08:40 PM · raycastphysics.raycast

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges