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


2017.07.11: created by
[Up] Japanese English

Prerequisite knowledge


Import the Humanoid Model created by MakeHuman into Unity and Move it with Key Operation

I will describe an easy way to import and move your Humanoid model on Unity.
  1. Start a new project with Unity.
  2. Start a new project named "Mecanim" of "3D" type.




  3. Import your Humanoid model (fbx) into Unity.
  4. Create a Plane as a ground in the Hierarchy. Choose Reset from Settings of Transform in the Inspector window to set Position (x,y,z)=(0,0,0).
  5. GameObject -> 3D Object -> Plane
    



  6. Insert "fAssets/Models/AsianBoy" into the Herarchy window.
  7. Change the "Center" and "Height" values of "CharacterController" component added in the Inspector window.
  8. While viewing the Scene window, if you select "AsianBoy" in the Hierarchy, the CharacterController's Capsel Collider will be shown with a green solid line. Change "Center" and "Height" values to match humanoid character. In this example, Center(x,y,z) = (0, 0, 87.0), Height = 1.7.




  9. Create a script to move the Humanoid Character.
  10. Change the Main Camera position in the Hierarchy.
  11. Ground is a XZ-Plain whose size is 10x10 centered on the origin. Also, "AsianBoy" is at the origin. Let's set Transform Position (x, y, z) = (0, 1, -5) which is the end of Plane becase the Main Camera is too far away.




  12. Click the Play button to execute.
  13. "AsianBoy" moves with the keyboard arrow keys (↑, ↓, ←, →) or the 'w', a', 's', and 'd' keys. But it has floated in the air.




  14. Change the "Assets/Scripts/PlayerMove.cs" in the Project window.
  15. When moving the Player, consider acceleration due to gravity. It is one line to which red character part was added. With AsianBoy moving across the edge of the Plane, it now falls.

    PlayerMove.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMove : MonoBehaviour {
        public float velocity = 1.3f;
        private CharacterController charController;
        void Start () {
    	charController = gameObject.GetComponent<CharacterController>();
        }
        void Update () {
    	float h = Input.GetAxis("Horizontal");
    	float v = Input.GetAxis("Vertical");
    	Vector3 moveDirection = new Vector3(h, 0, v);
    	moveDirection.y += Physics.gravity.y;
    	charController.Move(velocity * Time.deltaTime * moveDirection);
        }
    }
    
  16. In addition, change the "Assets/Scripts/PlayerMove.cs" in the Project window as follows.
  17. Make it possible to move by keyboard operation only when the foot is touching the ground.

    PlayerMove.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMove : MonoBehaviour {
        public float velocity = 1.3f;
        private CharacterController charController;
        void Start () {
    	charController = gameObject.GetComponent<CharacterController>();
        }
        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);
    	}
    	moveDirection.y += Physics.gravity.y;
    	charController.Move(velocity * Time.deltaTime * moveDirection);
        }
    }
    
  18. Save the Scene.
  19. Here is the Unity's project file Mecanim.zip which I explain here.

Yoshihisa Nitta

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