Pour gérer plusieurs animations sur un même objet, le composant Animator est l'outil idéal.

Seul soucis : son utilisation est assez complexe, au début, car très abstraite.

 

Télécharger le projet modèle.

 

Voici un schéma expliquant le fonctionnement global du composant Animator :

Schéma de fonctionnement du composant Animator
Schéma de fonctionnement du composant Animator

 

Voici un exemple d'utilisation simpliste :

  1. Créer un nouveau projet de tests de fonctionnalités (prototypage)
  2. Créer 3 dossiers (Animations, Scènes, Script) et enregistrer la 1ère scène
  3. Insérer un cube dans la scène, le nettoyer et l’équiper du composant Animator
  4. Dans le dossier Animations, créer un AnimatorController destiné à ce cube
  5. Créer une 1ère animation d’attente puis une 2ème de mouvement
  6. Assigner l’AnimatorController au composant Animator du cube
  7. Dans la fenêtre de l’Animator, mettre « Attente » comme animation par défaut
    • Tester
  8. Créer un premier paramètre booléen ainsi qu’une première transition
  9. Créer le script de contrôle
    • Faire fonctionner la première transition
  10. Faire de même avec les autres animations & transitions

 

Voici un exemple de script de contrôles simples :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using UnityEngine;                                                                           //Utiliser le moteur Unity

public class CubeMovements : MonoBehaviour
{
        //Variables à assigner via l'inspecteur
        public string           animHautBasParamName;                   //Nom du paramètre déclencheur de l'animation Haut / Bas
        public string           animGaucheDroitePramName;               //Nom du paramètre déclencheur de l'animation Gauche / Droite
        public string           animDevantDerrierePramName;             //Nom du paramètre déclencheur de l'animation Avant / Arrière

        //Variables privées
        private Animator    _animatorCPT;                                   //Référence au composant Animator


        // Use this for initialization
        void Start ()
        {
                _animatorCPT = this.GetComponent<Animator> ();        //Récupération du composant Animator
        }
        
        // Update is called once per frame
        void Update ()
        {
                //Execution en boucle de trois fonctions gérant les différentes animations
                AnimeCubeHautBas ();
                AnimeCubeGaucheDroite ();
                AnimeDevantDerriere ();
        }


        //Methode gérant l'animation Avant / Arrière
        private void AnimeDevantDerriere ()
        {
                if ( Input.GetKey ( KeyCode.DownArrow ) )                                                   //Si on appuie sur la touche bas du clavier
                        _animatorCPT.SetBool ( animDevantDerrierePramName, true );          //On passe à true le paramètre boléen "animDevantDerrierePramName"

                else
                        _animatorCPT.SetBool ( animDevantDerrierePramName, false );         //Sinon on le repasse à false
        }

        //Methode gérant l'animation Gauche / Droite
        private void AnimeCubeGaucheDroite ()
        {
                if ( Input.GetKey ( KeyCode.LeftArrow ) || Input.GetKey ( KeyCode.RightArrow ) ) //Si on appuie sur la touche gauche ou droite du clavier
                        _animatorCPT.SetBool ( animGaucheDroitePramName, true );            //On passe à true le paramètre boléen "animGaucheDroitePramName"

                else
                        _animatorCPT.SetBool ( animGaucheDroitePramName, false );           //Sinon on le repasse à false
        }

        //Methode gérant l'animation Haut / Bas
        private void AnimeCubeHautBas ()
        {
                if ( Input.GetKey ( KeyCode.UpArrow ) )                                                             //Si on appuie sur la touche haut du clavier
                        _animatorCPT.SetBool ( animHautBasParamName, true );                        //On passe à true le paramètre boléen "animHautBasParamName"

                else
                        _animatorCPT.SetBool ( animHautBasParamName, false );                       //Sinon on le repasse à false
        }
}

Écrire commentaire

Commentaires: 0