Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to make the MMD model move?

Discussion in 'Scripting' started by Penny_Lu, Nov 9, 2019.

  1. Penny_Lu

    Penny_Lu

    Joined:
    Oct 28, 2019
    Posts:
    12
    Hello all,

    I just started to learn Unity and imported a model by MMD4mecanim into Unity. What I want to do is simple: control it to move.

    The model has physics (hair, cloths, etc.), which shows properly in Unity. And I prepared two animations for it: walk and idle.

    I set the animator controller and used the script as follows (I followed the tutoriallJohn Lemon's Haunted Jaunt: 3D Beginner https://learn.unity.com/project/john-lemon-s-haunted-jaunt-3d-beginner?language=en):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float turnSpeed = 20f;
    8.  
    9.     Animator m_Animator;
    10.     Rigidbody m_Rigidbody;
    11.     Vector3 m_Movement;
    12.     Quaternion m_Rotation = Quaternion.identity;
    13.  
    14.     void Start ()
    15.     {
    16.         m_Animator = GetComponent<Animator> ();
    17.         m_Rigidbody = GetComponent<Rigidbody> ();
    18.     }
    19.  
    20.     void FixedUpdate ()
    21.     {
    22.         float horizontal = Input.GetAxis ("Horizontal");
    23.         float vertical = Input.GetAxis ("Vertical");
    24.        
    25.         m_Movement.Set(horizontal, 0f, vertical);
    26.         m_Movement.Normalize ();
    27.  
    28.         bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);
    29.         bool hasVerticalInput = !Mathf.Approximately (vertical, 0f);
    30.         bool isWalking = hasHorizontalInput || hasVerticalInput;
    31.         m_Animator.SetBool ("IsWalking", isWalking);
    32.  
    33.         Vector3 desiredForward = Vector3.RotateTowards (transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
    34.         m_Rotation = Quaternion.LookRotation (desiredForward);
    35.     }
    36.  
    37.     void OnAnimatorMove ()
    38.     {
    39.         m_Rigidbody.MovePosition (m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);
    40.         m_Rigidbody.MoveRotation (m_Rotation);
    41.     }
    42. }
    However, the model just turned around at the same location and didn't move when I tried to control it with the keyboard. Do I need to modify the script when I use a model with physics imported by MMD4mecanim? How to solve this problem?
     

    Attached Files:

  2. Penny_Lu

    Penny_Lu

    Joined:
    Oct 28, 2019
    Posts:
    12
    I've solved this problem. The script is right, and what made trouble was the configuration. The default status of "root bone" is "none" after the model is translated by MMD4mecanim. I changed it to the "center bone" and the model can move properly :)