动画系统

不能录视频所以不能看效果,我也是很郁闷,我会努力用图片和标识来表示清楚。
模型是用3dmax maya blender等软件进行建模,导出模型后缀为fbx .max .maya .blend
本文框架:
1、动画分类
(1)普通的动画
(2)UGUI的按钮动画(四个状态)
(3)2D游戏的精灵动画
(4)人物角色的 (人形 非人形)
2,普通动画创建
3,什么是Animator
4,UGUI的按钮动画
5,2D游戏的精灵动画
6,人物角色的动画导入
模型的两种模型动画存储方式
三种动画导入导入方式
7,Avatar Mask
8,MatchTarget
9,IK动画

Timeline
过场动画
电影效果

例子:

涉及:
视频截切
状态机实现对动画的控制
用混合树实现相同的效果
2D混合树控制左右转向动画的混合
状态机分层
IK动画
在动画播放中添加代码


image.png

视频截切

image.png

状态机实现对动画的控制

image.png

image.png

image.png

用混合树实现相同的效果

image.png
image.png

2D混合树控制左右转向动画的混合

image.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    private Animator anim;
    private int SpeedXID = Animator.StringToHash("SpeedX");
    private int SpeedZID = Animator.StringToHash("SpeedZ");
    // Use this for initialization
    void Start () {
        anim = this.GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () {
        anim.SetFloat(SpeedZID,Input.GetAxis("Vertical")*4.1f);
        anim.SetFloat(SpeedXID, Input.GetAxis("Horizontal")*126);
    }
}

2D Blending混合类型
2D Simple Directional: 用在没有同一个方向的动画, such as “walk forward”, “walk backward”, “walk left”, and “walk right”。
2D Freeform Directional:用在没有同一个方向的动画且同一个方向可以拥有多个动画,such as “walk forward” and “run forward”.保证一个动画在原点,a single motion at position (0, 0), such as “idle”.其它动画会在原点进行生成和柔和。
2D Freeform Cartesian: 用在没有同一个方向的动画,视野的转换,such as “walk forward no turn”, “run forward no turn”, “walk forward turn right向前走时只有头转右望”, “run forward turn right” etc. X parameter and Y parameter旋转速度和线性速度,such as angular speed and linear speed.

在动画播放中添加代码

image.png

image.png

状态机分层

image.png

image.png

Weight - 层的权重,多个层时可以设置

Mask - 身体遮罩对层的影响,在Create>Avatar Mask中可以创建。这个可以参考Unity状态机例子工程的Animator Controller场景

image

Blending - 混合模式


image.png

IK动画

image.png

image.png
image.png

image.png

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    private Animator anim;

    private int SpeedXID = Animator.StringToHash("SpeedX");
    private int SpeedZID = Animator.StringToHash("SpeedZ");
    private int VaultID=Animator.StringToHash("Vault");
    private int IsHoldWoodID = Animator.StringToHash("IsHoldWood");

     Vector3 matchTarget = Vector3.zero;
    private CharacterController characterController;
    private int ColliderID = Animator.StringToHash("Collider");
    private int SlideID = Animator.StringToHash("Slide");

    public Transform leftHold;
    public Transform RightHold;

    public GameObject Wood=null;

    // Use this for initialization
    void Start () {
        anim = this.GetComponent<Animator>();
        characterController = GetComponent<CharacterController>();
    }
    
    // Update is called once per frame
    void Update () {
        anim.SetFloat(SpeedZID,Input.GetAxis("Vertical")*4.1f);
        anim.SetFloat(SpeedXID, Input.GetAxis("Horizontal")*126);
        characterController.enabled = anim.GetFloat(ColliderID) < 0.5f;
        ProcessVault();
        ProcessSlide();

    }
    private void ProcessVault()
    {
        bool isVault = false;
        if (anim.GetFloat(SpeedZID) > 3 && anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position + Vector3.up * 0.3f, transform.forward, out hit, 4.5f))
            {
                if (hit.collider.tag == "Obstacle")
                {
                    if (hit.distance > 3)
                    {
                        Vector3 point = hit.point;
                        point.y = hit.transform.position.y + hit.collider.bounds.size.y + 0.09f;
                        matchTarget = point;
                        isVault = true;
                    }
                }
            }
        }
        anim.SetBool(VaultID, isVault);
        //anim.MatchTarget(matchTarget, Quaternion.identity, AvatarTarget.LeftHand, new MatchTargetWeightMask(Vector3.one, 0), 0.3f, 0.55f);
        if (anim.GetCurrentAnimatorStateInfo(0).IsName("Vault"))
        {
            anim.MatchTarget(matchTarget, Quaternion.identity, AvatarTarget.LeftHand, new MatchTargetWeightMask(Vector3.one, 0), 0.32f, 0.4f);
        }
       
    }
    private void ProcessSlide()
    {
        bool isSlide = false;
        if (anim.GetFloat(SpeedZID) > 3 && anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position + Vector3.up * 1.5f, transform.forward, out hit, 3f))
            {
                if (hit.collider.tag == "Obstacle")
                {
                    if (hit.distance > 2)
                    {
                        isSlide = true;
                    }
                }
            }
        }
        anim.SetBool(SlideID, isSlide);
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == ("Wood"))
        {
            Destroy(other.gameObject);
        }
        CarryWood();
    }
    private void CarryWood()
    {
        Wood.SetActive(true);
        anim.SetBool(IsHoldWoodID, true);
    }
    private void OnAnimatorIK(int layerIndex)
    {
        if (layerIndex ==1)
        {
            int weight = anim.GetBool(IsHoldWoodID) ? 1 : 0;
            anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHold.position);
            anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHold.localRotation);
            anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, weight);
            anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, weight);
            anim.SetIKPosition(AvatarIKGoal.RightHand, RightHold.position);
            anim.SetIKRotation(AvatarIKGoal.RightHand, RightHold.localRotation);
            anim.SetIKPositionWeight(AvatarIKGoal.RightHand, weight);
            anim.SetIKPositionWeight(AvatarIKGoal.RightHand, weight);
        }
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFallow : MonoBehaviour {
    private Transform player;
    private Vector3 offset;

    // Use this for initialization
    void Start () {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        offset = transform.position - player.position;
    }
    
    // Update is called once per frame
    void Update () {
        Vector3 targetPostion = player.position + player.TransformDirection(offset);
        transform.position = Vector3.Lerp(transform.position, targetPostion, Time.deltaTime * 3);
        transform.LookAt(player.position);
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wood : MonoBehaviour {
    public float notateSpeed = 10;
    // Use this for initialization
    void Start () {
    }
    
    // Update is called once per frame
    void Update () {
        transform.Rotate(Vector3.up * notateSpeed * Time.deltaTime, Space.World);
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容