1
votes

I'm trying to make a "player can't move while this animation is playing" check in my method for movement.

I have a 3x8 grid of panels I'm using for this application, with the player moving from panel to panel. I have 2 animations for this: MovingOut which plays when the player is moving away from a panel, and "MovingIn" which plays when the player is moving into a panel. So the flow I want is:

Player presses a movement key → movement is disabled → "MovingOut" plays → player's transform.position is moved to the target position → "MovingIn" plays → movement is re-enabled.

Each animation is only 4 frames. I currently have an animation event at the beginning of "MovingOut" which sets an int CanMove to 0, and another animation event at the end of "MovingIn" which sets CanMove to 1.

Here's what my code looks like so far:

public void Move(int CanMove)
{
    //this lets me use panelManager to access methods in the PanelManager script.
    panelManager = GameObject.FindObjectOfType(typeof(PanelManager)) as PanelManager;
    animator = GetComponent<Animator>();

    if (Input.GetAxisRaw("Horizontal") == 1 && CanMove == 1) //go right
    {
        movingToPanel += 1;

        if (IsValidPanel(movingToPanel))
        {
            //play animation MovingOut
            animator.Play("MovingOut");
            transform.position = panelManager.GetPanelPos(onPanel + 1);
            onPanel += 1;
        }
        else
        {
            movingToPanel -= 1;
        }
    }
    //else if( ...the rest of the inputs for up/down/left are below.
}

I have MovingIn set up in the animator so that it plays at the end of the MovingOut animation, which is why I don't call it in the script:

I can't for the life of me figure out how to get CanMove passed into the method without being forced to define it upon calling the method. Currently I have Move(1); being called in my Update() method just to check if movement is working (other than this issue I'm having) and it works in the sense that I can move panel to panel, but since CanMove is being defined as 1 in the Update Function, the animation's events don't prevent the movement.

Any insight would be greatly appreciated!

2

2 Answers

3
votes

You should change your design slightly. Remove canMove as a parameter being passed to Move and make it a field of the player's class. Then have one function to set canMove. Then have a separate function that allows you to move if canMove is true. Something like this:

private bool canMove = true;

public void SetMove(int setCanMove) // called with animation events
{
    canMove = setCanMove == 1 ? true : false;
}

public void Move()
{
    if (Input.GetAxisRaw("Horizontal") == 1 && canMove == true) //go right
    {
        //movement code and animation call...
    }
    // Other directions...
}

Then you can call the setMove funciton using your animation events and stop the player moving for the duration of them. i.e call setMove(false) at the start of the MovingOut animation and setMove(true) at the end of the MovingIn animation. This will stop canMove being set in your Update loop.

0
votes

you can get the integer value from animator's GetInteger function which will return the integer value set to current animation.

if(this.GetComponent<Animator>().GetInteger(canmove)==0)
  //move
else
  //can't move