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 Jan 06, 2014 at 03:24 AM by Invertex for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Invertex · Jan 05, 2014 at 01:56 PM · raycasthit2dlinecastsyntax

LinecastNonAlloc, documented syntax gives error...

I found out about the non memory allocating version of Linecast and want to move to it, but I can't for the life of me figure out how to get the syntax right to let it compile. The documentation simply says to at least have your start/end Vector 2's, as usual, and then RaycastHit2D[] for the third argument. But this does not compile, it will say "Unexpected symbol "[". The doc provides no actual script example, sadly =/

So has anyone used this version of Linecast yet and gotten it to work? Here's an example of the code line I'm trying.

 Physics2D.LinecastNonAlloc(groundCheckL_start.position, groundCheckL_end.position, RaycastHit2D[]); 

I've tried it in a fresh/clean script as well, just to make sure it wasn't an issue caused by something else in my script. And it's definitely not the first 2 arguments, or at least shouldn't be, as they are Vector2 positions which worked fine with the regular Linecast.

Reference: http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.LinecastNonAlloc.html

Comment
Add comment · Show 3
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 Josh707 · Jan 05, 2014 at 02:37 PM 0
Share

I think it requires an array of RaycastHit2D's, so you should try creating an array beforehand and it should pass the values to it

 RaycastHit2D[] hits;
 Physics2D.LinecastNonAlloc(..., ..., out hits);

Though the 3D ray/linecasts use the out keyword to set the variables values, the documentation didn't mention it for this one so I'm not exactly sure how it should work.

avatar image Invertex · Jan 05, 2014 at 03:54 PM 0
Share

Doesn't seem to work that way unfortunately, or at least I'm not understanding how to utilize it. It will accept the hits array and compile, but it doesn't seem to do anything with it when used like that? And yeah, out definitely doesn't work, tells you to remove it.

avatar image Josh707 · Jan 05, 2014 at 04:21 PM 1
Share

Yeah, out is used with the other functions so that it can return a boolean for if it hit anything as well as pass the information to another variable - I'm not sure if it's something I haven't learned about but without it I don't see how it would populate the array.

The documentation says "the results are returned in the supplied array - but the results array will not be resized if it doesn't contain enough elements to report all the results". So it cannot resize the uninitialized array as out would allow, 0 elements will not let it pass any information. I'm not sure how it would populate it but give it a try, if you only need the first intersection then only set it's length to 1.

 RaycastHit2D[] hits = new RaycastHit2D[1];

I would test it but I'm holding off updating Unity for now

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by HappyMoo · Jan 05, 2014 at 05:23 PM

 const int maxReturnedIntersections = 5;
 private RaycastHit2D[] hits = new RaycastHit2D[maxReturnedIntersections];
 
 ...
 ...
 
 int foundIntersections = Physics2D.LinecastNonAlloc(start, end, hits);
 
 if (foundIntersections>maxReturnedIntersections) Debug.LogWarning("Got More Intersections than expected. Consider increasing maxReturnedIntersections");
 
 int intersections = Mathf.min(maxReturnedIntersections, foundIntersections);
 for(int i = 0; i<intersections; i++)
 {
   Debug.Log(hits[i]);
 }
Comment
Add comment · Show 1 · 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 Invertex · Jan 05, 2014 at 11:28 PM 1
Share

Thanks! Got my surface checks converted now thanks to this. $$anonymous$$an, the documentation definitely needs more elaboration on this function haha

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Get the name of the collided object 2 Answers

Problem with raycast vertical 1 Answer

Calculating angle using a raycastHit2D 1 Answer

Physics2D.Linecast ignoring walls 1 Answer

Using methods from other classes on a linecast hit 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