Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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.