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
1
Question by shay4545 · Feb 16, 2015 at 11:53 PM · c#instantiateraycastmeshcurve

How to Instantiate a prefab on the surface of a curved structure or mesh

alt text

I'm making a game where a ball is rolling along a curved surface, like a 'u' shape or a halfpipe. I want to instantiate a "Hole" prefab that the ball can fall into on the surface of the structure. How would I make the holes instantiate at a random point on the structure or mesh? I was thinking about using raycasting, but I'm not very good at that. Any ideas?

4vhc0z.png (20.4 kB)
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

Answer by Glurth · Feb 17, 2015 at 02:30 AM

You could select a random vertex from the mesh's vertex array (an array of 3d points)

 int index=Random.Range(0,mesh.vertices.count);
 Vector3 someRandomlySelectedVertexPosition= mesh.vertices[index];

http://docs.unity3d.com/ScriptReference/Mesh.html

These vector3's are in local space,so you may need to transform them to world space, depending on how you child your instantiated object.

 Vector3 instancePos=transform.TransformPoint(someRandomlySelectedVertexPosition);

http://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html

Then instantiate

 Instantiate(pitPrefab, instancePos, mesh.normal[index]); //hmm for rotation, you could probably use the meshs normal for that vertex! try it? edit: I think this would need to be transformed too, at least it's orientation.

 

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Comment
Add comment · Show 3 · 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 shay4545 · Feb 17, 2015 at 08:59 PM 0
Share

I changed the code to the following since mesh.verticies.count was not valid according to the console

 public $$anonymous$$esh mesh;
 float index = Random.Range (0, mesh.vertices.Length);
         Vector3 someRandomlySelectedVertexPosition = mesh.vertices[index];

and now I'm getting the following error on the line with the vector 3:

 error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)

Also, it says that "normal" does not exist under the $$anonymous$$esh class.

avatar image Glurth · Feb 17, 2015 at 09:53 PM 0
Share

good fix regarding my using "count" error.

Take a look at that first link I posted to find the correct name of the normals array (might need to scroll down a bit). Those Doc's will be your best friend when program$$anonymous$$g unity. Or maybe START with Quaternion.Identity(no rotation) for the orientation, til you get the placement working, THEN look at setting the orientation.

Random range return a float type number, to convert it to an int (whole numbers) you need to "cast it" with (int). This will "chop off" any fractions.

 int index= (int)Random.Range(...);

You can only use int type numbers to index into an array, which is the cause of your error CS0266.

avatar image rageingnonsense · Feb 17, 2015 at 10:04 PM 0
Share

For anyone else wondering, the correct variable is mesh.vertexCount to get the count of vertices in the mesh.

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

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

How can I continue to instantiate an object after deleting 0 Answers

AI Instantiate targets/prefab. (pick target issue) 1 Answer

Offseting insantiated object 1 Answer

Best way for a shotgun fire? 1 Answer

Creating a custom mesh - Cleaning the mesh failed 2 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