【Unity动画系统】动画状态基本属性与相关API、IK简单概述

发布于:2024-04-28 ⋅ 阅读:(28) ⋅ 点赞:(0)

 动画状态基本属性与相关API


  

  • Tag:判断是否当前播放着相对应Tag的动画,如果是,那么玩家的输入就是无效的。
    using UnityEngine.InputSystem;
    
    public AnimatorStateInfo stateInfo;
    
    void State(){
        
        //stateInfo = animator.GetCurrentAnimatorStateInfo(animator.GetLayerIndex("op_Rootrint_Stop_Root"));  获取当前正在播放动画的层级
    }
    
    void Update(){
        if(Keyboard.current.wKey.isPressed){
    		stateInfo = animator.GetCurrentAnimatorStateInfo(0);	//获取动画所在的层级
            if(stateInfo.IsTag("Motion")){
                Debug.Log("不允许操作");
                return;
            }
            
            Debug.Log("允许操作");
        }
    }
  • Speed:正值加速,负值倒放(但是脚本中无法激活),要激活的话要下面的Multiplier。

  • Multiplier:使用这个值时要选择一个参数

    float animationScalar = 0f;
    
    void Start(){
        animator.SetFloat("Scalar" , animationScalar);
    }
    
    void Update(){
        if(Keyboard.current.wKey.isPressed){
            animationScalar += 0.1f;
            animator.SetFloat("Scalar" , animationScalar);
        }
    }

  • Animator哈希赋值
    int scalarHash;
    
    void Start(){
    	scalarHash = Animator.StringToHash("Scalar");
        animator.SetFloat(scalarHash , animationScalar);
    }

  • Motion Time:表示播放当前哪一帧。如下按w就会一直播放

    int process;
    
    void Update(){
    	if(Keyboard.current.wKey.isPressed){
         	process += 0.1f;
            animator.SetFloat(processHash , process);
            if(process > 1){
                process = 0f;
            }
        }
    }
     

 

  • Mirror:镜像动画
    animator.SetBool("Mirror" , !animator.GetBool("Mirror"));	//取反则会进行镜像变化

Foot IK到简单叙述IK修正动画


反向运动学(Inverse Kinematics):通过部位的位置反向计算它们的各个父节点的旋转位移和缩放

正向运动学(forward kinematices):由屁股到末梢骨骼节点依次计算其旋转位移和缩放来决定每一块骨骼的最终位置来决定每一块骨骼的这种被称为正向动力学。

红球代表IK Goal,Unity人物角色的脚已经放在红球的位置 , 膝盖前面的则是IK Hit,防止部位出现奇怪的弯曲。

Foot IK参照的是初始的位置,调整IK Goal后不会被参考。

要在脚本内使用IK方法,必须在使用IK的层级里激活IK Pass。

public int weight;

private void OnAnimatorIK(int layerIndex){	//IK回调方法
    animator.SetIKPosition(AvatarIKGoal.RightFoot , new Vector3(0 , 0 , 0));	//设置IKGoal的位置
    animator.SetIKPositionWeight(AvatarIKGoal.RightFoot , weight);	//设置IK Goal的权重,权重越高,对应部位就越跟随IK Goal,范围(0~1)
}

加上射线检测,则可以开发出脚步适应地形的效果

 

Write Defaults


Write Defaults:如果动画文件中没有描述某些属性(动画中可以更改的旋转角、位置、颜色等 )的变化,那么是否需要为这些属性写入默认值。

Animator不处理当前State包含的属性


网站公告

今日签到

点亮在社区的每一天
去签到