
Cliquez sur un chapitre pour lancer le lecteur et votre vidéo.
-
Développement d'un RPG 2D avec Unity
- Préparation du projet sous Unity 2020 et des assets
- Création d’un niveau avec l’outil Tilemap + Collisions 2D
- Programmation du script de déplacement 2D du personnage
- Animation du personnage via Animator et postprocessing
- Créer une quête pour notre RPG + Dialogue PNJ
- Aller plus loin avec Unity et le développement 2D
Animation du personnage via Animator et postprocessing
Description
Nous allons maintenant voir comment animer le personnage principal avec les animations de marche dans les 4 directions en utilisant l’outil Animation de Unity ainsi que l’outil Animator.
Code source
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Character : MonoBehaviour { public float speed = 5f; Rigidbody2D rb; Vector2 dir; Animator anim; void Start() { rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); } void Update() { dir.x = Input.GetAxisRaw("Horizontal"); dir.y = Input.GetAxisRaw("Vertical"); rb.MovePosition(rb.position + dir * speed * Time.fixedDeltaTime); SetParam(); } void SetParam() { if (dir.x == 0 && dir.y == 0) anim.SetInteger("New Int", 0); else if (dir.y < 0) // bas anim.SetInteger("New Int", 1); else if (dir.x > 0) // droite { anim.SetInteger("New Int", 2); GetComponent<SpriteRenderer>().flipX = true; } else if (dir.x < 0) // gauche { anim.SetInteger("New Int", 2); GetComponent<SpriteRenderer>().flipX = false; } else if (dir.y > 0) // haut anim.SetInteger("New Int", 3); } }