In unity how can I get a particular Bone Position from the Current Running Animation? I want to attach a Ingame Object to a particular bone.
Unity - Get Bone Position on Runtime
- تم التحرير
SkeletonAnimation.skeleton.FindBone("bonename"); does not seem to works.
- تم التحرير
It should, but what you get is a Spine.Bone object, not a Unity Transform.
"Attaching" it means updating the position of that object every update. It shouldn't be an expensive operation of the object was parented to the SkeletonAnimation object's transform too. Then you'd only have to update with local transform values.
[edit (09/2014): new Unity features]
For a transform-attaching functionality, use the BoneFollower.cs
script that comes with the latest Spine-Unity runtime. No need to worry about implementation details (kind of).
Was just looking for the same:
How do I get the world position for a spine bone ? bone.WorldX and bone.WorldY seem to return always the same value , even if the skeleton is moved in the world (e.g. by changing the transform.position value of the gameObject that has the SkeletonAnimation Component.
Do I have to tell the Spine Component to update the world coords first? If yes, how do I do that?
Strongly suggest you use either the BoneFollower component or use the Skeleton Utility tools to go through it. then you can just reference the transform of the bone.
additionally, bone.WorldX and Y are relative to the object that has the SpineAnimation component on it. if you just want the real world position, use transform.TransformPoint( new Vector3( bone.WorldX, bone.WorldY, 0) )
I see....
Thanks for the answer Mitch. And thanks for the work on the Skeleton Utility. I use that in other places, and it's awesome! Thanks for the effort!
For just getting the bone position once, using it seemed a bit overkill.
Now, about
bone.WorldX ...
I would say that the naming is not ideal: In the context of Unity I would expect "worldX" to be a global Unity world coordinate value. Not a coordinate local to the transform. I feel something like "positionInsideOfTransform" would be a bit more explanatory. I guess it's probably not that easy since the bone class was written mainly to be used "inside of Spine", and not to be used from the Unity context. Does that make sense? Just wanted to share my experience/confusion, maybe there's a way to change this to make it a bit more clear in the future.
About using BoneFollower: Just looked at it. I use a event-driven approach for my game code, and in this case I only need to get a bone position once to spawn another game object. For that, I prefer to store a reference to the bone in my component, and then get its location when needed, not every frame.
- تم التحرير
If you only need it once, Mitch's recommendation should be enough:
transform.TransformPoint( new Vector3( bone.WorldX, bone.WorldY, 0) )
I think TransformPoint
returns a Vector3
for you to use.
@Mitch, This does sound like a good extension method for Spine-Unity. I feel like we talked about this before. Or did we stop at the color getters and setters?
For the naming, I agree it can be misleading for someone looking for the world position of a bone.
I'm almost certain it's some kind of legacy thing that has to do with how Spine editor's internals were put together originally. After all, in the editor, there is no other "world". Changing variable names definitely doesn't just affect Spine-Unity or other Spine-C# users (XNA, MonoGame) but also users of all the other languages too; Spine runtimes mostly share these variable and method names across languages. I wouldn't hold my breath for an immediate change.
Just hypothetically though, I wonder what would be good, short names instead of worldX
and worldY
.
A skeleton is made up of bones which have a local transform and a world transform. You then stick the skeleton in a Unity game object. Now the skeleton's world transform is the game object's local coordinates. The bones still have a local and world transform, there is just another layer above that which is the bone's world transform * the game object's transform. From the skeleton's perspective, the name world transform is correct since it doesn't know anything about transforms above it.
I don't think naming methods can make it clear which coordinate system you mean. We don't want names like "boneLocalX", "skeletonWorldX", etc. The current names make sense in context.
Just want to say this tripped me up too. The Unity convention is for "world" to denote an absolute world position, so that's what I was expecting. Some kind of Unity specific extension method would be great for this, if possible.
Is there any way i can set the "depth" of the BoneFollower "inside" the skeletron itself?
I also found a video about Sprite Attachments but cannot find the script in the add component menu...
What is the difference between BoneFollower and Sprite Attachment?