• Unity
  • OverrideAlpha and setting Bone IK

Related Discussions
...

Hi, I'm sorry if this is really simple, but I've struggled with it for several days and can't get it figured out.

I have a character with 2 arms, grabbing a gun.
I have IK targets on the gun, to position each hand/arm on to the gun.
Sometimes I want the back hand to grab a rope.

When the character is off a rope, I want it to use the animations (for where to grab the gun, and aim and such).

What method should I use for this?

Currently I have tried:

  1. SkeletonData Asset for the main body of the character.
  2. On that, I added a "Skeleton Utility" script.
  3. That let me "spawn a hierarchy".
  4. The back arm IK target bone, I added an override.
  5. In my player code, when he's grabbing the rope I tried this:
    backHandIKGrab.overrideAlpha = 1f;
    backHandIKGrab.bone.X = grabPos.x;
    backHandIKGrab.bone.Y = grabPos.y;
    

When he is not supposed to grab the rope (and should follow the animations) I tried this:

backHandIKGrab.overrideAlpha = 0f;

RESULT:
He always just follows the animation, not matter what.

SECOND TRY:
I also tried this for when he is to follow the rope:

                        
backHandIKGrab.overrideAlpha = 1f;
backHandIKGrab.transform.position = grabPos;

RESULT:
But that makes him always grab the rope, and never follow animations, even though I set overrideAlpha to 0f, when he should follow animations.

What am I missing?
Thank you!
-Nathan

The first solution is not working, because overrideAlpha is used for blending bone.X and bone.Y position to the transform position, if you directly set bone.X and bone.Y you bypass the blending effect of overrideAlpha. The respective code parts can be found here for better understanding:
spine-runtimes/SkeletonUtilityBone.cs at 3.8

However, your second solution is expected to work correctly. Are you perhaps encountering other problems or side effects? You can have a look at the example scene Spine Examples/Other Examples/SkeletonUtility Animated Physics and change the Override Alpha value of Head and Neck via the Inspector - there you should see properly fading bone override intensity. Is it reacting in any way in your problematic scene when you modify the Override Alpha value in the Inspector?

Could you show us a screenshot of your hierarchy of SkeletonUtilityBone GameObjects? Are there perhaps other parent GameObjects that are interfering or having other overrides active?

  1. I tested the example scene you mentioned, and the slider worked. It reduced the movement on the dragon's head as the rider moved it up and down.

2.
I've attached a pic of the heirarchy for the character.
"BackHandHold" is the bone that uses the code called "backHandIKGrab" in the original example.

I tried the following:
I removed all code that was modifying the bone's position.
At that point, the animations (like shooting the gun and running) took over the bone's movement as expected.
If I then, while the game was running, but in the Unity Scene view, grabbed the bone's override position, and dragged it around, the player's arm would stay moved up in the air, if I moved it up in the air. The overrideAlpha slider did not effect how much or how little the arm would stay moved in the air.

Basically, once, I changed the transform on the "BackHandHold" in the scene view, (it was 0,0,0), the bone would follow the animation, but also be moved up in the air (if for instance, I moved it to (0,1,0).

I guess I'm confused on how to get the bone to follow a world position versus follow the animation driven position.

3.
Hmmm.. I'm starting to think my heirarchy is the issue.
I have a gun bone.
Then the back hand is IK'd to the gun bone.
So the animation for shooting moves the gun back and forth.
And the back-hand IK's to grab the gun.
But I want the back hand to grab a rope occasionally, but also snap back to the gun's grab point.
Maybe I can't parent them like this? I shouldn't have "BackHandHold" parented under the "torsoGunPivot"?

Thanks for posting the bone hierarchy.

Just to be sure: I assume that you have not re-parented any of the bones, but please note that SkeletonUtilityBone does not allow for simple reordering in the hierarchy (you would need to explicitly set the Parent Reference parameter in the Inspector).

MommysBestGames wrote

Basically, once, I changed the transform on the "BackHandHold" in the scene view, (it was 0,0,0), the bone would follow the animation, but also be moved up in the air (if for instance, I moved it to (0,1,0).

A few questions regarding your setup:

  1. What are the Inspector properties of the BackHandHold SkeletonUtilityBone component, did you set it up to follow both position and rotation?

  2. Does BackHandHold behave as expected when you set the Override Alpha parameter to 0 or 1 via the Inspector and disable any of your code?

  3. Does the bone follow the Transform correctly when Override Alphais set to 1 and you move it around via dragging the Transform around in the Scene view?

  4. Does the IK bone sertup work as desired when you test it in the Spine Editor?

Apologies if you already answered part of the questions and I misunderstood your description above.

Thanks for helping me figure this out!
Questions:

  1. All values are set to inherit (so yes, position and rotation are set to be overridden).
  2. Yes.
    When I disable the code completely, the back and front arms do the following:
  3. Open a very quick 'shoot recoil' animation, which moves on the 'torsoGunPivot' bone back and forth, to move the gun back and forth and the front/back hands both move with it (as they are IK'd to the gun).
  4. They also follow the gun as I use code to aim the gun around when you move the right stick.
  5. If I set override Alpha to 1, I can drag the back-hand IK around, and it follows the transform, HOWEVER (and this may be a clue!) once I move the transform, and offset it some, I can't get it to follow the gun again.
    If I set override Alpha to 0, it stays stuck, just where I left it. That doesn't seem to matter.
  6. Yes, moves around when I drag the IK bone around, as I expect, and follows the gun, when I move the gun around in Spine.

So, I noticed if I override the front hand as well, the same problem happens.
Both hands jiggle a little bit with the gun shoot animation, but if I drag them around, they stay stuck like that.

I was hoping there was a way to get them to snap back to a starting position.. but maybe this isn't an animation issue at all, but more of a thing where I should store their starting offset, and when I want the arm to be "off the chain and back on the gun" I should be resetting the position that way through code.

Maybe because most of this is IK driven to follow the gun rotation, that's the issue?

MommysBestGames wrote

3. If I set override Alpha to 1, I can drag the back-hand IK around, and it follows the transform, HOWEVER (and this may be a clue!) once I move the transform, and offset it some, I can't get it to follow the gun again.
If I set override Alpha to 0, it stays stuck, just where I left it.

I fear that we will not solve this problem via a forum discussion alone.
Could you create a minimal Unity project where this problem still occurs and send it as a zip file to contact@esotericsoftware.com? Then we can have a look at it.

Harald,
Okay, I've made a smaller animation test project, and written to the contact email.
Thank you.
-Nathan

We received your email, thanks for the reproduction package. We will have a look at it and will get back to you as soon as we figured out what's going wrong.


I think I know the cause of the problem:
Your animations do not change the position or rotation of the hand (or the IK target bone) at all, relying on the initial setup pose position. Now when you change the position via an override, no animation track will reset this value - thus it stays as is.

You could either add some position and rotation keys in the idle animation, or maybe as an alternative reset the skeleton to setup pose via code as follows:

else
{
    backHandIKGrab.overrideAlpha = 0f;
    this.GetComponent<SkeletonAnimation>().skeleton.SetBonesToSetupPose(); // replace with nicer code / cached SkeletonAnimation and don't call this every frame, only upon switching from holding to non-holding.
}

That worked when I tested it.
Please let me know if that resolves the problem for you as well!

5 أيام لاحقا

I used the code method and it worked for me.
Thanks!

backHandIKGrab.overrideAlpha = 0f;
this.GetComponent<SkeletonAnimation>().skeleton.SetBonesToSetupPose();

Thanks for getting back to us, glad to hear it works now!