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 09, 2013 at 01:06 PM · c#raycastairaycastingraycasthit

Another Raycast Issue.

This is related to my previous question.

This time the problem is that the raycast is not working well, not that it isn't working. I tried many things but I just can't get it right.

The problem: Even though the rays are cast they don't cast out properly. It instead seems to use one ray as the main ray and almost pivots the rest of the rays on it. Its the first ray in number of rays. The rays cast a different number depending on the angle and when facing say south, only one ray or none are cast.

I don't know what the hek is going on.

Here is the code.

  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);

                  int hitsIn = 0;

                  foreach(RaycastHit rhit in hits){
                        if(rhit.transform && rhit.transform.tag == "Player"){ 

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

 } //You were missing a "}" maybe a copy error?
 //Peter G
Comment
Add comment · Show 19
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 09, 2013 at 02:42 PM 0
Share

Oh yeah that is a copy error.

To rephrase, the code has no errors and is partially working. The main problem is that the rays don't cast properly which is dependent on the angle the AI(which the code is on) is facing.

The hek is going on lol?

avatar image DeadKenny · Aug 09, 2013 at 02:52 PM 0
Share

Let me explain a little better.

When the this AI dude is facing say north all the rays that are meant to be cast are cast and it detects anything that is hit by them, however if I start turning player through code or manually the number of rays start decreasing until all gone as it turns. It only happens towards certain angles/directions.

avatar image robertbu · Aug 09, 2013 at 03:07 PM 0
Share

I don't see a problem that would cause the specific issue you describe, but I do see a couple of things. First in your Quaternion.AngleAxis(), your use of 'transform.u'p could cause problems depending on the relationship of the 'headdummyDone' to this game object. First choice would be 'headdummyBone.up' or maybe 'Vector3.up'.

The second problem is your inner 'foreach(RaycastHit rhit in hits)' loop. You are rechecking the same rays over and over again. If you need to save the RaycastHits for some other use, then this code should be moved outside the 'for (int i = 0; i < numRays; i++)' loop. Or if you don't need to save the hits, then remove it and check the curent hit.

avatar image DeadKenny · Aug 09, 2013 at 03:34 PM 0
Share

When I remove the foreach then the raycast doesn't let the target pass through the rays it ins$$anonymous$$d pivots the whole set of rays on the one ray on the target, which makes the thing effectively only have one ray on the target, which then makes it lose sight very very quick.

avatar image DeadKenny · Aug 09, 2013 at 03:35 PM 0
Share

However I think I will remove the foreach, as it hits the FPS pretty hard I just found out.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Help finding out which side of a cube a raycast hits 1 Answer

Can't find a clone object with RayCast? [Solved] 2 Answers

Problem with Unity Ray Trace AI Detect Player 0 Answers

[C#] UNET Client raycast not the same as Server raycast 2 Answers

AI Field of vision 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