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 /
avatar image
1
Question by Toastbyte · Apr 17, 2017 at 09:48 PM · c#collisionverticescoordinatescoordinate-system

Trouble with positions of vertices

So I scripted a mesh, which looks like this:

(Yellow spheres are the vertices)(I named it "Ground")

image

I only need the drawn upside part of the mesh for this question

I also added a mesh collider to it and then created a new gameobject with a rigidbody attached to it.

They collide fine

I want to create a new vertex at exactly the position where they collided. So I used the OnCollisionStay() function:

 void OnCollisionStay(Collision collision)
     {
         ContactPoint contact = collision.contacts[0];
 
         if (contact.otherCollider.name == "Ground")
         {
 
             //Process of collision vertex creation
             newVertices = new Vector3[mesh.vertexCount + 1];
 
             for (int i = 0; i < mesh.vertexCount; i++)
             {
                 newVertices[i] = mesh.vertices[i];
             }
 
             //sets the position of the new vertex
             newVertices[mesh.vertexCount] = contact.point;
 
             //applies changes
             mesh.vertices = newVertices;
 
             mesh.RecalculateNormals();
             mesh.RecalculateBounds();
         } 
     }

But I can't seem to get what I did wrong

Instead of creating the vertex exactly where the collision happened, they spawn in wrong places. (See below)

part2

My guess is, that it has to do with the local and world spaces. I already tried different ways like using the transform.TransformPoint()

and similar to convert the contact point I created to relevant coordinates, but nothing.

What did I do wrong? Can you help me?

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 Bunny83 · Apr 17, 2017 at 09:52 PM

Vertex positions are defined in local space of the object while a hit point is a worldspace position. In order to use the hit point as vertex position you have to transform the worldspace position into localspace. This is done by using "InverseTransformPoint" of the transform of your mesh object.

Note that the hit point doesn't need to be at the exact vertex position of the other mesh. Mesh colliders could collide edge on edge or vertex on surface. We don't know for what purpose you need those points but keep that in mind.

edit
Also note that you can't change a MeshCollider mesh "on the fly". You either have to destroy and reattach a MeshCollider component in order to update the actual collider, or set it's sharedMesh first to null and then back to your mesh. At least that used to work.

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 Toastbyte · Apr 21, 2017 at 02:49 PM 0
Share

InverseTransformPoint was the function I needed. I will also keep in $$anonymous$$d, that I have to update my $$anonymous$$eshColliders. Thank you very much

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2d position of squares in different units than size. 0 Answers

Multiple Cars not working 1 Answer

Locations not registering as equal 1 Answer

Distribute terrain in zones 3 Answers

Manually register event handler for collisions 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