SebastianLawe

Untitled

May 8th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. public class MeshDenter : MonoBehaviour {
  8.     Vector3[] originalMesh;
  9.     public float dentFactor;
  10.     public LayerMask collisionMask;
  11.     private MeshFilter meshFilter;
  12.     void Start() {
  13.         meshFilter = GetComponent<MeshFilter>();
  14.         originalMesh = meshFilter.mesh.vertices;
  15.     }
  16.  
  17.     void OnCollisionEnter(Collision collision) {
  18.         Vector3[] meshCoordinates = originalMesh;
  19.         // Loop through collision points
  20.         foreach (ContactPoint point in collision.contacts) {
  21.             // Index with the closest distance to point.
  22.             int lastIndex = 0;
  23.             // Loop through mesh coordinates
  24.             for (int i = 0; i < meshCoordinates.Length; i++) {
  25.                 // Check to see if there is a closer index
  26.                 if (Vector3.Distance(point.point, meshCoordinates[i])
  27.                     < Vector3.Distance(point.point, meshCoordinates[lastIndex])) {
  28.                     // Set the new index
  29.                     lastIndex = i;
  30.                 }
  31.             }
  32.             // Move the vertex
  33.             meshCoordinates[lastIndex] += /*Insert Rest Of Equation Here*/
  34.         }
  35.         meshFilter.mesh.vertices = meshCoordinates;
  36.     }
  37.  
  38.     void Reset() {
  39.         meshFilter.mesh.vertices = originalMesh;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment