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 /
  • Help Room /
avatar image
0
Question by wesleywh · Aug 06, 2017 at 02:57 AM · airaycastingdistanceanglefov

AI - Raycast hitting itself

EDIT: After more debugging I have found that when that code block seems it actually is. I put a debug statement within the if Raycast itself but before the if debug check. Basically the raycast is so small I don't see the Gizmo being drawn. The raycast is hitting itself.

I guess the new question becomes why isn't this ignoring itself. I want to ignore the AI layer but it isn't.

How do I modify my "InOffsetRaycast" function to ignore the AI layer only but keep all others?


Original Post

I am getting some weird logic. The function labeled "CanSeeObject" will return true/false if the player is within the enemies AI sight.

  1. is in angle

  2. is within distance

  3. Raycast can hit the player

The issue i am getting is 1 and 2 execute perfect but 3 will only execute if the player is on the AI's left side (according to my debug tests). If he goes to the right side it doesn't even try to execute step 3. Super weird.

CanSeeObject

 public bool CanSeeObject(GameObject enemy, Transform eyes, float fieldOfView, float range, bool debugging=false)
             {
                 Vector3 direction = (enemy.transform.position - eyes.position)+Vector3.up;
                 // If the angle between forward and where the player is, is less than half the angle of view...
                 if (Vector3.Angle(direction, eyes.forward) <= (fieldOfView / 2) ) // within field of view
                 {
                     if (Vector3.Distance(eyes.position, enemy.transform.position) <= range)
                     {
                         if (InOffSetRaycast(enemy.transform, eyes, 0f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, -0.5f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, 0.5f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, 1f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, -1f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, -1.5f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, 1.5f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, 2.0f, debugging) ||
                             InOffSetRaycast(enemy.transform, eyes, -2.0f, debugging))
                         {
                             return true;
                         }
                     }
                 }
                 return false;
             }

InOffSetRaycast

 bool InOffSetRaycast(Transform enemy, Transform eyes, float offset, bool debugging=false)
             {
                 Vector3 direction = (enemy.position - eyes.position);
                 RaycastHit hit;
 //                int ignoreLayer = ~(1 << eyes.gameObject.layer);
                 int ignoreLayer = eyes.gameObject.layer;
                 direction = (offset == 0) ? direction : direction + (Vector3.up * offset);
                 if (Physics.Raycast(eyes.position, direction, out hit, ignoreLayer)) //draw raycast straight to object from eyes
                 {
                     if (hit.transform.root.gameObject == enemy.gameObject) //Hit the object?
                     {
                         if (debugging == true)
                         {
                             Debug.DrawRay(eyes.position, direction+(Vector3.up*offset), Color.red, 1.0f);
                             return true;
                         }
                         return true;
                     }
                 }
                 else if (debugging == true)
                 {
                     Debug.DrawRay(eyes.position, direction+(Vector3.up*offset), Color.blue, 1.0f);
                 }
                 return false;
             }

At first I thought it was a True/False logic thing... thinking false was getting the higher precedence but then I realized the gizmos weren't even being drawn. Here are some pictures to help illustrate my problem. I'm really confused here.

Is anyone able to see something the looks off about this logic?

Always hits on the left side

alt text

Will not execute "InOffSetRaycast" function if on right side

alt text

hitting.png (312.1 kB)
nothitting.png (354.3 kB)
Comment
Add comment · Show 1
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 wesleywh · Aug 06, 2017 at 08:29 PM 0
Share

In "InOffSetRaycast" if I change line:

 int ignoreLayer = eyes.gameObject.layer;

to line:

 int ignoreLayer = ~(1 << eyes.gameObject.layer);

The raycast will fail to detect my player at all. $$anonymous$$y AI's Layer = NPC (number 14) and my player's layer = Player (number 10). I would expect that to work. Since it does state that I should raycast on all layers except the NPC right?

1 Reply

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

Answer by wesleywh · Aug 06, 2017 at 08:52 PM

Honestly I don't know why this worked in the first place since the issue boiled down to the fact that I didn't specify a raycast distance.

I changed the following 2 lines in my "InOffSetRaycast" function from:

 int layerMask = eyes.gameObject.layer;

to

 int layerMask = ~(1 << eyes.gameObject.layer);

Then also changed the raycast if check from:

 if (Physics.Raycast(eyes.position, direction, out hit, layerMask))

to

 if (Physics.Raycast(eyes.position, direction, out hit, range, layerMask))

and added a float range input for the function then everything worked the same in both the right and left side.

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

156 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

Related Questions

AI Raycasting Question (Almost there!!!) 0 Answers

raycast hit distance issue 1 Answer

Raycast Information 0 Answers

GPD Pong - Trouble with Raycasting for Opponent's Look-Ahead AI 1 Answer

Need to place an object at a certain angle form the camera and at a certaine distance 0 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