├── BoneCube.fbx ├── BoneSphere.fbx ├── SoftbodyWithSprings.unitypackage ├── README.md ├── BoneSphere.cs ├── BoneCube.cs └── Softbody.cs /BoneCube.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omarvision/Softbody-Physics-Spring-Joint-/HEAD/BoneCube.fbx -------------------------------------------------------------------------------- /BoneSphere.fbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omarvision/Softbody-Physics-Spring-Joint-/HEAD/BoneSphere.fbx -------------------------------------------------------------------------------- /SoftbodyWithSprings.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omarvision/Softbody-Physics-Spring-Joint-/HEAD/SoftbodyWithSprings.unitypackage -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Softbody-Physics-Spring-Joint- 2 | Turn a mesh mushy with softbody physics. How can I make softbody physics in Unity using the things unity has built in? I tried to make softbody using a rigged mesh and spring joints. Here is how it came out. 3 | 4 | You Tube: https://youtu.be/rR3miSZxR3k 5 | -------------------------------------------------------------------------------- /BoneSphere.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class BoneSphere : MonoBehaviour 4 | { 5 | 6 | [Header("Bones")] 7 | public GameObject root = null; 8 | public GameObject x = null; 9 | public GameObject x2 = null; 10 | public GameObject y = null; 11 | public GameObject y2 = null; 12 | public GameObject z = null; 13 | public GameObject z2 = null; 14 | [Header("Spring Joint Settings")] 15 | [Tooltip("Strength of spring")] 16 | public float Spring = 100f; 17 | [Tooltip("Higher the value the faster the spring oscillation stops")] 18 | public float Damper = 0.2f; 19 | [Header("Other Settings")] 20 | public Softbody.ColliderShape Shape = Softbody.ColliderShape.Box; 21 | public float ColliderSize = 0.002f; 22 | public float RigidbodyMass = 1f; 23 | public LineRenderer PrefabLine = null; 24 | public bool ViewLines = true; 25 | 26 | private void Start() 27 | { 28 | Softbody.Init(Shape, ColliderSize, RigidbodyMass, Spring, Damper, RigidbodyConstraints.FreezeRotation, PrefabLine, ViewLines); 29 | 30 | Softbody.AddCollider(ref root, Softbody.ColliderShape.Sphere, 0.005f, 10f); 31 | Softbody.AddCollider(ref x); 32 | Softbody.AddCollider(ref x2); 33 | Softbody.AddCollider(ref y); 34 | Softbody.AddCollider(ref y2); 35 | Softbody.AddCollider(ref z); 36 | Softbody.AddCollider(ref z2); 37 | 38 | Softbody.AddSpring(ref x, ref root); 39 | Softbody.AddSpring(ref x2, ref root); 40 | Softbody.AddSpring(ref y, ref root); 41 | Softbody.AddSpring(ref y2, ref root); 42 | Softbody.AddSpring(ref z, ref root); 43 | Softbody.AddSpring(ref z2, ref root); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /BoneCube.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | //complete list of unity inpector attributes https://docs.unity3d.com/ScriptReference/AddComponentMenu.html?_ga=2.45747431.2107391006.1601167752-1733939537.1520033247 4 | //inspector attributes https://unity3d.college/2017/05/22/unity-attributes/ 5 | //Nvidia Flex video https://youtu.be/TNAKv1dkYyQ 6 | 7 | public class BoneCube : MonoBehaviour 8 | { 9 | /* 10 | E --------- F 11 | | | 12 | | A --------- B 13 | | | | | 14 | | | | | 15 | H --|------ G | 16 | | | 17 | D --------- C 18 | */ 19 | [Header("Bones")] 20 | public GameObject A = null; 21 | public GameObject B = null; 22 | public GameObject C = null; 23 | public GameObject D = null; 24 | public GameObject E = null; 25 | public GameObject F = null; 26 | public GameObject G = null; 27 | public GameObject H = null; 28 | [Header("Spring Joint Settings")] 29 | [Tooltip("Strength of spring")] 30 | public float Spring = 100f; 31 | [Tooltip("Higher the value the faster the spring oscillation stops")] 32 | public float Damper = 0.2f; 33 | [Header("Other Settings")] 34 | public Softbody.ColliderShape Shape = Softbody.ColliderShape.Box; 35 | public float ColliderSize = 0.002f; 36 | public float RigidbodyMass = 1f; 37 | public LineRenderer PrefabLine = null; 38 | public bool ViewLines = true; 39 | 40 | private void Start() 41 | { 42 | Softbody.Init(Shape, ColliderSize, RigidbodyMass, Spring, Damper, RigidbodyConstraints.None, PrefabLine, ViewLines); 43 | 44 | Softbody.AddCollider(ref A); 45 | Softbody.AddCollider(ref B); 46 | Softbody.AddCollider(ref C); 47 | Softbody.AddCollider(ref D); 48 | Softbody.AddCollider(ref E); 49 | Softbody.AddCollider(ref F); 50 | Softbody.AddCollider(ref G); 51 | Softbody.AddCollider(ref H); 52 | 53 | //down 54 | Softbody.AddSpring(ref A, ref D); 55 | Softbody.AddSpring(ref B, ref C); 56 | Softbody.AddSpring(ref F, ref G); 57 | Softbody.AddSpring(ref E, ref H); 58 | 59 | //across 60 | Softbody.AddSpring(ref A, ref G); 61 | Softbody.AddSpring(ref B, ref H); 62 | Softbody.AddSpring(ref F, ref D); 63 | Softbody.AddSpring(ref E, ref C); 64 | 65 | //top 66 | Softbody.AddSpring(ref A, ref B); 67 | Softbody.AddSpring(ref B, ref F); 68 | Softbody.AddSpring(ref F, ref E); 69 | Softbody.AddSpring(ref E, ref A); 70 | 71 | //bottom 72 | Softbody.AddSpring(ref D, ref C); 73 | Softbody.AddSpring(ref C, ref G); 74 | Softbody.AddSpring(ref G, ref H); 75 | Softbody.AddSpring(ref H, ref D); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Softbody.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEditor.PackageManager; 3 | using UnityEngine; 4 | 5 | public static class Softbody 6 | { 7 | #region --- helpers --- 8 | public enum ColliderShape 9 | { 10 | Box, 11 | Sphere, 12 | } 13 | #endregion 14 | 15 | public static ColliderShape Shape; 16 | public static float ColliderSize; 17 | public static float RigidbodyMass; 18 | public static float Spring; 19 | public static float Damper; 20 | public static RigidbodyConstraints Constraints; 21 | public static LineRenderer PrefabLine; 22 | public static bool ViewLines; 23 | 24 | public static void Init(ColliderShape shape, float collidersize, float rigidbodymass, float spring, float damper, RigidbodyConstraints constraints) 25 | { 26 | Shape = shape; 27 | ColliderSize = collidersize; 28 | RigidbodyMass = rigidbodymass; 29 | Spring = spring; 30 | Damper = damper; 31 | Constraints = constraints; 32 | ViewLines = false; 33 | } 34 | public static void Init(ColliderShape shape, float collidersize, float rigidbodymass, float spring, float damper, RigidbodyConstraints constraints, LineRenderer prefabline, bool viewlines) 35 | { 36 | Shape = shape; 37 | ColliderSize = collidersize; 38 | RigidbodyMass = rigidbodymass; 39 | Spring = spring; 40 | Damper = damper; 41 | Constraints = constraints; 42 | PrefabLine = prefabline; 43 | ViewLines = viewlines; 44 | } 45 | public static Rigidbody AddCollider(ref GameObject go) 46 | { 47 | return AddCollider(ref go, Shape, ColliderSize, RigidbodyMass); 48 | } 49 | public static SpringJoint AddSpring(ref GameObject go1, ref GameObject go2) 50 | { 51 | SpringJoint sp = AddSpring(ref go1, ref go2, Spring, Damper); 52 | 53 | if (ViewLines == true) 54 | AddLine(ref go1, ref go2); 55 | 56 | return sp; 57 | } 58 | public static LineRenderer AddLine(ref GameObject go1, ref GameObject go2) 59 | { 60 | return AddLine(ref go1, ref go2, ref PrefabLine); 61 | } 62 | 63 | public static Rigidbody AddCollider(ref GameObject go, ColliderShape shape, float size, float mass) 64 | { 65 | switch (shape) 66 | { 67 | case ColliderShape.Box: 68 | BoxCollider bc = go.AddComponent(); 69 | bc.size = new Vector3(size, size, size); 70 | break; 71 | case ColliderShape.Sphere: 72 | SphereCollider sc = go.AddComponent(); 73 | sc.radius = size; 74 | break; 75 | } 76 | 77 | Rigidbody rb = go.AddComponent(); 78 | rb.mass = mass; 79 | rb.drag = 0f; 80 | rb.angularDrag = 10f; 81 | rb.constraints = Constraints; 82 | return rb; 83 | } 84 | public static SpringJoint AddSpring(ref GameObject go1, ref GameObject go2, float spring, float damper) 85 | { 86 | SpringJoint sp = go1.AddComponent(); 87 | sp.connectedBody = go2.GetComponent(); 88 | sp.spring = spring; 89 | sp.damper = damper; 90 | return sp; 91 | } 92 | public static LineRenderer AddLine(ref GameObject go1, ref GameObject go2, ref LineRenderer prefab) 93 | { 94 | LineRenderer line = Object.Instantiate(prefab); 95 | line.positionCount = 2; 96 | line.SetPosition(0, go1.transform.position); 97 | line.SetPosition(1, go2.transform.position); 98 | return line; 99 | } 100 | } 101 | --------------------------------------------------------------------------------