Unity4.0動畫系統(Mecanim)


簡介

原來以為4.0會有新的UI系統,沒想到Unity選擇了在模型動畫上增加新內容,

就是據說很強大的Mecanim動畫系統了,官方的介紹鏈接:

http://china.unity3d.com/unity/mecanim/

http://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html

 官方教學視頻:

http://v.youku.com/v_show/id_XNDc5OTc4NTEy.html

一、基本概念

原來看那個宣傳視頻時真心覺得不錯,這兩天花了點時間大概了解了下:

現了解一些名詞術語之類的東西吧:

http://docs.unity3d.com/Documentation/Manual/AnimationGlossary.html

A Glossary of Animation and Mecanim terms

圖標 名字 描述 類型 用途/備注
Animation Clip related terms
Animation Clip

Animation data that can be used for animated characters or simple animations. It is a simple "unit" piece of motion, such as (one specific instance of) "Idle", "Walk" or "Run"

(用來保存使模型運動的動畫信息,是motion的一個基本單元,例如空閑狀態,走路狀態,跑步狀態等基本動作)

sub-Asset  
Body Mask

A specification for which body parts to include or exclude for a skeleton

(使身體的某些部分不受骨骼的影響)比如我一個動作只想影響到上半身,那我就屏蔽掉下半身的運動。

Asset (.mask)

Used in Animation Layers and in the importer

(在動畫分層中或模型導入中使用)

  Animation
Curves

Curves can be attached to animation clips and controlled by various parameters from the game

(對動畫綁定動畫曲線,在游戲時可以通過參數來改變動畫)

   
Avatar related terms
Avatar

An interface for retargeting one skeleton to another

(骨骼重定向的一個接口)

sub-Asset  
  Retargeting

Applying animations created for one model to another

(將一個模型的動畫施加到另一個模型上)

Process  
  Rigging

The prcoess of building a skeleton hierarchy of bone joints for your mesh

(組模型的骨骼層次結構的過程)

Process

done in an external tool, such as Max or Maya

(在Max或Maya中已經做好了)

  Skinning

The process of binding bone joints to the character's mesh or 'skin'

(為骨骼蒙皮的過程)

Process

done in an external tool, such as Max or Maya

(在Max或Maya中已經做好了)

  Muscle Definition

A Mecanim concept, which allows you to have a more intuitive control over the character's skeleton. When an Avatar is in place, Mecanim works in muscle space, which is more intuitive than bone space

(Mecanim系統中的一個概念,讓人可以更直觀的控制骨骼系統,當使用Avatar時,Mecanim系統是在muscle空間下工作,比骨骼空間更直觀)

就我理解,這個應該是就是一個或多個骨骼組合成一個 Muscle,可以更方便控制。

   
  T-pose

The pose in which the character has his arms straight out to the sides, forming a "T". The required pose for the character to be in, in order to make an Avatar

(兩手向外平伸張開形成一個T字,為了制作一個Avatar,角色需要滿足這種造型)

   
  Bind-pose

The pose at which the character was modelled

(角色模型的造型)

   
Human template

A pre-defined bone-mapping

(定義好的骨骼映射模板)

Asset (.ht)

Used for matching bones from FBX files to the Avatar.

(用於使導入的FBX文件的骨骼和Avatar匹配)

Animator and Animator Controller related terms
  Animator Component

Component on a model that animates that model using the Mecanim animation system. The component has a reference to an Animator Controller asset that controls the animation.

(Animator組件,指向Asset里定義的Animator Controller)

Component  
  Root Motion

Motion of character's root, whether it's controlled by the animation itself or externally.

(角色的根Motion,被自己的動畫控件或者被外部的動畫控制)

   
Animator Controller (Asset)

The Animator Controller controls animation through Animation Layers with Animation State Machines and Animation Blend Trees, controlled by Animation Parameters. The same Animator Controller can be referenced by multiple models with Animator components.

(通過帶有動畫狀態機的動畫層和動畫混合樹來控制動畫,同時又可以通過動畫參數來控制它。多個含有Animator組件的模型可以引用同一個Animator Controller)

Asset (.controller)  
  Animator Controller (Window)

The window where the Animator Controller Asset is visualized and edited.

(Animator Controller的可視化編輯窗口)

Window  
  Animation Layer

An Animation Layer contains an Animation State Machine that controls animations of a model or part of it. An example of this is if you have a full-body layer for walking / jumping and a higher layer for upper-body motions such as throwing object / shooting. The higher layers take precedence for the body parts they control.

(一個動畫層包括了一個動畫狀態機,這個動畫狀態機控制整個或部分模型的一系列動畫。例如,你有一個全身的動畫層,用於跑步或跳躍動作,和一個更高層次的上半身動作,如扔東西和射擊動作。高層次的就享有所控制身體的那部分的使用權。)

   
  Animation State Machine

A graph controlling the interaction of Animation States. Each state references an Animation Blend Tree or a single Animation Clip.

(動畫狀態的控制圖,每個動畫狀態都是一個動畫混合樹或者是一個基本動畫)

   
  Animation Blend Tree

Used for continuous blending between similar Animation Clips based on float Animation Parameters.

(通過對float型的動畫參數的修改來做到連續的混合類似的動作)比如左轉和右轉。

   
  Animation Parameters

Used to communicate between scripting and the Animator Controller. Some parameters can be set in scripting and used by the controller, while other parameters are based on Custom Curves in Animation Clips and can be sampled using the scripting API.

(用於腳本和Animator Controller之間的通信。一些參數可以在腳本中設置並且被controller使用,同時其它一些參數是基於動畫里的Custom Curves,可以通過腳本API來采樣)

   
  Inverse Kinematics (IK)

The ability to control the character's body parts based on various objects in the world.

(可以通過世界里的物體來控制角色的身體)IK的概念已經挺老的,可以百度“反向動力學”,最通俗的理解就是,你看到前面有個蘋果,你的手臂就能知道應該怎么運動能到蘋果那個位置,拿到蘋果。

   
Non-Mecanim animation terms
  Animation Component

The component needed for non-Mecanim animations

(不使用Mecanim動畫系統的動畫組件)就是4.0以前用的那一套動畫系統

Component  

 

二、使用

基本的操作,網上已經有人寫過一點教程,我就不再花時間去寫了。

http://www.narkii.com/club/bbs_271433.shtml

http://www.narkii.com/club/bbs_271638.shtml

看了上面兩篇,再加上官方文檔,應該就能入門了,至少知道模型應該如何導入,如何制作Avatar出來。

下面我要說一下狀態機的使用方式,其實最好是能把官方的DEMO載下來看,收獲會很多。

 

有了動畫之后,我們就想着要怎么把他們串起來,比如我如何從走變成跑,又如何變成跳。

Mecanim為我們提供了狀態機機制,並提供了可視化編輯窗口。

注意圖中的三個紅框,

①是設置狀態轉換的參數,通過腳本來改變設置的值與②中設置的條件對比來判斷是否轉換狀態。

②是狀態轉換的箭頭,在里邊設置轉換的參考值。

③是一個動作狀態,里邊設置Motion。

 這里可以設置Animation Clip或者是Blend Tree。

選中②的界面:

 

選中上面的紅框,進入下面紅框來設置狀態轉換的條件,如:這里設置的是當Speed參數的值大於0.1的時候進入奔跑模式。

同理可以在回來的箭頭上設置當Speed參數小於0.1里進入idle模式。

 

在代碼里控制Speed值:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
    protected Animator m_Animator;
    GameObject m_PlayerObj;
    
    // Use this for initialization
    void Start () {
        m_Animator = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () 
    {
        if (m_Animator)
        {
            // attack
//            if (Input.GetButtonDown("Fire1"))
//            {
//                m_Animator.SetBool("Attack1", true);
//            }
//            else if (Input.GetButtonUp("Fire1"))
//            {
//                m_Animator.SetBool("Attack1", false);
//            }
            
            
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            
            m_Animator.SetFloat("Speed", h*h+v*v);
            m_Animator.SetFloat("Direction", h, 0.25f, Time.deltaTime);
        }
    }
}

 

 

最后結果:

其實這里邊的跑動,是把官方Demo里的動畫重定向到我自己的模型上了,讓一個女士跑成這樣。。真是丑。。

 

加一個國外的教程:

http://www.gamasutra.com/blogs/HeikkiTormala/20121214/183567/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM