I had been reading many posts with similar problems and tried all suggested approaches for the last 2 days but still could not get my scenario to work. Therefore, I am posting my question with as much details as possible. The use case is shown in the attached file.
The ‘ball’ game object is inside the “BallContainer” parent object.
I have 3 animations: Idle, BallStraight and Shot1. Idle is the start position. When “Ball” button is clicked, I play the BallStraight Animation. The animation is simply a position transition. I just set the trigger for the animation to play.
anim.SetTrigger("BallStraight");
The animation does not work when “Apply Root Motion” in the Animator component is Checked. So I un-checked it. Then the animation works. The ball moves.
While the ball moves, button “Shot” is clicked. Then the ball should move to the right from its current position. To achieve this I used parent object position. (the script of code below is attached to the “Ball” object)
void Start ()
{
anim = GetComponent<Animator>();
defaultBallPosition = transform.position;
defaultParentPosition = transform.parent.position;
}
public void AnimateBallMovement()
{
transform.parent.position = transform.position;
transform.localPosition = Vector3.zero;
anim.SetTrigger(“Shot1”);
}
Now the ball moves right. When it reaches the boundary (which is a trigger collider), I have to move the ball back to “Idle” position. Since the “Idle” animation has no position coordinates defined, I have to set the position manually and change the animation state. Here I set it as follows.
public void ResetBallPosition()
{
transform.parent.position = defaultParentPosition;
transform.localPosition = Vector3.zero;
anim.SetTrigger("Idle");
}
The ball moves back to its original position. All works as expected to this point. Now when I click the “Ball” Button again, it does not play the “BallStraight” animation. Everything freezes. When I check the Animator window, I see the state has moved to “BallStraight” but frozen there. However, I never saw the ball move at all. It only plays once. I just want the state to reset, and continue playing the steps. What am I doing wrong? Is the animation somehow changing into “applyRootMotion=true” implicitly? Because that is the same behavior I see when I check the “Apply Root Motion” check box in Animator component in the start.