- Home /
How can I animate a GameObject that isn't parented, but will need to be for the animation?
Hi, I'm working on a game, where the player collects trash. They drag trash cans up to a trash truck. There is an empty GameObject, with a Box Collider attached to it. Once the / a trash can is dragged up to, and inside, the collider, it detects this, and I will animate the trash can, being moved around, and shaken, having it's contents dumped out, into the trash truck. What I'm trying to do, is make it so that the trash can is parented to this empty gameobject, so that it can have a locally-placed(?) animation, no matter where the trash truck is. The problem is, it isn't parented to the GameObject at the start, and it shouldn't be. So how can I achieve this? Is this possible? Is there a better / alternative way of doing this, that I should consider?
If anyone needs any more information, please, let me know. Thanks!
Answer by Envans · Nov 24, 2020 at 01:24 PM
You can add an animation event to the animation of the trash can being shaken at the first frame of that animation then you can create a function in script that does what you want and set it to be executed at the animation the event, I think this is an easy way to do what you want.
Ok, but I don't know if you realize, what I'm saying is, this trash can isn't yet parented to the GameObject. Unless you're saying something like, I should parent it to the GameObject, temporarily, maybe, and animate it, then unparent it, and then, in the script, I can make it get parented to the GameObject, and play the animation, then? All in all, in the end, I just want it to be so that no matter where or how the trash truck is positioned, the trash can will be centered, at the back of the trash truck, and pivot around the edge / ledge of the trash truck, tipping over, into the bottom.
Since you want it to be perfectly centered I think that yes you should parent it temporarily while that animation is playing and the unparent it since you are you using animation. Another way would be to animate it in script with a tweening library like leantween or dotween.
Oh, ok. I didn't know that I could animate things like that. I'll try it, thanks!. Although, just to be sure: Say, the Empty GameObject is called Trashcan_parent, and the trash can is called Trashcan_Root. Trashcan_Root, currently, isn't parented to anything, and shouldn't be. The player drags the trash can into the box collider, which triggers a script, that will parent the Trashcan_Root to the Trashcan_parent. Then, it plays the animation. Once the animation finishes, it unparents the trash can (Trashcan_Root,), from the Empty GameObject, Trashcan_parent.
So what you're saying is, I should (in the scene editor,), parent the Trashcan_Root, to the Trashcan_parent GameObject, animate the trash can, then unparent it, reposition it to where it was before, then use that animation?
@$$anonymous$$vans I'm still having issues with it. Now, it'll play the animation, but it'll stop at the end of the "Start" Animation, it will never get to the "$$anonymous$$id", or "End", animations.
Sorry for the late response but I cant seem to be able to download the file maybe try posting the portion of the code that handle the animation related behavior.
Hi, sorry for the late response also, I have gotten it to work... ...sort of. Here, I just thought of something I've seen somewhere here in Answers before, I've just created a Pastebin Account, and put it (my code, it goes past the character limit here,), there. Here it is: The Pastebin.
Right now, I have several different "Chunks", or maybe, sections, of code, for Unparenting the Trash can from the Trash Truck. I don't know which one would be the best to use. I just remember not being able to ever seem to get it to work, which is why I had made so many of them. Currently, it does unparent, but it also will distort now, when it is parented to the back of the Trash Truck. It also can (and it unfortunately recently seems more like it's been happening more frequently, now,), sometimes cause weird, I guess I would say, glitchy, physics. It will cause the Trash Truck to shake, jitter, bounce, as it will it's own self, and it can cause the Trash Truck to shoot up a bit and launch up a bit into the air, and flip around, and sometimes it will itself. I don't know how I can go about fixing this, either. Thanks!.