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 Sebane1 · May 11, 2015 at 05:49 AM · c#collisionrigidbodydamagemeshfilter

Mesh Filter Cosmetic Damage

Presently trying to work on a script that allows a ball to take cosmetic mesh damage as it goes through a level. Problem is, Ive been having trouble finding the proper equation for moving the vertexs.

Heres what I have thusfar

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 public class MeshDenter : MonoBehaviour {
     Vector3[] originalMesh;
     public float dentFactor;
     public LayerMask collisionMask;
     private MeshFilter meshFilter;
     void Start() {
         meshFilter = GetComponent<MeshFilter>();
         originalMesh = meshFilter.mesh.vertices;
     }
 
     void OnCollisionEnter(Collision collision) {
         Vector3[] meshCoordinates = originalMesh;
         // Loop through collision points
         foreach (ContactPoint point in collision.contacts) {
             // Index with the closest distance to point.
             int lastIndex = 0;
             // Loop through mesh coordinates
             for (int i = 0; i < meshCoordinates.Length; i++) {
                 // Check to see if there is a closer index
                 if (Vector3.Distance(point.point, meshCoordinates[i])
                     < Vector3.Distance(point.point, meshCoordinates[lastIndex])) {
                     // Set the new index
                     lastIndex = i;
                 }
             }
             // Move the vertex
             meshCoordinates[lastIndex] += /*Insert Rest Of Equation Here*/;
         }
         meshFilter.mesh.vertices = meshCoordinates;
     }
 
     void Reset() {
         meshFilter.mesh.vertices = originalMesh;
     }
 }
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
1
Best Answer

Answer by FortisVenaliter · May 13, 2015 at 09:22 PM

I would suggest two options:

  1. Use a random deformation like:

    meshCoordinates[lastIndex] += new Vector3(Random.Range(-DeformScale,DeformScale), Random.Range(-DeformScale,DeformScale), Random.Range(-DeformScale,DeformScale));

  2. Dent inwards by offsetting the vertex by the inverse normal like:

    meshCoordinates[lastIndex] -= meshCoordinates[lastIndex].normalized * DeformScale;

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 Sebane1 · May 15, 2015 at 05:54 PM 0
Share

That did the trick! I'm using option two.

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

2 People are following this question.

avatar image avatar image

Related Questions

Collision detection from specific directions using rigid body character? 0 Answers

Check if other object has fired a Collision Event 2 Answers

Force a script generated primitive to test for collision 2 Answers

Velocity data from OnCollisionEnter is delayed (incorrect) 3 Answers

Collision triggers do not work on child gameobject 3 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