Hi, I’m pretty new to Unity scripting and would really appreciate any suggestions. Right now I have a slider that controls a model’s blendshape (bird’s wings opening), but I would prefer to have a button animate it (i.e., the first click makes the blendshape value 100, wings open, the second click makes it 0, wings closed). I’m having difficulty figuring out where to start to make it transition from 0 → 100 and 100 → 0 over an arbitrary number of seconds. Appreciate any pointers / nudging in the right direction,
You could just create an animation that manipulates that blend amount back and forth over time.
If you need control of it, then connect the playing of that animation to a button.
ALTERNATELY, if you want to code it all yourself, this is the pattern I always use:
Smoothing movement between any two particular values:
https://discussions.unity.com/t/812925/5
You have currentQuantity and desiredQuantity.
- only set desiredQuantity
- the code always moves currentQuantity towards desiredQuantity
- read currentQuantity for the smoothed value
Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.
The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
Another useful method for back-and-forth function generating is to feed a linearly-increasing floating point value into this method:
https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html
and use the output for your blend.
Very belated, but that’s was helpful… thanks!
I’m going the route of having an animation that controls the blendshape. A follow-up question: how might I go about having the button either open or close the wings, depending on what its current state is (i.e. open → close, close → open)? I’m a bit at a loss of how to achieve this. I wonder if there’s a way to do this purely in Unity (such as with the Animator) without recourse to handwritten C# script (but I will be happy to write a C# script… though also a little at a loss of what to do with that too).
I have the following set-up (attached) with a Toggle that is supposed to change open / close values OnClick, but it just constantly opens / closes the wings. I’ve been searching, but if anyone could point me in the right direction (e.g. Unity documentation, tutorials), it’d be much appreciated.
