Answer the question
In order to leave comments, you need to log in
Why doesn't the script work on base models from the Blender 3D editor?
I started learning Unity and C# quite recently, I decided to disassemble a very interesting script that gives a jelly effect to a 3D object. But ran into a problem. The script works only on basic 3D objects (cube, cylinder), if I create exactly the same object in a 3D editor (Blender), it refuses to work.
No compilation errors were found, but an error occurs when starting the game.
Not allowed to access vertices on mesh 'Pink(Clone)' (isReadable is false; Read/Write must be enabled in import settings)
UnityEngine.Mesh:set_vertices (UnityEngine.Vector3[])
JellyMesh:FixedUpdate () (at Assets/JellyMesh .cs:35)
Pink is the name of the mesh.
I can't figure out how to allow access to model mesh vertices.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JellyMesh : MonoBehaviour
{
public float Intensity = 1f;
public float Mass = 1f;
public float stiffness = 1f;
public float damping = 0.6f;
private Mesh OriginalMesh = null, MeshClone = null;
private MeshRenderer renderer;
private JellyVertex[] jv;
private Vector3[] vertexArray;
void Start()
{
OriginalMesh = GetComponent<MeshFilter>().sharedMesh;
MeshClone = Instantiate(OriginalMesh);
GetComponent<MeshFilter>().sharedMesh = MeshClone;
renderer = GetComponent<MeshRenderer>();
jv = new JellyVertex[MeshClone.vertices.Length];
for (int i = 0; i < MeshClone.vertices.Length; i++)
jv[i] = new JellyVertex(i, transform.TransformPoint(MeshClone.vertices[i]));
}
void FixedUpdate()
{
vertexArray = OriginalMesh.vertices;
for (int i = 0; i < jv.Length; i++)
{
Vector3 target = transform.TransformPoint(vertexArray[jv[i].ID]);
float intensity = (1 - (renderer.bounds.max.y - target.y) / renderer.bounds.size.y) * Intensity;
jv[i].Shake(target, Mass, stiffness, damping);
target = transform.InverseTransformPoint(jv[i].Position);
vertexArray[jv[i].ID] = Vector3.Lerp(vertexArray[jv[i].ID], target, intensity);
}
MeshClone.vertices = vertexArray;
}
public class JellyVertex
{
public int ID;
public Vector3 Position;
public Vector3 velocity, Force;
public JellyVertex(int _id, Vector3 _pos)
{
ID = _id;
Position = _pos;
}
public void Shake(Vector3 target, float m, float s, float d)
{
Force = (target - Position) * s;
velocity = (velocity + Force / m) * d;
Position += velocity;
if ((velocity + Force + Force / m).magnitude < 0.001f)
Position = target;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question