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 JasonBricco · Aug 07, 2014 at 07:05 PM · c#raycastmeshcollidervoxel

Knowing When a MeshCollider is Done Updating

Hi everyone.

I'm having a little issue that I can't quite figure out how to resolve. I have objects that procedurally generate mesh colliders. These are chunks in a voxel engine. So pieces of the ground basically generate mesh colliders.

What I'm trying to do is write some code that allows the player to be spawned right on top of the ground upon loading the world, rather than falling from the sky from above.

The problem is I don't know what kind of ground will be there. It could be a mountain or a valley or something the player created. So what I have to do is I have to cast a ray at the ground and figure out where the ray hits, and put the player a little bit above where the ray hit.

The problem with this is my chunks generate in a separate thread. So if I immediately cast a ray, the mesh won't be generated quite yet and the ray won't hit anything. That's not helpful.

I also don't like the idea of delaying it by a fixed amount of time before trying the ray. That doesn't feel like a good solution and it may not work on some devices.

So I figured I'd set up an event from my chunks that get triggered when the chunk is done loading. So I basically use meshCollider.sharedMesh = [mesh I calculated in another thread]. I do this on the main thread, as Unity enforces that. And after that's all done, I trigger my event.

Then using that event I can say: okay, the chunk is generated, now I can cast my ray and position the player properly.

But it seems to not be working that way. I cast my ray after my event triggers, and it hits nothing! I tested delaying the raycast by a second, and it hit something just fine.

So what's going on? My guess is that the process of updating the mesh that happens when I use meshCollider.sharedMesh = [mesh I calculated] actually does work on other threads. And therefore it's still not actually done by the time I trigger my event.

Which leads to my question. Is there any way to know when the mesh has been fully calculated and is done, so I know when I can cast my ray?

Thank you for your help!

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 Eric5h5 · Aug 08, 2014 at 01:47 AM

No functions are asynchronous unless specifically mentioned in the docs, and nothing like this is ever done in other threads since that would leave the engine in an indeterminate state. When you assign the mesh nothing else can happen until it's done computing. I expect that it would be registered with the physics engine at the start of the next physics frame.

Comment
Add comment · Show 5 · 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 JasonBricco · Aug 08, 2014 at 03:12 AM 0
Share

So... essentially, all I have to do is wait 1 frame after my event fires and it should work out?

avatar image JasonBricco · Aug 08, 2014 at 04:17 AM 0
Share

I tried a yield return null... nothing. I tried a yield WaitForFixedUpdate... nothing.

Yet, trying a WaitForSeconds(1.0f) works fine. Why is this happening? $$anonymous$$y event is definitely fired after the meshCollider.shared$$anonymous$$esh is set.

avatar image Kiwasi · Aug 08, 2014 at 04:25 AM 0
Share

Try yielding till after the next FixedUpdate frame, as indicated in the answer.

avatar image JasonBricco · Aug 08, 2014 at 04:39 AM 0
Share

Isn't that exactly what 'yield return new WaitForFixedUpdate()" does? And that is not helping me. Even if I wait for another one after that, it still does nothing.

avatar image JasonBricco · Aug 08, 2014 at 06:44 AM 0
Share

Alright, I realized the problem was something else entirely. Your answer helped with the part relevant to this question.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

raycasting without colliders 0 Answers

Physics.Raycast to Physics2D.Raycast? 1 Answer

C# raycast shooting script broken 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