Animation stops object movement.

Hi guys,

This is the first time I am trying to implement a basic animation to my object. My object which is a ball moves in the forward direction of its transform. I created a basic 360 degree rotation anim using Unity’s Animation tool and applied it to the ball so that when my ball moves around in the scene it looks like its rotating at the same time. The animation plays correctly but when I place it in the update method of ball script, it only keeps playing the animation but not do anything else like update its position every frame. I have switched off the Play Automatically checkbox but still it doesnt work. Here is my code below
for the update function.

void Update () {
	    
		horizontalVector = new Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
		
		if(horizontalVector.magnitude > maxVelocity)
		{
		 horizontalVector.Normalize();
		 horizontalVector *= maxVelocity;	
		}
		rigidbody.velocity = new Vector3(horizontalVector.x,0,horizontalVector.y);
		rigidbody.AddRelativeForce(0,0,moveZ);
		
		animation.Play(BallAnim.name);
	}

Is it something to do with the rigid body attached? I can play the animation but not update the position at the same time but when I remove the animation the object updates per frame and moves around. What could be the problem?

The animation overrites the settings from the rigidbody. One solution may be to place the animation with the mesh in a child object of the rigidbody. Like that the animation doesn’t clear the position settings from the rigidbody and it locally plays the animation, relative to the rigidbody.

2 Likes

Awesome, perfect! I deleted my animation on parent rigidbody, added a child object and made animations to this child object. On my parent object i unchecked mesh renderer and applied all my parent materials to the child object. I hope this is what you meant. It works though :slight_smile:

Thanks a lot again!

All you needed to do was create an empty GameObject and make your animated object a child.

There is no need to even make it as a child. You can just Use the combination of…
this.GetComponent.enabled=true;
this.GetComponent.enabled=false;
and
this.GetComponent.Play();

3 Likes

Hi guys, it just doesn’t work, If I create an animator on a child object the animation doesn’t start at all.

5 years till the answer and it still works! Thanks.

Great explanation! Using the new 2D animation (preview) package (don’t use it, it’s terrible), and had the animation override the forces being added to the Rigidbody.

Created a child object, added the animator component to that object and moved all other child objects to that new object with the animator.

Ex:

  • MainGameObject (rigidbody, script, colliders, no animator!)

  • AnimationParentObject (only has the animator component)

  • Everything else!

  • etc…

1 Like

Here’s the rule: any object that has a keyframe in the currently active animation cannot move (except for by the animator), it’s FROZEN. The rule stretches. If there is a transition arrow from the currently active animation to another, that one is considered active and no objects with keyframes in that animation are allowed to be moved outside the animator.
Summary: If an object cannot move because of the animator, it is involved in the currently active animation or one of the transition animations PERIOD

I did it for 2D and the sprite is invisible, how do I fix this???
Edit: nvm the animation was just small, I just had to scale it up

I have a similar problem and my sollution is:

  • create a boolean start in false called animationEnd;
  • create a method called AnimationEnd() who put the boolean on true and deactivate the animator.
  • In the animation, I’ve created an “animation event” at the finish of it, who call “AnimationEnd()”.

private bool animationEnd = false;

public void AnimationEnd()
{
animationEnd = true;
this.GetComponent().enabled = false;
}

it is still the most practical solution

Thank you! I was looking for a way to do so without making another gameobject and stuff, and this works perfectly!

Can you elaborate on what you did with those three commands and how they enabled rotation during animation?

This thread is ten years old now. Please don’t necro-post.

If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.