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?
Related
I have already made a sequence frame animation in Unity with Mecanim system. But I do not want to setup Mecanim's animator component and animator controller. I think Legacy Animation System is much lighter and easier for my project.
I first added Animation and Sprite Renderer component to an object.
And then using Animation window to create a property of Sprite Renderer's sprite.
Finally, added every sprite to key frame and make sequence as usual.
But After the settings, I found the sprite renderer or image component can not work. The animation component could not drive the SpriteRenderer.sprite or Image.sprite.
So is it must be achieved by script?
In Unity 2D, is there a way to get the current sprite used either by a SpriteRenderer or an Animation? The idea I have is to have multiple GameObjects to use for a player character, and from the main one which receives animations and the script, edit the others based on the sprite currently used.
Now, I know that to get the current sprite name I can do this:
GetComponent<SpriteRenderer>().sprite.name
But that will only return the sprite used for the main GameObject's SpriteRenderer, and the name of it would not change even as animation changes it in Game Mode
GetComponent<SpriteRenderer>().sprite.name
This 100% does work , just tried it now and it is changing the name for each current sprite that the animator is changing.
You need to give more information and maybe a video since there is a problem somewhere else.
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.
created a prefab for door and added the prefab doors to the scene. This prefab door has an animator. The problem is when I try to change the animator x,y,z position on the second door in the scene it changes the first door.
I tried duplicating the doors and changing the position x y z but it changed the animator for the first door.
I just have a script that play the animator.
The expected result should be that I can drag and drop doors as needed without creating individual animators for each door. It looks like i need to create animators per door there is and also this probably will require me to create new scripts per each door. It seems awkward since I should be able to add doors and just chaning the position x y z in the animator.
The problem is that their Animator component uses the same AnimatorController asset which uses certain AnimationClip asset references for it's states.
I don't know if there is an easier way for achieve but I would do the following:
For what you want you would have to copy the AnimatorController asset (CTRL+D) as well as the AnimationClip asset you want to change.
Then in the cloned AnimatorController click on the according State you want to change and in the Inspector reference the according cloned AnimationClip.
Now you have a copy of the AnimatorController using a different AnimationClip but with the same states, traditions etc. so you can edit both animations and controllers individually.
finally in the Animator component reference the according AnimatorController for each prefab.
The remaining problem maybe is now that for the future if you change the States, transitions and especially parameters you have to do it in both AnimatorController assets individually.
That is perhaps not because of an animator, but a specific behavior of animation in Unity. Animation animates transform property according to parent, as far as I understand. So the first parent of your prefab is captured, and its transform property is used for animation. But I'm not exactly sure about this explanation - the actual behavior is covered under the Unity's source code. A common solution for this that I found is to create a parent or container inside your object's prefab. I don't know why it works, but it works. Perhaps because the parent of your animated object is now always the same.
Parent can be just an empty game object. So you make your door prefab, place another empty game object into this prefab, make your door a child of this container, set 0 for all coordinates of your door according to container and for the container itself. Now you can add an animator component to your object, which you want to animate (not the container), and animate it inside the prefab scene. After that you can place the copies of this prefab (container + your animated object inside) wherever you want inside your game scene - all animations of copies will proceed independently.
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!!!