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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I create a spring effect?

Discussion in 'Scripting' started by TRG96, Jan 16, 2015.

  1. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    I have this robot character with an antenna on its head. I have tried using Vector3.Lerp(); and it works but I want it to have a jiggle effect rather than just moving to the position and just stopping.
    Anyone got any suggestions. I have been trying to figure this out for days but had no luck.
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Use an animation and just have it loop, probably better. Not sure what need it to do though.
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    use this instead of lerp

    Code (CSharp):
    1. public static float Spring(float from, float to, float time)
    2. {
    3.     time = Mathf.Clamp01(time);
    4.     time = (Mathf.Sin(time * Mathf.PI * (.2f + 2.5f * time * time * time)) * Mathf.Pow(1f - time, 2.2f) + time) * (1f + (1.2f * (1f - time)));
    5.     return from + (to - from) * time;
    6. }
    Code (CSharp):
    1. public static Vector3 Spring(Vector3 from, Vector3 to, float time)
    2. {
    3.     return new Vector3(Spring(from.x, to.x, time), Spring(from.y, to.y, time), Spring(from.z, to.z, time));
    4. }

    or use an easing/tweening system like itween, leantween, dotween or https://github.com/rakkarage/Ease
     
    Last edited: Jan 16, 2015
  4. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,973
    You could try something like the JiggleBone script free from the wiki, or Dynamic Bone from the asset store. I used to used JiggleBone but switched to Dynamic Bone recently. They both work nicely.

    If you don't have bones in the antenna you could possibly use a "rubber" effect for the verts. There's a script here that does that. It's from 2011, though, so I don't know if it still works as is. Barring that, there are a few "rubber" type solutions out there. Check Google for that.
     
    TRG96 likes this.
  5. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    Thanks Ony, Dynamic Bone is exactly what I needed.
     
    Ony likes this.
  6. VijayVeluri03

    VijayVeluri03

    Joined:
    Mar 18, 2013
    Posts:
    3
    Code (CSharp):
    1. public static float Spring(float from, float to, float time)
    2. {
    3.     time = Mathf.Clamp01(time);
    4.     time = (Mathf.Sin(time * Mathf.PI * (.2f + 2.5f * time * time * time)) * Mathf.Pow(1f - time, 2.2f) + time) * (1f + (1.2f * (1f - time)));
    5.     return from + (to - from) * time;
    6. }
    Code (CSharp):
    1. public static Vector3 Spring(Vector3 from, Vector3 to, float time)
    2. {
    3.     return new Vector3(Spring(from.x, to.x, time), Spring(from.y, to.y, time), Spring(from.z, to.z, time));
    4. }
    Thanks rakkarage. This works Great.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Unity also has a spring joint, but I haven't tried it to know if it produces the effect you want.
     
  8. Shabreen_

    Shabreen_

    Joined:
    Jun 13, 2023
    Posts:
    1
    Unity provide spring joint you can use that.
    Here is ref video
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    @Shabreen_ Don't resurrect threads from 2015 only to say something that was already said in 2017. Please. Thank you.