- Home /
How can I lerp animator layer weights in/out?
My player's arms snap into place when the aim key is pressed. How can I use lerp to increase or decrease the upperbody's weight so his upper body doesnt just snap into position but happens more gradually?
void Actions()
{
if (Input.GetKeyDown(KeyCode.F))
{
aim = !aim;
if (aim == true)
{
anim.SetLayerWeight(1, 1);
aimik.solver.SetIKPositionWeight(1);
}
else
{
anim.SetLayerWeight(1,0);
aimik.solver.SetIKPositionWeight(0);
}
}
Answer by TheDevGuy · Nov 30, 2015 at 09:10 AM
I know this thread is a few months old
but, all that needs to be done is to call the 2 functions to so that you can swap between them.
Also, this is applying to layer 1 of the animator. The indexes start from 0 and go up so basically im lerping from the base layer to the next layer.
http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
http://docs.unity3d.com/ScriptReference/Animator.GetLayerWeight.html
http://docs.unity3d.com/ScriptReference/Animator.SetLayerWeight.html
These links will help you better understand what is going on.
using UnityEngine;
using System.Collections;
public class SetLayerWeight : MonoBehaviour {
public Animator animator;
public float smoothTime;
private float yVelocity = 0.0F;
private float currWeight;
public void Update()
{
currWeight = animator.GetLayerWeight(1);
}
public void UnarmedActive ()
{
float startWeight = Mathf.SmoothDamp(currWeight, 1.0f, ref yVelocity, smoothTime);
animator.SetLayerWeight(1, startWeight);
}
public void UnarmedInactive ()
{
float endWeight = Mathf.SmoothDamp(currWeight, 0.0f, ref yVelocity, smoothTime);
animator.SetLayerWeight(1, endWeight);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611233210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SetIKHintPosition and SetIKHintPositionWeight seems to have no impact. 0 Answers
OnAnimatorIK Help 3 Answers
What is Avatar.SetIKHintPosition() ? 0 Answers