• Unity
  • Use draw order from another track or deep copy draw order?

  • संपादित
Related Discussions
...

I have a case where I play an idle animation with the character looking down and the arms are drawn behind the torso.
I then aim a gun, where I control the angle of the arm from within Unity, I want the arm holding the gun be in front of the torso.

This should happen only when looking down, not left/right/up, and holding a gun. I tried fiddling with moving the arm slot up/down X positions, depending on where he looks, like this: change the draw order at runtime

However it's getting very complicated when I don't have absolute draw orders, and also I need to override it each frame, since the idle animation changes the draw order too.

So can I add a new TrackEntry, which contains only the arm slot and its draw order? I guess not, as the draw order is for all slots..

Or can I make a deep copy of the draw order list, and then override it for each frame as long as he's looking down?

Thanks!


I'm currently trying this to store the correct draw order from another animation track:

currentMovementTrack = skeletonAnimation.AnimationState.SetAnimation(movementTrack, "shoot_test", true);
drawOrderArm = new ExposedList<Slot>(skeletonAnimation.skeleton.drawOrder.ToArray()); // Clonse draworder from shoot_test

However, it seems as skeleton.drawOrder doesn't contain the draw order at the exact time as I do SetAnimation. is that correct? Can I force it to update?

I would rather suggest to perform these task in Spine instead of changing them in Unity later.
You can use Spine's IK-constraints to aim at a target, as also described on this thread here:
Look at target blend into animation
You can then control the aim-target in Unity, as shown in the Spine Examples/Getting Started/4 Object Oriented Sample unity scene included in the spine-unity runtime.

You can also change draw order mid-animation in Spine, which will make it much more controllable than changing it via code afterwards. You can then have an aiming animation with a draw order where the arm is in front, and other animations where the arm is behind certain other attachments.

Ouch, i spent about 2 weeks coding the aiming and shooting working in Unity with my own IK.. 🙂 However I'll consider doing it directly from Spine, maybe that would work better all in all, and require less code.

I made my own IK-thing to make the character aim, and the arms are rotating. However I made a slider, to adjust for different weapons, like a smaller gun, his arm is straight out, and a bigger gun, the arm is bent, like shooting it from the hip. (See gif below)

1) How do I do the same in Spine? Can I have some bone that I can change in Unity to control the bend of the lower arm, and still keep the aim IK working? Check the image below where I slide the "Lower arm rotation" slider up and down.

2) I also have recoil that I can adjust so it gives a kick, upwards, or a bend, pushing the gun backwards (Kind of like in the gif, but faster), which can be adjusted per weapon. But I guess, it just needs to translate to the correct bone and constraint setup in Spine, which is currently kinda hard for me at the moment 🙂

3) Also, how do I compensate for different gun offset in Y? Some guns might go up higher from the arm.

Sorry to hear that you have spent a lot of time on your own worflow.

If you are otherwise happy with your programmatic IK solution and only need to change draw order, then it should work to just create animations where the draw order is modified (and nothing else) and play these animations on another track. It might of course need some sync with the rest of your animations or could also be part of animations that are already playing (I don't know your setup, so it's a bit hard to guess). That's just a quick idea.

Regarding your questions:

1) How do I do the same in Spine? Can I have some bone that I can change in Unity to control the bend of the lower arm, and still keep the aim IK working? Check the image below where I slide the "Lower arm rotation" slider up and down.

Yes, this all is possible. You can modify bones and still have the IK applied. Your setup just needs to be adjusted for your requirements. I'm no constraint setup expert though (my solutions would only work, but very likely not be optimal and pretty), maybe Erikari can chime in and provide some recommendations for a good constraint setup here.

2) I also have recoil that I can adjust so it gives a kick, upwards, or a bend, pushing the gun backwards (Kind of like in the gif, but faster), which can be adjusted per weapon. But I guess, it just needs to translate to the correct bone and constraint setup in Spine, which is currently kinda hard for me at the moment 🙂

You can have a look at the Spine Examples/Getting Started/4 Object Oriented Sample example scene, where Spineboy can run and at the same time shoot with some recoil. You can also open the Spineboy projett in Spine and have a look at the shoot animation, it's only a few parts that are animated that can then be combined with other animations.

3) Also, how do I compensate for different gun offset in Y? Some guns might go up higher from the arm.

What doe you mean by "go up higher"? More recoil, or different positional offset?

Hello! I can help with the editor side setup.

A first suggestion would be: if you have two draw orders that override each other (perhaps because they're at different frames of the animations) remove them from the animations.
Since you can play several tracks at the same time, if you really need to change the draw order, you could create an animation that alters it.
Usually, the idle animation doesn't require a change in draw order, as it should be the closest to the setup pose, why do you need draw order keys? perhaps it can be set up differently.

Regarding aiming and recoil, you can set an IK constraint like Spineboy's one for aiming Spineboy example: Aiming
And you can have an animation with just one frame for each type of gun, where the gun is equipped and the bones are adjusted to accommodate it.
If recoil is made so that it pushes in the opposite direction of where it is aiming, then animating just the bones that are holding the gun should do the trick.

You can see a similar setup, with all the features you mentioned, in the Gunman asset pack: Spine: Asset Packs

Awesome thanks to both of you!
Having just the draw orders in a separate track should work, and no I don’t know why the idle animation animates the draw order 🙂

I will definitely check this out, thanks again!


Hey, I bought the Gunman asset, looking really good! But there doesn't seem to be an aim target IK, instead you seem to rotate the Aim Pivot. Did I miss anything, or would I have to add that myself?

Also, do you have any suggestions on how to set this up for up/down animations, or would it be straight forward? I guess I'd have to modify the recoil mechanics a bit.

Also since I already have a project, is there any way to copy constraints from one project to another, or should I rather set it up manually?

Thanks!

Hello again! to answer your questions, I believe Gunman sets the aim directly in Unity via code, you should be able to find examples in the asset pack.

You can also see a Unity example with Spineboy aiming instead here: GitHub - EsotericSoftware/spine-superspineboy: Example platformer game for spine-libgdx.

You'll have to re-do the constraints to apply them to your project, but dissecting the project in the asset pack should provide a good insight of how things could work. Unfortunately, constraints cannot be copied from a project to another.

I suggest studying both examples to decide how to best implement their mechanics into your project.

Great, thanks a lot, will consider that. I'm always amazed how many cool things can be done from within the Spine file itself. It's an amazing tool! 🙂

Thanks, glad you like it! 🙂

Hi again,
I have a question on the Gunman asset. I'm using Unity 2019, and I noticed there were two versions of the asset, 2017 and 2018.
However in using 2018 version in Unity 2019.1.8f1, the aiming is a but off off, see the video on the link below:
https://i.imgur.com/aP7IP8K.mp4

Is it the same if I use Unity 2018? Do you know if there's a fix for that, since 2019.1 is out and stable, and 2019.2 is probably coming soon.

Thank you in advance! 🙂

Sorry to hear that! Thanks for reporting this problem. I will have a look at it and get back to you as soon as I can offer a solution or bugfix.


I just discovered what went wrong there - it applies to all versions of Unity (2017 - 2019) and both packages:
The aimPivot GameObject which is the reference point for the line to the mouse cursor is set too low:

It should be placed higher up like this:

In action this will then line up correctly:

I will fix the example packs and re-upload them. I will let you know once they are on the server.
In the meantime you could simply move the GameObject up a bit as shown.


You can now download the updated asset packs, they have been uploaded to the server.

Thanks for the quick fix!

However this works only exactly for the gun, which is aimed exactly from the eye, but the shotgun that is aimed from the hip is still off. The launcher and sniper is very close to correct, but if you have the mouse close to the weapon you can see that it's off too.

I was first thinking to change the AimPivot in the setup animation of each weapon, but that moves the weapon, so maybe we would need an AimOrigin or something, that is used to detect where the aim is, which can be adjusted for each weapon.

I'm not fully aware of the setup in spine and the code, but something like that might work.