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 beardkey · Dec 29, 2020 at 11:54 PM · raycastinstantiate prefab

Raycast not finding collider when instantiated from code

tl;dr Using the same methods, I am not able to get raycast to find one of two objects. If I manually drag the 'missing' object into the scene from a prefab, it works no problem.

Setup: I have two objects, a mesh I use for terrain, and a hill. Both created in blender, exported as FBX with the same settings. Both show the mesh normals as expected.

For both objects I created a prefab with the following process:

  • create 3d empty in scene

  • add imported FBX as child object

  • add a mesh collider to FBX child (which auto-assigns the mesh)

  • set layer to custom 'Ground' layer (not yet used anywhere)

  • drag into assets to create prefab

    From there I use the following code to instantiate the prefabs (the hill mesh bounds rise above the bounds of the terrain mesh):

```

 GameObject terrain = Instantiate(terrainGameObject);
 terrain.transform.position = new Vector3(0f, 0f, 0f);
 terrain.transform.parent = terrainParent.transform;
 
 GameObject hill = Instantiate(hillGameObject);
 hill.transform.position = new Vector3(40f, 1f, 12f);
 hill.transform.parent = terrainParent.transform;
 
 // Check for the hill placed
 RaycastHit terrainRayCast;
 if(Physics.Raycast(new Vector3(40f, 10f, 12f), Vector3.down, out terrainRayCast, 11f)){
     Debug.DrawRay(terrainRayCast.point + new Vector3(0f, 11f, 0f), Vector3.down * 11f, Color.red, 0.5f);
     Debug.Log(string.Format("Hit point: {0} at {1} on {2}",
             LayerMask.LayerToName(terrainRayCast.transform.gameObject.layer),
             terrainRayCast.point,
             terrainRayCast.transform.gameObject.name));
 }


...where the game objects indicated are prefabs pulled into a field in the inspector. Both objects show up as expected, with all components and I've verified they're all enabled at execution. The mesh colliders are intact. But, the resulting output shows
Hit point: Default at (40.0, 0.1, 10.0) on terrain_flat_01

To further test, I have manually brought in via the editor the hill prefab 25 units over, adjusted a second raycast and gotten the desired results:

Hit point: Ground at (40.0, 2.5, -15.0) on terrain_hill_03

I manually placed the terrain prefab via editor as well and the raycast works to find the terrain mesh no problem. I've also tested in editor and at runtime, to the same effect. Here is a visualization of the issue, with the code-instantiated hill on the left: alt text

Since it works when manually placed I know it's not the mesh. Since the terrain mesh works both via code and manually, I know it's not my setup process. Since the raycast always hits the terrain no matter what and also hits the manual hill, it's not the raycast code. Also, in runtime the player collides with both meshes without issue, as expected (able to walk on it).

The only difference I see is that the code-instantiated object isn't linked to a prefab, which is expected, but doesn't affect the terrain. I've also tested the order of the prefab instantiation, the method (e.g., using PrefabUtility.Instantiate instead), re-assigning the mesh at execution, but I can't figure it out.


Any idea what could be the issue?

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
2
Best Answer

Answer by beardkey · Dec 30, 2020 at 06:37 AM

After doing more testing, this appeared to be a timing issue. If I make two buttons in the editor inspector, one to do the placement/instantiation with an immediate raycast, and one to do the raycasting separately, the former maintains the issue while the latter works without issue. Adding in a system sleep for half a second doesn't appear to work, which told me it wasn't the order or timing of my code as much as what happens AFTER my code completes, e.g., in a frame update.

So, after some additional digging into what happens after the code returns and before any other code is called, I came upon this Stack Overflow post which detailed how to simulate a physics step to recalculate the collider mesh:

 Physics.autoSimulation = false;
 Physics.Simulate(Time.fixedDeltaTime);  
 Physics.autoSimulation = true;


Calling this prior to the raycast resolved this issue.

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

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

156 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 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

Issues with instantiated prefabs transform using hit.point? 0 Answers

My Raycast on awake fires and will not change Help! 0 Answers

Instantiate, null reference and Raycasting problem. 2D 1 Answer

Noob Raycast question 0 Answers

Confused as to how my RaycastHit script works 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