Unity5: Use a Humanoid Model created by yourself (move + animation)


2017.07.11: created by
Japanese English
To Table of Contents

Prerequisite knowledge


Use a Humanoid Model created by yourself (move + animation)

  1. Expand the Unity's project file Mecanim.zip which was created in "Unity5: Use a Humanoid Model created by yourself (move)". Rename the folder to "mecanim2/".
  2. Based on the "PlayerMove.unity" scene in the above project, create a "PlayerMoveAnim.unity" scene.
  3. Prepare motion animation to use with Humanoid Character. I will use Standard Assets of Unity here.
  4. Assets -> Import Package -> Characters -> With the "All" selected -> Import
    






    Behavior animaions for the Humanoid Character are imported to the "Assets/Standard Assets/Characters/ThirdPersonCharacter/Animation/" in the project window.




  5. Add Animator Controller component to the AsianBoy in the Hierarchy.
  6. Create C# Script "Assets/Scripts/PlayerMoveAnim.cs" and change the contents as follows.
  7. Right click in the Assets/Scenes -> Create -> C# Script -> rename to "PlayerMoveAnim"




    PlayerMoveAnim.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMoveAnim : MonoBehaviour {
        public float velocity = 1.3f;
        private CharacterController charController;
        private Animator animator;
        void Start () {
            charController = gameObject.GetComponent<CharacterController>();
    	animator = gameObject.GetComponent<Animator>();
        }
        void Update () {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            Vector3 moveDirection =  new Vector3(0, 0, 0);;
            if (charController.isGrounded) {
                moveDirection = new Vector3(h, 0, v);
            }
    	float MovingSpeed = velocity * moveDirection.magnitude;
    	if (MovingSpeed > 0.1f) {
    	    animator.SetFloat("MovingSpeed", MovingSpeed);
    	} else {
    	    animator.SetFloat("MovingSpeed", 0.0f);
    	}
    	transform.LookAt(transform.position + moveDirection);
            moveDirection.y += Physics.gravity.y;
            charController.Move(velocity * Time.deltaTime * moveDirection);
        }
    }
    
  8. Drag "Assets/Scripts/PlayerMoveAnim" in the Project window onto the "AsianBoy" in the Hierarchy window, then the "Player Move Anim (Script)" component is added to the "AsianBoy". Remove the check of the "Player Move (Script)" added before in the Inspector window, because we will not use this now.






  9. Click "Play" button to execute.
  10. "AsianBoy" moves with the keyboard arrow keys (↑, ↓, ←, →) or the 'w', 'a', 's', 'd' keys. You can see that animation of the Humanoid Character switches according to the state. However, switching of behavior seems to be somewhat slow.




  11. In the Animator window, select "State transition from Idle state to Walk state" and "State transition from Walk state to Idle state", and uncheck "Has Exit Time" in the Inspector window. If this is unchecked, it will take as much time as usual to state transition.






  12. Save Scene.
  13. We already created the "PlayerMoveAnim.unity" scene, so overwrite it.

    File -> Save Scenes
    
  14. Here is the Unity's project file Mecanim2.zip I explained here。

Yoshihisa Nitta

http://nw.tsuda.ac.jp/