How to prefab animations in Unity? - unity3d

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.

Related

Can you attach a sprite renderer to a child of a main game object and still have the animator access functions from the main object for events?

When creating a player or enemy in unity, I've learnt that it's best practise to attach the sprite renderer to a child game object so that you can easily reposition the sprite. However, doing this means that the animator also needs to be attached to the child game object, which means you don't have any access to the functions from the parent game object for animation events . Does anyone know a way around this?
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
No! What happened is you moved the animator root so the original property paths stored in your animation clip is invalid now.
The property paths are basically stored via the objects names.
What previously was e.g. (don't know the exact form of the property paths)
/Sprite/SomeProperty
would now need to be something like
YourSpriteObject/Sprite/SomeProperty
but since the animation still uses the first one and there is no Sprite component on your parent object it is invalid and displays as missing.
You will have to redo your animations or write an editor script for automatically changing all animation paths accordingly.
Here I once did something similar for renaming animated objects. You could probably write a similar thing for moving the animator component to a different object in the hierarchy
Alternatively expose the methods on your child object, and then forward them to trigger stuff on the parent via
transform.parent.GetComponent<YourParentClass>().YourMethod();
which of course I would either cache once or assign via the Inspector

How to stick the particle system to a game object?

We've made a flame particle system and we want to attach it to our sword in run animation while the animation is playing.The flame is suppose to be attached on the sword and in run animation sword is moving as expected,but when we the running animation plays the particle system is not attached to the sword, we couldn't find a way to make it attached to sword,it just runs the particle effect but its not sticking to the sword.Is there a way to attach it or do we have to make another game object any advices?
here is a picture of the particle effect and character
and as i sad here it is its not attached to the sword
There are three modes for the ParticleSystem set via the property
Simulation Space
Controls whether particles are animated
in the parent object’s local space (therefore moving with the parent object),
in the world space,
or relative to a custom object (moving with a custom object of your choosing).
Yours seems to be set to World space so no matter where your object moves the particles move related to the world and are not affected by your sword.
So simply set it to the mode you want via the Inspector or set ParticleSystem.MainModule.simulationSpace via code to ParticleSystemSimulationSpace.Local
Simulate particles in local space.
and attach the ParticleSystem to the sword or one of its children
or ParticleSystemSimulationSpace.Custom
Simulate particles relative to a custom transform component, defined by ParticleSystem.MainModule.customSimulationSpace.
So in the case of Custom additionally set the ParticleSystem.MainModule.customSimulationSpace to be your sword. It doesn't need to be attached to it or anywhere nested below it.
Try making the particle system a child of the sword game object.

Collider not animating

I have a roll animation for my character and added the Capsule Collider.Center and Capsule Collider.Height properties to the animation to change the collider's size during the animation.
It won't let me adjust the properties without changing the permanent properties of the collider. I can hit record or select a key and adjust the properties I want, but then all keys for those properties and the properties on the collider on the GameObject are changed to those new values.
To make sure that a collider can be animated, I created a new capsule in the scene, created a new controller for it, then added those same properties to a new animation. That works perfectly, the properties I'm trying to animate work just fine. Why is it not working on my other animation?
Also, I'm using Unity version 2019.4.18f1.
Edit: I can use events to change the collider's size through script, but it would be much smoother if I could animate it with the animator.
I sort of figured out the problem, it's somehow related to how I have my player character made in the Hierarchy. The object with the issue has one level higher of nested objects, that's the one called player. The one that works perfectly and allows me to animate the properties on the Capsule Collider, is the RPG-Character. Every component on the parent objects are identical, RPG-Character has an exact copy of every component on Player, along with their values. I don't get it, but this has fixed my issue.

Unity 5.3.8 Animator - Glitch?

I created a bunch of non-AI animations for enemies that do basic back.forth or up/down motions. Everything was working perfect until I started working on the Boss of the level. The boss has his own tag "Boss" and enemies have their own tag as well. Anyway, the problem is when I click start, every enemy leaves the game board. I can see them animated above the game board still doing their routines.
Any clue as to why this happened and how to fix it? I'd really, really hate to have to scrap all the enemies and start from scratch...
I used the Animation tool inside unity.
Extra Note: I created an EMPTY and moved all of my enemies into that Empty object to clean the hierarchy panel up. The animations were fine before this.
************** Currently Resolved **************
Whew! Okay, so apparently Unity doesn't like it when you move your animated stuff into an empty AFTER being animated. I FIXED them by simply removing them from the EMPTY that I had placed them in. However, I'd still like to know why this is. So any useful resources, links, manuals, personal insight/observations or anywhere I can read up on this would be appreciated!
Here is an explanation what happened:
The moment you dragged them all into the empty GameObject I guess this object probably wasn't placed on 0,0,0 in the Scene.
So Unity automatically changed all the local position values of your enemy items to fit the current position offset to the empty object.
Result: In the editmode they don't change their actual global position in the scene but their local position. This is supposed to happen if you just want to organize stuff.
However, now when you start the game and the animations are played, the animators change all the local positions to whatever is stored in your animations.
Result: all objects jump back to their original localPosition which had an offset to the empty GameObject.
To solve this make sure the empty GameObject is at position 0,0,0 and optimally has rotation 0,0,0 and scale 1,1,1 before you drag anything into it.
Easiest way to achieve that is by clicking reset in the empty objects Transform component before starting to drag stuff into it.

Why isn't Unity allowing me to apply certain changes to a prefab?

I have a prefab called square in the asset tray, I drag it into the scene to create an instance of the square prefab (in the hierarchy it is highlighted blue and has the select, revert and apply buttons in the properties). If I move/resize the instance and then press apply, the changes are applied to the prefab.
However, the square prefab has a script with a public game object. I drag the game object from the hierarchy into the slot in the instance's properties tab and is shows that the script is now referencing the actual gameobject. However, when I press apply this change isn't applied to the prefab (the prefab's script still isn't referencing any actual gameobject). All other changes to the prefab are applied.
I have tried dragging the instance into the asset tray to create a new prefab with the changes, but, as soon as I do, the script no longer references the game object.
I shouldn't have to create an entirely new prefab every time I add a public variable to a script so why can't I apply this change?
Ok you can't really drag an instance to a prefab. As a rule of thumb keep instances with instances and prefabs with prefabs.
I would instead find the instance from the prefab when this one is instantiated, with a method like findobjectoftype, or gameobject.find. Let me know if you want me to expand the answer
Solved: Prefabs can only reference other prefabs since there might not be an instance. Made the actual gameobject into an instance of a prefab and applied the prefab to the script
Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.