Search Unity

I have a code by idk how connect it to the fbx model from blender

Discussion in 'Prefabs' started by lazizjon21, Apr 30, 2021.

  1. lazizjon21

    lazizjon21

    Joined:
    Apr 29, 2021
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class JellyMesh : MonoBehaviour
    5. {
    6.     public float Intensity = 1f;
    7.     public float Mass = 1f;
    8.     public float stiffness = 1f;
    9.     public float damping = 0.6f;
    10.     private Mesh OriginalMesh = null, MeshClone = null;
    11.     private MeshRenderer renderer;
    12.     private JellyVertex[] jv;
    13.     private Vector3[] vertexArray;
    14.     void Start()
    15.     {
    16.         OriginalMesh = GetComponent<MeshFilter>().sharedMesh;
    17.         MeshClone = Instantiate(OriginalMesh);
    18.         GetComponent<MeshFilter>().sharedMesh = MeshClone;
    19.         renderer = GetComponent<MeshRenderer>();
    20.         jv = new JellyVertex[MeshClone.vertices.Length];
    21.         for (int i = 0; i < MeshClone.vertices.Length; i++)
    22.             jv[i] = new JellyVertex(i, transform.TransformPoint(MeshClone.vertices[i]));
    23.     }
    24.     void FixedUpdate()
    25.     {
    26.         vertexArray = OriginalMesh.vertices;
    27.         for (int i = 0; i < jv.Length; i++)
    28.         {
    29.             Vector3 target = transform.TransformPoint(vertexArray[jv[i].ID]);
    30.             float intensity = (1 - (renderer.bounds.max.y - target.y) / renderer.bounds.size.y) * Intensity;
    31.             jv[i].Shake(target, Mass, stiffness, damping);
    32.             target = transform.InverseTransformPoint(jv[i].Position);
    33.             vertexArray[jv[i].ID] = Vector3.Lerp(vertexArray[jv[i].ID], target, intensity);
    34.         }
    35.         MeshClone.vertices = vertexArray;
    36.     }
    37.     public class JellyVertex
    38.     {
    39.         public int ID;
    40.         public Vector3 Position;
    41.         public Vector3 velocity, Force;
    42.         public JellyVertex(int _id, Vector3 _pos)
    43.         {
    44.             ID = _id;
    45.             Position = _pos;
    46.         }
    47.         public void Shake(Vector3 target, float m, float s, float d)
    48.         {
    49.             Force = (target - Position) * s;
    50.             velocity = (velocity + Force / m) * d;
    51.             Position += velocity;
    52.             if ((velocity + Force + Force / m).magnitude < 0.001f)
    53.                 Position = target;
    54.         }
    55.     }
    56. }
     
unityunity