Animation d'un personnage en 3D sur Unity en C# et importation d'un joueur avec Mixamo (facile) !

3 min read 3 hours ago
Published on Sep 27, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you'll learn how to animate a 3D character in Unity using C# and how to import a player model from Mixamo. This guide is designed for beginners who want to enhance their Unity projects with animated characters. By following these steps, you'll gain practical experience in character animation and integration with the Unity game engine.

Step 1: Set Up Unity Project

  • Open Unity and create a new project.
  • Choose a template (2D or 3D) based on your needs.
  • Name your project and select a location to save it.

Step 2: Import the Character Model from Mixamo

  • Go to Mixamo.
  • Sign in or create an account if you don’t have one.
  • Search for a character model you want to use.
  • Select the character and choose animations that fit your project.
  • Download the character:
    • Choose the format suitable for Unity (FBX with skin).
    • Ensure animation is included in the download settings.

Step 3: Import the Model into Unity

  • In your Unity project, navigate to the Assets folder.
  • Drag and drop the downloaded FBX file into the Assets folder.
  • Select the imported model and check the Rig tab in the Inspector:
    • Set the Animation Type to Humanoid.
    • Click Apply.

Step 4: Create an Animator Controller

  • In the Assets folder, right-click and select Create > Animator Controller.
  • Name your animator controller (e.g., “PlayerAnimator”).
  • Drag the Animator Controller onto your character in the Hierarchy.

Step 5: Add Animations to Animator Controller

  • Open the Animator Controller by double-clicking it.
  • Drag and drop the animations you downloaded from Mixamo into the Animator window.
  • Create transitions between animations:
    • Right-click on an animation and select Make Transition to another animation.
    • Set up parameters as needed (e.g., “isWalking” for walking animations).

Step 6: Script Character Movement

  • In the Assets folder, create a new C# script (e.g., “PlayerMovement”).
  • Open the script and add the following code:
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;
    public Animator animator;

    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontal, 0, vertical);
        transform.Translate(movement * speed * Time.deltaTime);

        animator.SetBool("isWalking", movement.magnitude > 0);
    }
}
  • Attach the script to your player character in the Hierarchy.
  • Drag the Animator component from your character to the script's animator field in the Inspector.

Step 7: Test Your Animation

  • Click the Play button in Unity.
  • Use the arrow keys or WASD to move your character.
  • Observe the animations playing based on your input.

Conclusion

You've successfully animated a 3D character in Unity using C# and imported a model from Mixamo. You learned how to set up your project, import models, create an Animator Controller, and script basic character movement. Next steps could include refining animations, adding more complex behaviors, or exploring additional features within Unity to enhance your game. Happy developing!