How to reset the animation after a gameobject is deactivated in Unity? - unity3d

I have a simple cube gameobject which has an animator component attached to it. This animator only controls one simple animation clip, this animation clip simply changes the scale of the gameobject from (1,1,1) to (2,2,2) in half a second.
Now in an another script lets say GameManager I trigger this animation by anim.SetTrigger("Scaleup") and thats works fine where the cube gameobject scales to the correct values which is (2,2,2).
All i'm after here is: When this cube gets deactivated and then reactivated again the animator does reset to its default state (Idle state) but the size never gets back to its original scale which is (1,1,1) and stays at (2,2,2) no matter what I do which it seems impossible in unity.
How can I reset the values of the animation clip when a gameobject gets deactivated and reactivated again?
I tried doing this anim.enabled = false and anim.enabled = true but it doesn't work. Help Guys.

Animator does not change any parameter which is not changed in the animation. In your example, the scale was changed by Scaleup animation and is never mentioned anywhere else.
Add a new simple animation to Idle state, add game object scale to it and make it constant (1.0, 1.0, 1.0). That will effectively reset your object's scale.

Related

Animation changes transform when entering game mode

In my Unity 3D project I've added an animated body as a child to the Player object. The player moves as is should, with the right animations displaying for each key. The problem however is that the transform of the child changes when I enter game mode, meaning the character is walking above the ground. In scene mode it's relative to it's parent, it's grounded, but when I switch to game mode the character suddenly changes it's y-axis transform. I also checked the inspector for this object, it's y-axis changes.
I tried disabling the movement script, to check if this could have anything to do with it, but nothing changes. The transform still changes. I'm new to this problem, and every thread I've found on the topic hasn't helped much. Does anyone know what might cause this?
The hierarchy. mixamorig:Hips is the one changing it's transform. The animations and animation skin I'm using is from Mixamo.

How do I have an animation always start from the same position on screen instead of the animation playing from its current position?

I'm working on a menu system but am having trouble with the animations.
I need to have it so that at the start of the animation its position starts somewhere else. So if the panel has moved to the right off the screen, when I hit the button it can come in from the left. Instead, no matter what I've done, the animation will just play from where ever it currently is and I can't move its position.
If you're using the Animator system, it always considers the GameObject local position.
So you could parent the animated GameObject and move it's parent around, while preserving the animation coordinates.

Unity3d Animation - How to make the player to stay at the target position after the animation completes

Created a simple animation to move a cube from (0,0,0) to (5,0,0).
When the animation stops, cube is going back to (0,0,0). How to make sure that the cube stays at (5,0,0) even after animation completes
You could also checkout the AnimatorState WriteDefault value.
Whether or not the AnimatorStates writes back the default values for properties that are not animated by its Motion.
By default it is enabled so if the State is left the Animator resets to the default values of your inanimated objects.
If you disable this value than the last state stays persistent although the state is left or the Animator disabled.
Got it working using simple Animator state machine with idle, moveLeft and moveRight states and appropriate triggers!!!

best way to change 2d sprite to animation

I have a GameObject for Chest(with Collider and SpriteRenderer)
By default it is a static sprite on the screen
now i want to add animation to this game object when open is triggered (by script), I do not have Idle animation, idle animation must be the sprite itself
what is the best to add an animation(no looping, 1 time and then show the animated sprite) to sprite?
I can do it by adding the sprite array and using an coroutine to set the sprite periodically with 0.2f delay (it work fine but not sure if it is the only way which using coroutine with delays to simulate an animation by myself...).
I can do it by create 1 more GameObject with the Animator, when the box open event is triggered, I set the original GameObject to inactive and instantiate the GameObject with the animation (but it requires to create 2 prefabs for two different gameObject).
But none of them look proper way to do this. What is the best solution for this?

How to move an animated AI character in Unity3d

Current Situation
I have a character and a "walking" animation.The animation is used in an animation controller. All assigned and done with the Inspector, no scripts involved. "Loop" is active.
When I hit the "play" button, the character's position moves according to the animation.
Problem
Once the animation reaches the end, the character gets relocated back to the origin point.
Question
How do I achieve that the character continues walking forward?
I read that I should activate "apply root motion" which I did, but that doesn't change anything, the character's position still gets moved back to the origin after the animation ends.
do all your animations in place and move your object via code , change player position with code and let your in place run animation play.
I really dont think that this can or should be done without any scripts.
Even currently playing animation should depend on parameters of the animator, as well as the movement itself.
And you won`t have to write a separate script for every state.
One script can get the user input, assign animator parameters (e.g. if the player had pressed LeftCtrl it sets 'Crouch' parameter to true and animator triggers related animator state/transition) and update character position depending on the speed and direction values.
In order to save some time finding appropriate value of movement speed to match your animation, you can add an animation speed multiplier to every state in the animator and make it a parameter. In that case one variable with speed value will modify actual movement speed and animation framerate.