I’m animating ducks swimming in a pond. I successfully animated the duck’s circle of movement. I have duplicates of the same duck and I want to attach the single animation to all the ducks and move them in different areas of the pond.
When I run the animation they all clump to the spot I animated them instead of running the animation where I placed them. I’ve tried adding an empty game object, zeroed out the translation and parented the duck objects to the empty game object but they still clump to one spot.
Is there anything I can do to fix this? Or do I have to make unique animations for each duck?
I might struggle with explaining this but I think I know what is happening.
So when you created the animation you probably animated the top parent of your gameobject. Thus “hard coding” what the x y and z chordates of where the animation will take place. So for example if your ANIMATION starts at 0,0,0 and ends on 5,5,5 it doesn’t matter if your gameobject starts at 8,8,8 as soon as the animation begins it will jump to 0,0,0.
This holds true for any gameobject using this animation.
The way I get around this is I create an empty gameobject and parent it to whatever I want to animate. Therefore you are animating the x y and z of the child so it’s chordates will revolve around whatever the parent is.
You can then stick your parent object anywhere you want and the child will animate in relation to the parent’s location.
you are right and that is what I did. more over, I created a prefab of the empty game object that includes my animated game object and all animation is working cool as wanted. thanks!
It does answer the question, quite succinctly. You just need a reference to an instance of a class wherever you want to call one of its instance methods. Edit: On no I take that back it doesn't completely you are right.
A solution would just be using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestB : MonoBehaviour { public void MyMethod() { } } public class Test : MonoBehaviour { public TestB AnotherClass; void Update() { AnotherClass.Invoke("MyMethod", 12); } } ---------- That said I am quite sure that on release it will optimise that call away anyway so they are to all intents and purposes they compile to the exact same thing.
Right on man - solved my issues - at least with position animations - THANKS
Absolutely not. The question was asking how to Invoke a method in another class, the answer is Invoking in the same class. They are fundamentally different and can introduce extra issues that would not normally exist to do with object lifecycles, static v instance. More importantly, it's simply not the answer to a very specific question. If he/she was asking for a workaround, maybe this would be the right answer. But they weren't, they were asking "invoking method from a different class" This means the invoke call has to be on a DIFFERENT class to the method being invoked.
did you solve your problem?
– rara