This is an old revision of the document!


2D Animations and Interactions

2D animations in Unity

In Unity (current version) we have two possibilities to make 2D animations: sprite-sheet si bone-based.

In the first image, the character is presented in several positions, with a sequence of positions for different actions. This image represents a sprite-sheet, being very popular in 2D games. This allows you to clearly imagine how the game will move.

In the second image, the dragon is divided into several parts of the body (head, body, arms and so on), requiring a newer 2D animation technique, commonly called bone-based animation. As the name suggests, the animation will be bone-based, in which each bone in the body can have a specific action or animation. With all the main body parts separate, it allows developers to create animations directly in the editor. This new animation technique is very similar to the one used in 3D animation.

Resources

Animator Controller

Urmatorul pas este sa configuram aceste animatii astfel incat sa le putem folosi, si sa putem tranzitiona intre ele. Pentru acest lucru, vom folosi un Animator Controller. Acest controller functioneaza ca un automat de stari, fiecare stare putand fi o animatie.

Pentru a folosi animatiile se pot adauga cu drag-and-drop in Animator.

Pentru animatii fluente si controlate, trebuie sa definim tranzitii intre acestea. Tranzitiile au mai multe proprietati, printre care timpi de activare, conditii etc.

Conditiile sunt folosite pentru a controla cand are loc o tranzitie. Spre exemplu, putem folosi un parametru de stare, pentru a trece dintr-o animatie in alta, sau alte conditii (spre exemplu daca este pe sol, in aer, primeste damage etc.)

Spre exemplu putem folosi urmatoarea conventie pentru un parametru de stare:

  • ken_idle = 0
  • ken_walk = 1
  • ken_crouch = 2
  • ken_jump = 3
  • ken_hadooken = 4

si punem conditia de tranzitie de la Idle la Walk state=1. Acest lucru îi va spune personajului, dacă este în Idle, pentru a tranzitiona la animația de Walk dacă parametrul de stare se schimbă la 1.

Schimbarea starilor unui Animator

Pentru a schimba starile unui animator, trebuie in primul rand adaugata componenta de Animator pe obiect. Apoi, putem controla parametrii definiti prin functii disponibile in clasa Animator.

const int STATE_IDLE = 0;
const int STATE_WALK = 1;
const int STATE_CROUCH = 2;
const int STATE_JUMP = 3;
const int STATE_HADOOKEN = 4;
 
string _currentDirection = "left";
void Start()
{
        //define the animator attached to the player
        animator = this.GetComponent<Animator>();
}
 
void Update()
{
        if (Input.GetKey ("right")) {
          changeDirection ("right"); //schimba directia personajului
          transform.Translate(Vector3.left * walkSpeed * Time.deltaTime); //muta personajul
          animator.SetInteger ("state", STATE_WALK); //activeaza animatia
 
        }
}
 
void changeDirection(string direction)
     {
 
         if (_currentDirection != direction)
         {
             if (direction == "right")
             {
             transform.Rotate (0, 180, 0);
             _currentDirection = "right";
             }
             else if (direction == "left")
             {
             transform.Rotate (0, -180, 0);
             _currentDirection = "left";
             }
         }
 
     }

Animatie fundal

Petru a anima fundalul unui joc 2D, putem altera pozitia texturii. Astfel, trebuie sa definim un material cu o textura, si sa o aplicam pe un obiect (de obicei un quad). Dupa care avem la dispozitie componenta de Render in care putem accesa materialul si implicit texturile, la care putem adauga un offset, in functie de pozitia caracterului, sau o miscare in timp etc.

   renderer = GetComponent<Renderer>();
   
   renderer.material.SetTextureOffset("_MainTex", offset);
   
   // "_MainTex" is the main diffuse texture. This can also be accessed via mainTextureOffset property. 
   // "_BumpMap" is the normal map. 
   // "_Cube" is the reflection cubemap.

Pentru a functiona corect, textura pe care o folositi trebuie sa fie setata pe Repeat

Animation projectiles / shooting

For shooting animation, a status is generally used to update the player's animation, and the projectile is generated as follows (recommended):    * A script is added to the weapon (which is on the scene) that manages the projectile generation and direction    * Starting from the location of the weapon, a projectile (using the Instantiate function, already studied) is instantiated (prefab as a rule, it must not already exist on the scene, using the position of the weapon). The projectile has attached a script containing details of speed, damage, etc.    * The projectile has a continuous translational motion in one direction, until it reaches a limit or collider when it triggers

Animations and scene instancing

Instantiate (gameobject, position) is used for instancing objects. To generate content only in visual space on the screen, you can define objects that fix this frame, and then randomly generated in this interval. The generated objects can be scripted so that they have continuous animations and movements.

Character movement

The character's movement can be done using a RigidBody2D component and applying forces or movements.

rb = GetComponent<Rigidbody2D>();
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

rb.AddForce (movement * speed);

rb.MovePosition (transform.position + offset * Time.deltaTime);

Colision

For collision detection a collider2D can be used. For easy interaction, a Trigger can be generated at the collision event by ticking the Trigger box in the component. After that you can retrieve the event and the object with which the collision is made.

void OnTriggerEnter(Collider otherObject) {

		score += 10;
		scoreText.text = score.ToString();
		Destroy(otherObject.gameObject);

	}

Tasks

Making a 2D scroller game

  • Configure scenes for 2D
  • Add to the scene an image that represents the game background (animated)
  • Add an animated character to the scene:
    • it can be controlled from the keyboard
    • shoots projectiles at enemies
    • animated in several states (minimum 3)
    • has a score that increases when killing enemies
  • Add at least two types of enemies animated by different techniques
    • destroyed on collision with the player's projectile
    • destroyed collision with the player
    • generated dynamically, at random positions and they move continuously
pjv/laboratoare/en/03.1571832631.txt.gz · Last modified: 2019/10/23 15:10 by alexandru.gradinaru
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0