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
1
Question by UniteCrafty · Sep 10, 2017 at 01:11 PM · raycastarraysrayforeach

Some syntax correction on using an array of Rays inside a foreach for doing Raycasts

I know the title isn't the easiest to understand but that is roughly the gist. You understand how normally when you do the raycast check - if (physics.raycast( - You can use a pre-defined ray instead of typing out the position and direction?

Well I'm trying to do the same thing, but using a foreach loop to fire six different rays at once. At least that's the plan. Imagine checking on each side of a hexagon to see if there are any neighbours.

 public Ray[] rays;  
 
 rays = new Ray[6];
 
 rays[1] = new Ray(gameObject.transform.position, gameObject.transform.forward);
 rays[2] = new Ray(gameObject.transform.position, (gameObject.transform.up * 60));

On a second note, I imagine I've done that 60 degree check incorrectly, but the point is, I'm trying to use an array of rays to simplify the checks and make a potential foreach out of them for the raycasting. Unfortunately it's impossible for me to memorise syntax, plus this is new territory for myself. This is what I've manged thus far.

 foreach (Ray item in rays)
 {
    RaycastHit hit;
 
    if (Physics.Raycast(rays, out hit, 80))
    {
         print(hit.collider.gameObject.name);
         Debug.DrawRay(gameObject.transform.position, gameObject.transform.forward);
    }
 }

Simple enough logic. For every ray I've defined in that rays array, I want to use their values inside that raycast check. Unfortunately rays is incompatible.

I imagine there's a correct way to do this, but I don't know the required syntax. Any insight from a more seasoned coder would be highly appreciated.

All the best.

Comment
Add comment
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

1 Reply

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

Answer by techniman · Sep 13, 2017 at 08:30 AM

It looks like you're iterating over an array, but then passing the entire array to the Raycast function. When using foreach, it will work through the contained code block once each time for each member of the array, giving you access to each individual item with (in this case) the Ray item variable.

foreach (Ray item in rays) { ... if (Physics.Raycast(item, ...)) { ... } }

You can consult the Unity scripting reference if you have further trouble https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 4 · 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 UniteCrafty · Sep 13, 2017 at 12:28 PM 0
Share

Use item. Got it. This seems to fix the problem about as concisely as I imagined. I have been using the API links but there's no example of them being used in arrays, so I was stuck in the mud on the syntax. On a final note, just to be sure it works. I don't think the second ray has the correct calculation on it to cast the ray out at a 60 degree angle from forwards (assu$$anonymous$$g the rotation axis to be y). Any idea what the correct equation for it is? And secondly, could I use item as the parameter for a draw ray, for debugging purposes?

avatar image techniman UniteCrafty · Sep 13, 2017 at 01:17 PM 0
Share

I see; your current "rotation" attempt is simply amplifying the Vector3 by 60, basically pointing 60 units in the up direction. To get a rotation, you'll want to use either transform.Rotate() or maybe Vector3.RotateTowards() depending on what exactly you're looking to achieve. https://docs.unity3d.com/ScriptReference/Transform.Rotate.html https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html Transform.Rotate could be useful, but it affects the transform directly rather than return a rotated transform, so you could make a copy of the GO's transform then rotate it by 60 degrees for each ray.

It looks like DrawRay takes Vector3, Vector3 rather than just a Ray, so you'll have to use: Debug.DrawRay(item.origin, item.direction) to draw the ray. https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

avatar image UniteCrafty techniman · Sep 13, 2017 at 01:45 PM 0
Share

Got the DrawRay set up. Ideally I want 6 rays feeling out from the centre of a hex tile gameobject, so at 0, 60, 120, 180, 240, and 300 degrees in y respectively. Just tried using the rotate in the ray itself,

 HexRays[2] = new Ray(gameObject.transform.position, gameObject.transform.Rotate(0,0,60,Space.Self));

I get this likely isn't the right way, but I'm not sure how to convert the rotate into the Vector3 it asks for. It's a void at present. What should my syntax be?

Show more comments

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

141 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

Related Questions

Raycast under crosshair 0 Answers

2D raycast not working 0 Answers

I need help to make a Ray hit a 2D sprite hexagon map. 1 Answer

Raycast is offsetting object 1 Answer

How can i detect 2 raycasthit2D in the same function storing their Vector2 position? 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