动画系统

不能录视频所以不能看效果,我也是很郁闷,我会努力用图片和标识来表示清楚。
模型是用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);
    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容