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.
Voici un schéma expliquant le fonctionnement global du composant Animator :
Voici un exemple d'utilisation simpliste :
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 } } |
Ce site a été conçu avec Jimdo. Inscrivez-vous gratuitement sur https://fr.jimdo.com
Écrire commentaire