Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is there any working softbody assets/tutorials

Discussion in 'Physics' started by Vefery, Aug 9, 2019.

  1. Vefery

    Vefery

    Joined:
    Feb 23, 2018
    Posts:
    119
    I need softbody for my game, but surprisely I can't find ANYTHING working with new versions. I've tried to use cloth simulation, but it's broken and keep collapsing any mesh except default shapes, I've also tried all free assets (and even tried pirated!) and none of those work.
    Is there anything about softbody?
    P.S I'm not able to pay 50$+ for assets, because it's pretty expensive in my country and my game is not commertial
     
  2. CheapMeow

    CheapMeow

    Joined:
    Mar 30, 2021
    Posts:
    4
    Beginner tutorial is:

    And the second tutorial shows how to use more kinds of joints:


    To copy easily, it is the script in the first video:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.U2D;
    5.  
    6. public class SoftBody2D : MonoBehaviour
    7. {
    8.     #region Constants
    9.     private const float splineOffset = 0.5f;
    10.     #endregion
    11.    
    12.     #region
    13.     [SerializeField]
    14.     public SpriteShapeController spriteShape;
    15.     [SerializeField]
    16.     public Transform[] points;
    17.     #endregion
    18.  
    19.     #region MonoBehaviour Callback
    20.     private void Awake()
    21.     {
    22.         UpdateVerticies();
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         UpdateVerticies();
    28.     }
    29.     #endregion
    30.  
    31.     #region privateMethods
    32.     private void UpdateVerticies()
    33.     {
    34.         for(int i = 0;i < points.Length;i++)
    35.         {
    36.             Vector2 _vertex = points[i].localPosition;
    37.  
    38.             Vector2 _towardsCenter = (Vector2.zero - _vertex).normalized;
    39.  
    40.             float _colliderRadius = points[i].gameObject.GetComponent<CircleCollider2D>().radius;
    41.  
    42.             try
    43.             {
    44.                 spriteShape.spline.SetPosition(i, (_vertex - _towardsCenter * _colliderRadius));
    45.             }
    46.             catch
    47.             {
    48.                 Debug.Log("Spline points are too close, recalcuate...");
    49.                 spriteShape.spline.SetPosition(i, (_vertex - _towardsCenter * (_colliderRadius + splineOffset)));
    50.             }
    51.  
    52.             Vector2 _lt = spriteShape.spline.GetLeftTangent(i);
    53.  
    54.             Vector2 _newRt = - (Vector2.Perpendicular(_towardsCenter) * _lt.magnitude);
    55.             Vector2 _newLt = - (_newRt);
    56.  
    57.             spriteShape.spline.SetLeftTangent(i,_newLt);
    58.             spriteShape.spline.SetRightTangent(i,_newRt);
    59.         }
    60.     }
    61.     #endregion
    62. }
    63.