Search Unity

  1. Unity 2018.3 is now released.
    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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. 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.