I have strange situation, when I am trying to deform some mesh.
I have scene with two object - lower smaller cylinder (deformer) and upper bigger cylinder (deformable).
Both objects has C# scripts:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class deformer : MonoBehaviour
{
bool clicked = false;
public float speed = 2f;
public Mesh mesh;
public Vector3[] verts;
public float maxDistance;
public GameObject explosion;
void Start ()
{
mesh = this.GetComponent<MeshFilter> ().mesh;
verts = mesh.vertices;
this.GetComponent<MeshFilter> ().mesh = mesh;
}
void OnCollisionEnter (Collision other)
{
Debug.Log ("Collided with " + other.gameObject.name);
if (other.gameObject.GetComponent<deformable> () != null) {
Vector3 colPosition = transform.InverseTransformPoint (other.contacts [0].point);
movePoints (other.gameObject);
}
}
public void movePoints (GameObject other)
{
print ("entering movePoints");
Vector3[] otherVerts = other.GetComponent<deformable> ().verts;
float distance;
for (int i=0; i<otherVerts.Length; i+=1) {
// print(i);
distance = Vector3.Distance ((GetComponent<Rigidbody> ().position), other.transform.TransformPoint (otherVerts *));*
-
if (distance <= maxDistance) {*
_ print (otherVerts *); *_
otherVerts = new Vector3 (otherVerts .x, otherVerts .y + (1 / distance), otherVerts .z);
* }*
* }*
* other.GetComponent ().UpdateMesh (otherVerts);*
* }*
}
and
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;*
* }*
}
As result, when deformer hits deformable, no changes applied to the deformable object:
Initial scene:
![enter image description here][1]
Hit scene:
![enter image description here][2]
After hit scene - here deformable MUST be changed:
![enter image description here][3]
And strangest in this situation is fact, what changes of the script doesn’;t applied on Game scene, but applied on the Scene of the Unity editor!
![enter image description here][4]
Why changes doesn’t applied on the game scene, but applied on the Editor scene?
*[1]: http://i.stack.imgur.com/bAdrY.jpg*_
[2]: http://i.stack.imgur.com/AsGhr.jpg*_
_[3]: http://i.stack.imgur.com/NfiBA.jpg*_
_*[4]: http://i.stack.imgur.com/ynz5W.jpg*_