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.
Use an animation and just have it loop, probably better. Not sure what need it to do though.
use this instead of lerp
public static float Spring(float from, float to, float time)
{
time = Mathf.Clamp01(time);
time = (Mathf.Sin(time * Mathf.PI * (.2f + 2.5f * time * time * time)) * Mathf.Pow(1f - time, 2.2f) + time) * (1f + (1.2f * (1f - time)));
return from + (to - from) * time;
}
public static Vector3 Spring(Vector3 from, Vector3 to, float time)
{
return new Vector3(Spring(from.x, to.x, time), Spring(from.y, to.y, time), Spring(from.z, to.z, time));
}
or use an easing/tweening system like itween, leantween, dotween or GitHub - rakkarage/Ease: Simple Unity3D Easing
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.
Thanks Ony, Dynamic Bone is exactly what I needed.
public static float Spring(float from, float to, float time)
{
time = Mathf.Clamp01(time);
time = (Mathf.Sin(time * Mathf.PI * (.2f + 2.5f * time * time * time)) * Mathf.Pow(1f - time, 2.2f) + time) * (1f + (1.2f * (1f - time)));
return from + (to - from) * time;
}
public static Vector3 Spring(Vector3 from, Vector3 to, float time)
{
return new Vector3(Spring(from.x, to.x, time), Spring(from.y, to.y, time), Spring(from.z, to.z, time));
}
Thanks rakkarage. This works Great.
Unity also has a spring joint, but I haven’t tried it to know if it produces the effect you want.
@Shabreen Don’t resurrect threads from 2015 only to say something that was already said in 2017. Please. Thank you.