Pickup Animation for Closest Object
Hello, I am working on a game with 4 platforms. Each platform spawns balls that will roll down to player. Player has 4 statements as left up, left down, right up, right down. And each has different animations connected to each other. And also I have 5 stages of pick up animations. If object is at the edge of the platform it will play the longest distance pick up animation, if a bit closer runs the one that picks up depending on the distance. Problem is, I dont know how to tell it to unity. First of all, I need to determine closest object in specific platform. Which means when I turn my player to left up, it will select the closest one at the left up platform and pick it up with the animation, when that one is destroyed, then it will pick up the closest one again until I turn my player away to another platform.
This is the animation sheet. Longest idle just idles when there is no ball to pick up. If there is, depending on the distance it should pick and play it. http://prntscr.com/cwgytl
This is the platforms. http://prntscr.com/cwgyx3
Thanks.
Answer by b1gry4n · Oct 20, 2016 at 06:00 AM
You need to store the objects in some sort of list or array. Whether you add them to the list when they are created, add them when they cross a trigger, or use "find".
Here is an example of finding the closest object in a list : http://answers.unity3d.com/questions/598323/how-to-find-closest-object-with-tag-one-object.html
After you sort them, the very first object in the array will be the closest. From there you would need to do a distance check (since they are sorted by distance, but you dont know the distance from the closest object) to determine which animation to play.
float dist = Vector3.Distance(transform.position, transformArray[0].position);
if(dist < 1.0f && dist > 0.5f){
//anim A
}else if(dist <= 0.5f && dist > 0.25f){
//anim b
}
//etc etc etc
Your answer
![](https://koobas.hobune.stream/wayback/20220612095020im_/https://answers.unity.com/themes/thub/images/avi.jpg)