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 JustinC · Feb 02, 2015 at 07:54 AM · raycastarrayarraysraycasting

Raycast Arrays

Hello all, I am trying to do a collision detection and object avoidance script for a moving around in 3d space. I think I am going to try this with a system of 14 different Raycasts. Problem is, I am trying to figure out a way to store each Racycast within an array, that way I can easily reference each raycast though out the script. I can make the array of raycasts, but I cannot figure out how to set each element of the array to an individual ray, or is that even possible? any help would be appreciated.

Comment
Add comment · Show 8
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 Baste · Feb 02, 2015 at 08:25 AM 0
Share

Ray[] rayArray = new Ray[14] isn't working for you?

avatar image JustinC · Feb 02, 2015 at 03:37 PM 0
Share

That is working, but what is not working is when I try to define each element.

CastArray[0] = RaycastHit f;

I get an error that say "expected ; " after the Raycast. I am not sure if I am even doing this right. Cannot seem to find an example.

avatar image JMurray · Feb 02, 2015 at 04:54 PM 0
Share

I get an error that say "expected ; " after the Raycast.

You don't need to define "f" as a Raycasthit. It should already be defined earlier as a hit.

         RaycastHit[] CastArray = new RaycastHit[14];
         RaycastHit f;
         if (Physics.Raycast (transform.position, -Vector3.up, f)) {
             CastArray[0] = f;
         }
 
avatar image JustinC · Feb 02, 2015 at 05:54 PM 0
Share

so that seems to be working but now it is casting my ray from the objects transform (this is good) to world position 0,0,0 until i hit a collider (this is not what i want), I want it to cast out infront of the object at all times for a distance of 50

 RaycastHit f;
             if (Physics.Raycast (transform.position,transform.forward,out f,50))
             {    castArray [0] = f;
                 if (f.transform != transform && f.collider.tag != "Terrain");
                         fDetected = true;
             }


what can i do to fix this issue?

avatar image Glurth · Feb 02, 2015 at 06:12 PM 0
Share

Your raycast call parameters look ok, perhaps a Debug.Log() command to output their values would help figure it out. I would double check the value of tranform.forward in particular.

Show more comments

1 Reply

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

Answer by JMurray · Feb 03, 2015 at 05:45 PM

  RaycastHit[] castArray = new RaycastHit[14];
  
  RaycastHit[] f = Physics.RaycastAll (transform.position,transform.forward,50);
  
  for (int i = 0; i < f.Length; i++)
  {
      if (f[i].transform != transform && f[i].collider.tag != "Terrain")
      {
          castArray [0] = f[i];
          fDetected = true;
      }
  
  }

Per the comments the following code worked for you.

However, unless their is preceding code that loops through the 14 expected RayCastAll calls your castArray will be:

 castArray[0] = possible hit
 castArray[1] = null
 castArray[2] = null
 castArray[3] = null
 castArray[4] = null
 castArray[5] = null
 castArray[6] = null
 castArray[7] = null
 castArray[8] = null
 castArray[9] = null
 castArray[10] = null
 castArray[11] = null
 castArray[12] = null
 castArray[13] = null

I got an email update on this thread from you (that for some reason I don't see on this question) that said:

Will this cast all 14 rays in the same direction? I want each of the 14 rays to be >casted out in a different direction (forward, -forward, up, -up, right, -right, etc.)

With the code I posted it wont cast in 14 directions at once.

Even if you have preceding code that casts in 14 directions then loops through each RaycastHit[], I would recommend you use SphereCastAll.

SphereCast and SphereCastAll cast a sphere from your originating point. This sphere can travel but doesn't have to.

SphereCast can be used to detect an explosion radius to tell all objects hit by the sphere to take damage (think like a grenade).

SphereCast can also be used to draw a fat ray from the transform to detect for objects in relative position. This is helpful when you have a first person player trying to interact with an object. Using a Ray is a fine line and if the object is small you have to be very accurate and your FPS camera has to be lined up with where the raycast is being drawn from. Instead using a spherecast can make it so the user doesn't have to be exactly on the object but rather the object they are interacting with just has to be in their window since its drawing a fat array.

Mind my crude drawing but there isn't enough detailed information on SphereCasting. alt text

I believe if you are trying to just get all objects that is NOT the transform and is NOT tagged as "Terrain" in ALL directions you should do the following.

 RaycastHit[] castArray = Physics.SphereCastAll (transform.position,50,transform.forward,0);
   
   for (int i = 0; i < castArray.Length; i++)
   {
       if (castArray[i].transform != transform && castArray[i].collider.tag != "Terrain")
       {
           fDetected = true;
       }
   
   }

Also, instead of using a tag. If you set your Terrain to Layer you can exclude all terrain from your SphereCast using its optional Layermask (this is integer based on the integer id of the layer).

If you set Terrain to integer 9 in EDIT -> Project Settings -> Tags and Layers.

Then simply use

 RaycastHit[] castArray = Physics.SphereCastAll (transform.position,50,transform.forward,0,9);
 
 if(castArray != null)
 {
     fDetected = true
 }

You wont get 14 direction array but rather an array of all objects within 50 units of your transform. This could be 1 object, this could be 1000 objects.

I hope this does help you and I didn't go off on a tangent. Good luck!


spherecast.png (20.0 kB)
Comment
Add comment · Show 2 · 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 JustinC · Feb 04, 2015 at 07:02 AM 0
Share

That's awesome thanks for helping me out with this. great explanation, cannot thank you enough!

avatar image osamh · Jun 25, 2015 at 06:15 AM 0
Share

Hi J$$anonymous$$urray, just to add to this. How can i make it so that the object which has the SphereCast script on, simply has the sphere around it? Ins$$anonymous$$d of specifying a distance?

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

22 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

Related Questions

RaycastHit always returns 0 1 Answer

Linear Interpolation of Array Values 1 Answer

Storing vs not storing bool during Raycast ? 1 Answer

How can I differentiate between colliding with an object or its child? 1 Answer

RaycastHit returns object without collider, in wrong layer 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