目前正在尝试制作一个脚本,允许球在经过一个关卡时对其进行美观网格伤害。问题是,我一直无法找到适当的方程来移动顶点。
这就是我所拥有的东西
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;
}
}
答案 0 :(得分:0)
引自: http://answers.unity3d.com/questions/962794/mesh-collision-damage.html#answer-966389
我建议两个选择:
使用随机变形,如:
meshCoordinates[lastIndex] += new Vector3(Random.Range(-DeformScale,DeformScale),
Random.Range(-DeformScale,DeformScale), Random.Range(-DeformScale,DeformScale));
通过将反转法线偏移顶点向内凹陷,如:
meshCoordinates[lastIndex] -= meshCoordinates[lastIndex].normalized * DeformScale;