Search Unity

Question Smooth change transform depending on the distance

Discussion in 'Animation' started by Zimaell, May 31, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    This applies to changing transform of the arms, legs, body.
    How to smoothly change positions and rotations based on their difference?
    That is, you need to smoothly change the position, but if the difference in positions or rotations is very large, then change it faster, if it is small, then slower...

    for example - I do not use the blend tree, for certain reasons, so I need to script a smooth change in actions, for example, the character was walking and stopped abruptly...
    (without CrossFade, somehow managing the transformations)
    i do this but it doesn't work

    Code (CSharp):
    1. private void OnAnimatorIK(int layerIndex){
    2.    if(layerIndex == 0){
    3.       LeftFootIKPosition = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
    4.       RightFootIKPosition = animator.GetIKPosition(AvatarIKGoal.RightFoot);
    5.  
    6.       LeftFoot = animator.GetBoneTransform(HumanBodyBones.LeftFoot);
    7.       RightFoot = animator.GetBoneTransform(HumanBodyBones.RightFoot);
    8.  
    9.       LeftFootIKPositionReq = GetIKPositionReq(LeftFootIKPosition, LeftFoot.position);
    10.       RightFootIKPositionReq = GetIKPositionReq(RightFootIKPosition, RightFoot.position);
    11.  
    12.       animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f);
    13.       animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f);
    14.       animator.SetIKPosition(AvatarIKGoal.LeftFoot, LeftFootIKPositionReq);
    15.       animator.SetIKPosition(AvatarIKGoal.RightFoot, RightFootIKPositionReq);
    16.       }
    17.    }
    18. public float speed = 1f;
    19. private Vector3 GetIKPositionReq(Vector3 IKpos, Vector3 pos){
    20.    Vector3 refVelocity = Vector3.zero;
    21.    IKpos = Vector3.SmoothDamp(IKpos, pos, ref refVelocity, speed * Time.fixedDeltaTime);
    22.    return IKpos;
    23.    }
     
    Last edited: May 31, 2023