Search Unity

Chess pieces moving animation

Discussion in 'Animation' started by Szaroslav, Dec 8, 2018.

  1. Szaroslav

    Szaroslav

    Joined:
    Dec 8, 2018
    Posts:
    1
    Hi everyone!
    I've been working on a chess game and I'd like to add moving animation. Partially I achieved my goal by creating a script, but I have no idea how to add easing curve. Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ChessPieceMovingAnimation : MonoBehaviour
    4. {
    5.     public float duration = 0.25f;
    6.  
    7.     private bool isXEqualZero;
    8.     private Vector3 destination = Vector3.zero;
    9.     private Vector3 move = Vector3.zero;
    10.  
    11.     public void SetDestination(float x, float y, float z)
    12.     {
    13.         destination = new Vector3(x, y, z);
    14.     }
    15.  
    16.     private void OnEnable()
    17.     {
    18.         move = destination - transform.position;
    19.         isXEqualZero = move.x == 0 ? true : false;
    20.     }
    21.    
    22.     private void Update()
    23.     {
    24.         transform.position = transform.position + move / duration * Time.deltaTime;
    25.  
    26.         if (!isXEqualZero)
    27.         {
    28.             if (move.x > 0 && transform.position.x >= destination.x)
    29.             {
    30.                 transform.position = destination;
    31.                 enabled = false;
    32.             }
    33.             else if (move.x < 0 && transform.position.x <= destination.x)
    34.             {
    35.                 transform.position = destination;
    36.                 enabled = false;
    37.             }
    38.         }
    39.         else
    40.         {
    41.             if (move.y > 0 && transform.position.y >= destination.y)
    42.             {
    43.                 transform.position = destination;
    44.                 enabled = false;
    45.             }
    46.             else if (move.y < 0 && transform.position.y <= destination.y)
    47.             {
    48.                 transform.position = destination;
    49.                 enabled = false;
    50.             }
    51.         }
    52.     }
    53. }
    54.