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 John Lemon's Haunted Jaunt: 3D Beginner - Unity Learn):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float turnSpeed = 20f;
Animator m_Animator;
Rigidbody m_Rigidbody;
Vector3 m_Movement;
Quaternion m_Rotation = Quaternion.identity;
void Start ()
{
m_Animator = GetComponent<Animator> ();
m_Rigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
m_Movement.Set(horizontal, 0f, vertical);
m_Movement.Normalize ();
bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately (vertical, 0f);
bool isWalking = hasHorizontalInput || hasVerticalInput;
m_Animator.SetBool ("IsWalking", isWalking);
Vector3 desiredForward = Vector3.RotateTowards (transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation (desiredForward);
}
void OnAnimatorMove ()
{
m_Rigidbody.MovePosition (m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);
m_Rigidbody.MoveRotation (m_Rotation);
}
}
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?
