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 DeadKenny · Aug 06, 2013 at 02:04 PM · c#raycastaihit

Raycast & AI problem.

I have this here AI and it is retarded. The problem is that I can't get it to stop seeing the target it looks at when going out of view like going behind another gameobject with collider.

I had actually gotten it to work(in a very bad way) before but it was all in LateUpdate which was making it lag and just kind go crazy. It just looked wrong in the LateUpdate too.

So after I gave the raycasting its own void the thing stopped detecting the colliders. The other codes I have seen don't use late update to do it so I know its something else.

So let me clarify. The rays and raycasting is working but its not detecting other colliders and or is ignoring them utterly. The AI does however detect the player with the tag and acts accordingly. It also does not detect the maximum distance of the rays which if the target exceeds then it is supposed to stop doing some things, which it does not.

Anyway here is the code or at least the part where the problem is coming from. Main part being the RayCasting().

     void Update (){
             
         AIRayCasting();
         
     if(canFollow == true){
             
            Following();
           
     if(stopFollow == true){    
         
         StopFollowing();
     
         }
         
         }
     }
 
     void AIRayCasting(){
         
         
         numRays = fovAngle;
         currentAngle = fovAngle / -2;
             
         hits.Clear();
 
         for (int i = 0; i < numRays; i++)
         {
         

direction = Quaternion.AngleAxis(currentAngle, transform.up) * headdummyBone.forward;

            RaycastHit aimhit = new RaycastHit();
         
 if(Physics.Raycast(headdummyBone.position, direction, out aimhit, maxSeeDistance)){
                  
             hits.Add(aimhit);

             currentAngle += 1f;
                     
     Debug.DrawRay(headdummyBone.position, direction * maxSeeDistance, Color.blue);
             

          if(aimhit.transform.tag == "Player"){ 
                     
             canFollow = true;
             looking = true;
   
             target = aimhit.transform;
                     
                     
                 }else{
                                 
                     StopFollowing();
                         
                     }
             
                 }
     
             }
         }
             
 

Hope I was clear.

Comment
Add comment · Show 5
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 gardian06 · Aug 06, 2013 at 02:27 PM 0
Share

you do know you are contradicting yourself when you say "The rays and raycasting is working" then every thing that is said after just contradicts that.

a few things on inspection:

  • why is StopFollowing() being called in both Update(), and the AIRayCasting(). pick one.

  • where you are casting the ray from headdummyBone are you sure this bone is not set high enough to see over objects?

  • are you sure that maxSeeDistance is being constrained correctly?

avatar image DeadKenny · Aug 06, 2013 at 02:38 PM 0
Share

StopFollowing() in AIRayCasting() is there just to test if it stops following. It will be something else in final.

headdummyBone is high enough I can see its detecting the player when I move into sight. Also the debug rays show me too.

The only problem is that it doesn't detect when player is behind something or out of range, just keeps going at the player.

avatar image gardian06 · Aug 06, 2013 at 02:48 PM 0
Share
  • where does canFollow get set to false?

  • what is looking used for (where is it set to false)?

  • your else feels like a fall through more then a else, and once you find the player you should stop the for loop.

  • there should be a break after finding the player.

avatar image DeadKenny · Aug 06, 2013 at 03:33 PM 0
Share

Yo yeah the problem is co$$anonymous$$g at the else. I don't know how to make it stop for the loop. Can you show me?

avatar image DeadKenny · Aug 06, 2013 at 03:55 PM 0
Share

No worries its solved. Thanks for input.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 06, 2013 at 02:32 PM

It is hard to tell what is going on with this thin slice of your code. 'canFollow' is never set to false here. We don't see how 'stopFollow' get set, and it looks like you are calling StopFollowing() in one too many places. Plus you don't bail out of your 'for' loop if the player is spotted, which means that StopFollowing() gets called every time the raycast fails even if some of the raycasts succeed. This is likely the cause of your problem. Try putting a 'break' statement on line 18.

Note rather than raycast a bunch of rays, why not just check the angle between your forward and the player? Something like:

 Vector3 v3 = player.position - transform.position;
 if (Vector3.Angle(v3,  headdummyBone.forward) < fovAngle / 2) {
    // Sees the player
 }

This will behave differently than your current code since it will also spot the player if he is below or above your fan of rays.

And a RaycastHit is a struct, so you don't have to do a 'new' to initialize it.

Comment
Add comment · Show 5 · 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 DeadKenny · Aug 06, 2013 at 03:38 PM 0
Share

The problem is at this part. The break you said I must add is above it like so right? It doesn't really fix the problem but it does detect the distance now I think. Though I can tell for sure because its not stopping at else{}.

}

if(aimhit.transform.tag == "Player"){

    canFollow = true;
    looking = true;
 
  target = aimhit.transform;
 
 
   }else{
 
       StopFollowing();
 
       }
 
   }
 
  }

}

avatar image robertbu · Aug 06, 2013 at 03:41 PM 0
Share

There's no break statement in the code you pasted in the comment. You want to add '`break;`' at line 18. Or in the code you just pulled out in the comment, it would go on line 5.

avatar image DeadKenny · Aug 06, 2013 at 03:43 PM 0
Share

Oh thats a break.

lol.

avatar image DeadKenny · Aug 06, 2013 at 03:53 PM 0
Share

O$$anonymous$$! cool, it worked THAN$$anonymous$$S!

Thank you...

avatar image DeadKenny · Aug 06, 2013 at 03:54 PM 0
Share
  canFollow = true;
    looking = true;
  
  target = aimhit.transform;
  
  break;
   }else{
  
       canFollow = false;
  
       }
  
   }
  
  }
 }

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

15 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

Related Questions

How can I get a character to patrol and follow terrain? 1 Answer

How do I make it know what it's hitting? 3 Answers

[C#] Raycasts and Object Tags 1 Answer

How to make "enemy" have a frustum(cone) view and not a circle radius 2 Answers

The name `target' does not exist in the current context 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