0
votes

I'm making checkers game in Unity, and and I have piece object with one tile move animation inside (GameObject). When I click new position, animation is triggered and piece moves. But after that, moving this piece to next tile(new coordinate) does not work !

Here is the code for that Part!

 if(selectedPiece.ValidMove(pieces, x1, y1, x2, y2, hasKilled))
        {
            // Tile jump check
            if (Mathf.Abs (x2 - x1) == 1) {
                if(selectedPiece.isWhite)
                {
                    if(selectedPiece.transform.position.x < mouseOver.x)
                    {
                        // default 
                        selectedPiece.GetComponentInChildren<Animator>().SetTrigger("tileJump");   
                        selectedPiece.transform.GetChild(0).transform.position = Vector2.zero;                     
                        //pieces[x2, y2] = selectedPiece;
                        pieces[x1, y1] = null;                           
                        //MovePiece (selectedPiece, x2, y2);
                    }else{

                         selectedPiece.GetComponentInChildren<Animator>().SetTrigger("tileJump");    
                    }
                }
            }
        }
1
Use Debug.Log() to verify that the code is executed as you want. Also, check your animator: maybe you miss a transition between two states - Basile Perrenoud
yeah, seems problem with my transition, Entry --> idle(null) ; AnyState --> newState(trigger) here is the animator transition. after animation done, it stuck. I have added new transition to entry, but after animation piece goes to old position again ! - Sherzodkh

1 Answers

0
votes

Animations with the animator are intended to be loopable, as such when you play it again it will always return to where it was.

One solution would be to through code set the position of the object to the intended position while at the same time resetting the animator.

So

transform.position = yourCheckersIntededPosition;

If you only wish the checker object to slide across the board you could also do this in code instead of animating it with the animator, then the position would always be true to where it is displayed.