Unity animation fusion IK MatchTarget director

I don't have a deep understanding, record memos

animation fusion

Stealth Secret Operation 14-17

New state machine

Motion on the panel

//Motion movement
//Locomotion, movement, own name
//Loco, locomotive
//Blend, mixing
insert image description here

The difference from normal animation on the panel

//The Motion of ordinary animation is an animation Clip
insert image description here

new behavior tree

//Double click to enter
insert image description here

add animation

//Click the node
//Click the first small circle on the right to add animation
//If the animation items are not enough, click the "+" below
insert image description here

(1D example) The size of Speed ​​is controlled by walking to running

//You can drag other models in the lower right corner
insert image description here

Effect

//It is not easy to fax on a gif card with a size of 3M
insert image description here

You can drag other models to the window in the lower right corner

insert image description here

(2D example)

Unity TimeLine application tutorial cool opening animation 23-31
//cartesian, Cartesian
//1. Choose a 2D (try any 2D, the main effect), select parameters (Horizontal means horizontal/left, Vertical means vertical/front and back) //2. Choose randomly (maybe angular velocity control left and right can be referenced, others don’t know which elements to refer to), as long as the effect of
3
insert image description here

cut/adjust animation

Generally, all the animations of a model are placed in a general clip, and the clip in what state is needed, whether it has been cut or cut by yourself.






insert image description here

angular velocity

//Angular velocity has a greater impact on steering
//Turning depends on turning around instead of "crabs" walking sideways
insert image description here

Effect

insert image description here

(Problem) Animator opens empty

reboot

I

Anti wood

Unity TimeLine application tutorial cool opening animation 43-47

FixAnimatorIKPositionAndRotation (bone part, specific position, weight) [custom function]

    [Tooltip("肩膀上的木头,一开始不激活")] public GameObject wood;
    [Tooltip("模型中左手腕处")] public Transform leftHand;
    [Tooltip("模型中右手腕处")] public Transform rightHand;
    //
    private void OnTriggerEnter(Collider other)//接触木头
    {
    
    
        if (other.tag == "Log")
        {
    
    
            Destroy(other.gameObject);
            print("碰撞");
            CarryWood();
        }
    }
    private void CarryWood()//扛木头
    {
    
    
        wood.SetActive(true);
        animator.SetBool(willHoldLogID,true);
    }
    private void OnAnimatorIK(int layerIndex)//每帧调用,需要层启用IK
    {
    
    
        if (layerIndex == 1)//对应HoldLog层
        {
    
    
            int weight = animator.GetBool(willHoldLogID) == true ? 1:0;//willHoldLogID的值

            FixAnimatorIKPositionAndRotation(AvatarIKGoal.LeftHand,leftHand, weight);
            FixAnimatorIKPositionAndRotation(AvatarIKGoal.RightHand,rightHand, weight);
        }
    }
    private void FixAnimatorIKPositionAndRotation(AvatarIKGoal target, Transform match, int weight)//使用match的位置角度来调整target
    {
    
    
        animator.SetIKPosition(target, match.position);//设置位置
        animator.SetIKRotation(target, match.rotation);
        animator.SetIKPositionWeight(target, weight);//设置优先级
        animator.SetIKRotationWeight(target, weight);
    }

The first parameter bone part

AvatarIKGoal.LeftHand
AvatarIKGoal.RightHand
means that the state machine of this layer only uses the left and right hands. Other parts are not under its control
insert image description here

The second parameter, position LeftHand/RightHand

//The specific operation is to create two empty nodes under the wood to store the Transform of the left and right hands when "resisting wood"
//Rotate the hand to make it fit the wood to achieve the effect
//Assign the Transform of the left and right hands to the two empty nodes under the wood. Left and right hand reset
insert image description here

The third parameter IK recognizes that the position of the bone is still the position of the target (set by yourself)

//The principle is not clear, anyway, if they are not all set to 1 (identify the position of the target), there will be a position error
insert image description here

Method parameter comparison

Animator.SetIKPositionWeight
insert image description here

Target Match MatchTarget

over the wall

//The green ball is generated by the following code

        animator.MatchTarget(matchTarget,
            Quaternion.identity,
            avatarTarget,
            new MatchTargetWeightMask(matchTargetWeightMask, 0),
            startTime,
            endTime
        );

insert image description here

    private void Vault()//腾跃,尝试过和Slide重构复用,太麻烦
    {
    
    
        //1、播放动画
        //速度>3 && 是 LocalMotion行为树

            //射线检测
            float testDistance = 4f;//检测距离
            float vaultDistance = 3f;//起跳距离
            bool willVault = false;//是否要进行翻墙

            //rayPos,direction,射线,长度
            bool isHit = Physics.Raycast(transform.position+Vector3.up*0.3f, transform.forward, out RaycastHit hit, testDistance);
            if (isHit && hit.collider.tag == "Obstacle")//障碍物是墙
            {
    
    
                if (hit.distance > vaultDistance)//
                {
    
    
                    willVault = true;
                    //
                    Vector3 point = hit.point;
                    point.y = hit.collider.transform.position.y + hit.collider.bounds.size.y;//位置加自身高度
                    matchTarget = point;//得到着手点
                    matchTarget.y +=matchTargetOffsetY;
                }
            }          
            animator.SetBool(willVaultID, willVault);
        

        //2、MatchTarget
        //有一个状态叫ClimbOver && 在翻墙中, 不在状态切换过程中(差值)
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ClimbOver") && animator.IsInTransition(0) == false)
        {
    
    
            //1、MatchTarget
            float startTime = 0.3f;
            float endTime = 0.4f;
            MatchTarget(matchTarget, AvatarTarget.LeftHand, Vector3.one, startTime, endTime);
        }
    }
        void MatchTarget(Vector3 matchTarget,AvatarTarget avatarTarget, Vector3 matchTargetWeightMask, float startTime, float endTime)
    {
    
    
        //动画在(startTime,endTime)时
        animator.MatchTarget(matchTarget,
            Quaternion.identity,
            avatarTarget,
            new MatchTargetWeightMask(matchTargetWeightMask, 0),
            startTime,
            endTime
        );
    }

slippery

//I mainly adjust endTime
//Video matchTarget = hit.point+transform.forward * 2 (The bigger the farther, the video is set to 10 when watching the effect, and the slide is far away) //
matchTarget decides how far the whole model will move forward during the period of (startTime, endTime)
insert image description here

    void Slide()//滑地
    {
    
    
        //1、播放动画
        //速度>3 && 是 LocalMotion行为树
        if (animator.GetFloat(verticalID) > runSpeed && animator.GetCurrentAnimatorStateInfo(0).IsName("LocalMotion"))
        {
    
    
            //射线检测
            float testDistance = 4f;//检测距离
            float slideDistance = 2f;//滑地距离
            bool willSlide = false;//是否要进行翻墙


            //start,direction,射线,长度
            bool isHit = Physics.Raycast(transform.position + Vector3.up * 1.5f, transform.forward, out RaycastHit hit, testDistance);

            if (isHit && hit.collider.tag == "Obstacle")//障碍物是墙
            {
    
    
                if (hit.distance > slideDistance)//起跳距离
                {
    
    
                    willSlide = true;

                    //设置match
                    Vector3 point = hit.point;
                    point.y = 0;//滑行不影响高度
                    point = point + transform.forward*10;
                    matchTarget = point;//滑行个2米
                }                  
            }
            animator.SetBool(willSlideID, willSlide);
        }
          
        //
        if (animator.IsInTransition(0) == false && animator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
        {
    
    

            //1、MatchTarget
            float startTime = 0.38f;//0.17
            float endTime = 0.67f;//0.67f;//30
            //float startTime = 0.17f;//0.17
            //float endTime = 0.30f;//0.67f;//30
            MatchTarget(matchTarget, AvatarTarget.Root, new Vector3(1, 0, 1), startTime, endTime );
        }
    }
    void MatchTarget(Vector3 matchTarget,AvatarTarget avatarTarget, Vector3 matchTargetWeightMask, float startTime, float endTime)
    {
    
    
        //动画在(startTime,endTime)时
        animator.MatchTarget(matchTarget,
            Quaternion.identity,
            avatarTarget,
            new MatchTargetWeightMask(matchTargetWeightMask, 0),
            startTime,
            endTime
        );
    }

director

public PlayableDirector director;//过场动画的相机,播放时间轴
    private void OnTriggerEnter(Collider other)//接触木头
    {
    
    
        if (other.tag == "Playable")
        {
    
    
            director.Play();//播放Timeline
        }
    }

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_39538253/article/details/118282229