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. Dismiss Notice

Question Dotween squish and stretch 3d cube

Discussion in 'Scripting' started by mrCharli3, Oct 8, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Hi,

    I've spent way too long trying to get a cube to do a simple squish and stretch when it impacts with ground. Could someone please give me an example?

    Code (CSharp):
    1. Sequence sequence = DOTween.Sequence();
    2. sequence.Append(transform.DOMoveY(0, .5f)).SetEase(Ease.InExpo);
    3. // Append squish and stretch
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    I don't know about dotween but if you create a new scene and drop the script below onto a cube then it will give you some jello for later.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Jello : MonoBehaviour
    4. {
    5.     Rigidbody rb;
    6.  
    7.     Vector3 scaleVelocity;
    8.     Vector3 scaleTarget;
    9.     void Start()
    10.     {
    11.         rb=gameObject.AddComponent<Rigidbody>();
    12.         PhysicMaterial material = new PhysicMaterial();
    13.         material.bounciness=1;
    14.         GetComponent<Collider>().material = material;
    15.         GameObject obj=GameObject.CreatePrimitive(PrimitiveType.Cube); // create a floor
    16.         obj.transform.localScale=new Vector3(10,0.2f,10);
    17.         obj.GetComponent<Renderer>().material.color=Color.white;
    18.         transform.position=Vector3.up*4f;
    19.         GetComponent<Renderer>().material.color=Color.red; // has to be red. it's the law..
    20.         scaleTarget=Vector3.one;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         transform.localScale+=scaleVelocity;
    26.         scaleVelocity+=(scaleTarget-transform.localScale)*Time.deltaTime;
    27.         scaleVelocity*=0.9f;
    28.     }
    29.  
    30.     void OnCollisionEnter(Collision collision)
    31.     {
    32.         float h=Mathf.Abs(rb.velocity.y*0.03f);
    33.         scaleVelocity=new Vector3(h,-Mathf.Abs(rb.velocity.y)*0.03f,h);
    34.     }
    35. }
    36.  
     
    Last edited: Oct 9, 2023
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Squash and stretch is just adjusting the scale. Just look at Dotween documentation about changing the scale.
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Yes but its also about the right easing curves, didnt manage to make it feel good yet.
     
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Done, feels quite good now:


    Code (CSharp):
    1. Sequence sequence = DOTween.Sequence();
    2.  
    3. sequence.Append(transform.DOMoveY(0, .5f)).SetEase(Ease.InExpo);
    4.  
    5. sequence.Append(transform.DOScaleY(.6f, .2f));
    6. sequence.Insert(.5f, transform.DOScaleX(1.2f, .2f));
    7. sequence.Insert(.5f, transform.DOScaleZ(1.2f, .2f));
    8.  
    9. sequence.Append(transform.DOScale(1f, .2f));
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    flashframe likes this.