I'm not entirely sure what the problem is, but here is a probably really messy script that I made awhile back that deforms another mesh.
**this goes on the object hitting the deformable object**
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class deformer : MonoBehaviour {
public float speed = 2f;
public Mesh mesh;
public Vector3[] verts;
public float maxDistance;
public GameObject explosion;
void Start() {
mesh = this.GetComponent().mesh;
verts = mesh.vertices;
this.GetComponent().mesh = mesh;
}
void OnCollisionEnter(Collision other) {
Debug.Log("Collided with " + other.gameObject.name);
if (other.gameObject.GetComponent() != null) {
Vector3 colPosition = transform.InverseTransformPoint(other.contacts[0].point);
movePoints(other.gameObject);
}
}
public void movePoints(GameObject other) {
Vector3[] otherVerts = other.GetComponent().verts;
float distance;
for (int i=0; i().UpdateMesh(otherVerts);
}
}
**this goes on the object being deformed**
using UnityEngine;
using System.Collections;
public class deformable : MonoBehaviour {
public Mesh mesh;
public Vector3[] verts;
public Mesh oldMesh;
void Start () {
if (mesh == null) {
oldMesh = mesh = this.GetComponent().mesh;
}
verts = mesh.vertices;
this.GetComponent().mesh = mesh;
}
public void UpdateMesh() {
mesh.vertices = verts;
this.GetComponent().mesh.vertices = mesh.vertices;
}
public void UpdateMesh(Vector3[] points) {
mesh.vertices = points;
this.GetComponent().mesh.vertices = mesh.vertices;
this.GetComponent().sharedMesh = null;
this.GetComponent().sharedMesh = mesh;
}
void OnApplicationQuit() {
this.GetComponent().mesh.vertices = oldMesh.vertices;
this.GetComponent().sharedMesh = null;
this.GetComponent().sharedMesh = oldMesh;
}
}
↧