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 /
This question was closed Aug 16, 2013 at 09:03 PM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by DeadKenny · Aug 12, 2013 at 02:08 PM · c#raycastairayforeach

For each Raycast?

I has this code here and it works but only on the one ray.

What it does is cast 3 rays out one goes left, one goes right and one right in front. Like a field of view, but only with 3 rays. I intend it to be like that now, its not supposed to be a sweep.

So yeah how to get the hits from all three rays to work with the if? I can only get the left one to work.

 Vector3 forwd = headdummyBone.TransformDirection(Vector3.forward);
 Vector3 left = headdummyBone.TransformDirection(new Vector3(-30, -2. 10));
 Vector3 right = headdummyBone.TransformDirection(new Vector3(30, -2. 10));
 
 
 RaycastHit hit;
 
 if(Physics.Raycast(headdummyBone.position, fwd, out hit, maxSeeDistance, ignoringLayers)){
    
 
 if(Physics.Raycast(headdummyBone.position, left, out hit, maxSeeDistance, ignoringLayers)) || Physics.Raycast(headdummyBone.position, right, out hit, maxSeeDistance, ignoringLayers)){
 
 if(hit.transform.tag == "Player"){
    
    canFollow = true;
 
 
 }}}}
Comment
Add comment · Show 17
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 12, 2013 at 02:20 PM 0
Share

Scroll right to see the 3rd ray for right side.

avatar image nixcs2512 · Aug 12, 2013 at 02:31 PM 0
Share

Actually 3 raycast worked. You just need an array to store information for each raycast (if it works) like:

 RaycastHit[] hit = RaycastHit[3];
 if(Physics.Raycast(...,fwd,out hit[0],...)
 {
  if((Physics.Raycast(...,left,out hit[1],...)||(Physics.Raycast(...,right,out hit[2],...))
 }
 ...

and get the info from the array hit.

avatar image Joyrider · Aug 12, 2013 at 02:43 PM 0
Share

@nixcs2512, convert your comment to an answer ;)

avatar image DeadKenny · Aug 12, 2013 at 02:58 PM 1
Share

It doesn't work like that though.

avatar image clunk47 · Aug 12, 2013 at 03:22 PM 2
Share

If you want to check every direction, don't put one statement inside of another.

 if(//Raycast in 1st direction)
 {
     //DoSomething
 }
 
 if(//Raycast in 2nd direction)
 {
     //DoSomething
 }
 
 if(//Raycast in 3rd direction)
 {
     //DoSomething
 }
avatar image clunk47 clunk47 · Aug 12, 2013 at 03:56 PM 3
Share

Code would only execute within each if() statement if the statement is true. AFAI$$anonymous$$, this will not effect cpu, unless you have an old Pentium II or something lol. If this works, please vote up (thumbs up) and accept (check mark) the answer, happy developing :D

avatar image clunk47 clunk47 · Aug 12, 2013 at 03:58 PM 3
Share

P.S. Having your left and right statements inside the forward statement means you'd have to be hitting something in the fwd direction in order to use the left or right directions, meaning you would only be able to hit left or right casts ONLY if something was hit in fwd direction.

avatar image clunk47 clunk47 · Aug 12, 2013 at 04:11 PM 2
Share

If this is used within Update(), this is running every frame, just to clarify, if you are running 60 frames per seconds, you're checking for hits 60 times per second with one RaycastHit ins$$anonymous$$d of 3. Checking for one thing is more efficient than checking for 3, especially 60 times per second, even 30 times per second. And you are correct, the difference won't be noticed, at least on most computers these days.

Show more comments
Show more comments

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by verenion · Aug 12, 2013 at 02:57 PM

This code is saying:

If the FRONT ray hits something AND the left OR right hits something, do this... etc.

That's not right is it? canFollow = true; will only run when something intersects the front AND one of the two sides.

You also are using the same raycast hit, you need to use an array of RaycastHits, as suggested by someone else, or simply define three raycasthits for each direction..

Try something like this (NOTE, this is untested, this is simply to get you on the lines of what you need to do.)

 RaycastHit hitLeft;
 RaycastHit hitFwd;
 RaycastHit hitRight;

 // you are seperating each hit by an OR - so IF (front hits) OR (left) OR (right)
 // also note, each raycast, needs a different hit to use.
 if(Physics.Raycast(headdummyBone.position, -headdummyBone.right, out hitLeft, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.right, out hitRight, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.foward, out hitFwd, maxSeeDistance, ignoringLayers)) {
      
     // one of the rays hit something. Check each hit for tag too.
     
     if(hitFwd.transform.tag == "Player" || hitRight.transform.tag == "Player" || hitLeft.transform.tag == "Player"){
     
        // is the object it hit a player?
        canFollow = true;
      
      
     }
     
     }
Comment
Add comment · Show 9 · 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 12, 2013 at 03:06 PM 0
Share

Err... it doesn't work. Only seems to detect the first Physics.Raycast.

In this case left.

The rays cast fine on the debug though. Can't be vectors... could it?

avatar image Joyrider · Aug 12, 2013 at 03:18 PM 0
Share

try and seperate your 3 raycasts, and display the bool of each raycast with a debut, and (if it returns true) the hit collider.

But to me, your 3 raycasts should be working.

avatar image verenion · Aug 12, 2013 at 03:38 PM 0
Share

sorry, I've overlooked something, Editing answer now.

avatar image verenion · Aug 12, 2013 at 03:51 PM 1
Share

Also, rather than using the vectors, you can just use transform.forward and transform.right this does exactly the same as your vectors, with less work.

avatar image DeadKenny · Aug 12, 2013 at 04:01 PM 1
Share

Alright cool. Thanks dudes.

Thanks. $$anonymous$$y AI is working! I use a collider also to do the fine detail checking ins$$anonymous$$d of a million rays(which causes LAG).

So thanks again.

Show more comments

Follow this Question

Answers Answers and Comments

19 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

Related Questions

AI Pathfinding using RayCast 0 Answers

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

Why in Unity3D RaycastHit. textureCoord always return 0,0 after building the project? 1 Answer

Rays not hitting (most of the time) 0 Answers

[C#] Raycasts and Object Tags 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