Using an object transform to trigger a series of blendShapes
Im using an interactive object that has its transforms locked except move it from 0 to 1 in the z axis. I want to use this as a slider to drive a series of blendshapes at different times. for example from 0 to 0.49 I want it to do nothing, but then from 0.5 to 0.6 I want to blendshape[0] to go from 1 to 80 while at the same time moving the 'slider' from 0.55 to 0.7 will drive blendShape[1] from 50 to 100 etc. I guess my biggest issue is translating the values from the Slider to the blendshape ranges needed. Particularly as I want to use this mechanism more than once and range the Slider might move will likely vary. So far I have this.
public GameObject ctrlrTool;
SkinnedMeshRenderer skinnedMeshRenderer;
Mesh skinnedMesh;
public int TouchPoint=1;
void Awake()
{
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
skinnedMesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
}
void Update()
{
float toolPos = (ctrlrTool.transform.position.z);
if (toolPos >= TouchPoint)
{
float bloop = toolPos;// how can I make 'bloop' translate from 0 - 100 regardless of the range?
skinnedMeshRenderer.SetBlendShapeWeight(0, bloop);
print (bloop);
}
if (toolPos < TouchPoint)
{
skinnedMeshRenderer.SetBlendShapeWeight(0, 0);
}
}
}
any help would be appreciated Cheers
Answer by UnityCoach · May 08, 2018 at 10:13 PM
You could use Animation Curves to adjust the mapping in the Editor.
You could also nest this all in a sub-class, like :
[System.Serializable]
public class BlendShapeWeightMapping
{
System.Action<int, float> blendUpdated; // an event to subscribe to, to receive updated value
[SerializeField] int _blendShapeIndex;
[SerializeField] AnimationCurve _mappingCurve;
float _blending;
public float blending
{
get { return _blending; }
set
{
if (value != _blending) // prevents value to update for nothing
{
_blending = value;
if (blendUpdated != null)
blendUpdated.Invoke (_blendShapeIndex, _mappingCurve.Evaluate (_blending));
}
}
}
}
great! Yeah, triggering an animation is definitely a smarter way to go. Thank you! edit: I dont understand all of this code, but can look it up. I just want to ask if this would give me the ability to control the animation, like Im scrubbing thru the timeline (forward, reverse, pausing... depending where Ive set my controller) Thanks again
discovered the Linear Drive scripts and demo in the S$$anonymous$$mVR folders, for anyone interested. This is works perfectly.