Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Shayfen · Jul 16, 2019 at 02:03 PM · raycastplayerdetectempty

Player moving out of raycast

I have a basic raycast coming off of an enemy. It's only purpose is to detect whether the player is there or not. It is set up in the animator to trigger with certain states. If it detects the player, it switches a boolean to true and it moves to another state. That part all works perfectly fine. The problem I'm having is switching that boolean back to false. What I want to happen is that when the player is no longer detected by that raycast, it then triggers the boolean to false. Here is the code that I currently have.

 {
     RaycastHit hit;
     Ray detect = new Ray(animator.transform.position, animator.transform.forward);

     Debug.DrawRay(animator.transform.position, animator.transform.forward * rayLength);
     
     if(Physics.Raycast(detect, out hit, rayLength))
     {
             if(hit.collider.tag == "Player")
             {
                 animator.SetBool("Alert", true);
             }
             else
             {
                 animator.SetBool("Alert", false);
             }
     }
 }

To me, it looks as if it should work. It's checking if it collides with the player, and if it doesn't then it sets it to false. There's no errors or anything. It just simply doesn't work. Can you perform an action with an empty raycast, or does it actually need to be detecting something?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Shayfen · Jul 16, 2019 at 03:10 PM

After some playing around, I found the issue. It seems to be a problem with Unity. I experimented with variations of the code, including both of what you guys offered, and it still didn't work. Then I changed the exit time of the transition and it works now.

I was giving it an exit time of 2 and then it would transition to a new state. The transitions I was using that switched the boolean to true didn't have an exit time. For some reason when trying to run a condition with an exit time of 2, it just doesn't work. Changing the exit time to 1 allows the condition to trigger. That seems like a glitch in Unity.

Anyway, thanks for the help.

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
avatar image
0

Answer by Dawdlebird · Jul 16, 2019 at 02:07 PM

you will need to set it to false when you have no raycast hit at all as well:

      if(Physics.Raycast(detect, out hit, rayLength))
      {
              if(hit.collider.tag == "Player")
              {
                  animator.SetBool("Alert", true);
              }
             else 
            {
                     animator.SetBool("Alert", false);
             }
      }
      else 
      {
             animator.SetBool("Alert", false);
      }
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
avatar image
0

Answer by pyramidhead01 · Jul 16, 2019 at 02:16 PM

Try this:

 {
      RaycastHit hit;
      Ray detect = new Ray(animator.transform.position, animator.transform.forward);
 
      Debug.DrawRay(animator.transform.position, animator.transform.forward * rayLength);
      
      if(Physics.Raycast(detect, out hit, rayLength))
      {
              if(hit.collider.tag == "Player")
              {
                  animator.SetBool("Alert", true);
                  return;
              }
      }
       animator.SetBool("Alert", false);
  }

On if statements, if the final thing you want to happen turns true in the middle of a block of code, just return back at each final true moment, and then add all the false statements below that.

Comment
Add comment · Show 1 · 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
avatar image Dawdlebird · Jul 16, 2019 at 03:07 PM 0
Share

only setting it to false when you don't hit anything doesn't account for raycasting against obstacles like walls.

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

172 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 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 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 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 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 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 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 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

unity raycast layers in Multiplayer 0 Answers

Detect if player is in range? 0 Answers

raycast hit to player in network ,how?? 0 Answers

How To Detect Player Movement 1 Answer

Detect if player is in range? 2 Answers


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