• Unity
  • mixing IK aiming with other animations

Hey all,

I was trying to throw up a quick rig that would allow for aiming akin to what is present in the spineboy pro example (e.g. running and aiming; jumping and aiming etc.). I set up an animation for aiming that had my firing arm on my character set to track an IK target 100%. The jump and run animations I am using do not have IK set but they are running at a lower track (0). When I fire, I have an animation called "shoot" that runs on track 1 and one called "aim" that runs on track 2. When standing still, I am able to aim at, and shoot toward, my IK target, but when running, jumping, or falling with my character I just fire wildly because the default animations play out without having higher track animations take over. I did code this a bit differently than the example so perhaps that's the issue...but I don't clearly see a reason why it wouldn't work. I am hoping someone else does. P.S: I know the string compares are not fast checks, those will be optimized later (assuming this method survives).

Please find a code snippet below. Thanks for any help!

Related Discussions
...
  • Modificato
ultrafop ha scritto

When standing still, I am able to aim at, and shoot toward, my IK target, but when running, jumping, or falling with my character I just fire wildly because the default animations play out without having higher track animations take over.

Could you explain in more detail what you mean by "I just fire wildly"?

In general please post code as text instead of images. You can use triple-backticks before and after code sections to format it properly:

``` 

or surround it with code tags:

[code]code goes here

[/code]


Regarding your code, there is nothing obviously wrong with the section shown.

Oh, sorry about that! I will post code correctly in the future.

By "firing wildly," I mean to say that the projectiles I am spawning are going wherever the normal run/fall/jump animation happen to place the firing arm. The IK never seems to take over and have the character aiming while doing those animations.

Hmm if the code isn't an issue I'm at a loss...I feel like I've checked and rechecked the spine project as well to make sure the IK is on for the proper animations (aim/firing).

ultrafop ha scritto

Oh, sorry about that! I will post code correctly in the future.

No worries! 🙂

ultrafop ha scritto

By "firing wildly," I mean to say that the projectiles I am spawning are going wherever the normal run/fall/jump animation happen to place the firing arm. The IK never seems to take over and have the character aiming while doing those animations.

Thanks for the clarification. So the animation looks correct, but only the spawned projectiles are using the wrong location?

If so, then it might be the problem that due to script execution order your projectile spawning script is called to early, either before your aiming script is placing the IK target bone or before your animation has been adjusted to the IK target. You should see that by setting breakpoints in the respective locations or adding debug log statements. You can also keep the code independent of script execution order by using the SkeletonAnimation update callbacks (see the spine-unity documentation section SkeletonAnimation Update Callbacks).

If the above does not resolve your issue, I'm afraid we will need more info regarding your code, since we have only seen one and a half method so far, but not from where these methods are called.

Happy Thanksgiving!

I apologize. I think I accidentally created red herring with the content about the projectiles. They are working/spawning as they should but because the IK doesn't seem to adjust they are following the default animations on track 0 (think spineboy shooting during his run animation if the mouse aim suddenly didn't work). I have my IK target mapped to a gamepad joystick and that is also working as expected.

I appreciate your help! I feel excited to look over and implement the animation callbacks you linked! I'll give it a try this weekend and report back ?

Thanks for getting back to us, glad you've figured it out, and happy thanksgiving! 🙂

I haven't got it figured out just yet but I will be trying that callback technique soon and checking back in ?

Oh, I misunderstood you there, thanks for clarifying. 🙂

No worries! I appreciate the help!

I really appreciate your time. I know how valuable it is.

I was looking through the callback information and unfortunately I'm confused about how to implement it after looking through the documentation and spineboy example project. I'm also a bit worried that by the time I understand and implement this animation callback system I'm also going to be having the same problem I am now. I say this because the current script I'm using is able to use the firing animation I made on on track 1 (all base animations like running, jumping, standing, and falling are track 0) when the X button is pressed, regardless of animations on track 0, it just never seems to update the IK strength like it's supposed to for the player's firing arm (I can see my arms twitching to fire, they just aren't point toward the IK target). Anything on Track 0 appears to be overriding the IK strength on track 1. I'll paste my full current code to see whether something is sticking out as an issue below.

If there's really nothing visibly wrong here and nothing else to try then I want to sincerely apologize in advance for the amount of posting I'm about to do to figure this callback stuff out 😃...

public class PlayerAnimScript : MonoBehaviour
{
Spine.TrackEntry setAnim; 
SkeletonAnimation anim;
Spine.AnimationState animState;
Spine.Skeleton skel; 
public Renderer rend; 
public Dynamic_Player_Script player; 
public string curAnimation;
public string debugShoot;


// Start is called before the first frame update
void Awake()
{
   anim = GetComponent<SkeletonAnimation>();
   rend = GetComponent<Renderer>(); 
   skel = anim.Skeleton;
   animState = anim.AnimationState;
   debugShoot = player.xButton;
}

// Update is called once per frame
void Update()
{
  
 checkifMoving();
 checkifRight();
 checkifFiring();

 if(player.isInPlay)
 rend.enabled = true;
 else rend.enabled = false;

}

//flip the animation depending on which direction the player is facing 
    void checkifRight(){
        if(!player.isRight)
        skel.ScaleX = -1; 

    else
    skel.ScaleX = 1; 
}

void checkifMoving(){

    //set animation state
    if(player.inputDirection != 0 && player.ground && curAnimation != "running"){
        setAnim = anim.AnimationState.SetAnimation( 0, "running", true);
        setAnim.MixDuration = 0f; 
        curAnimation = "running";}

    if(player.inputDirection == 0 && player.ground && curAnimation != "standing" ){
        setAnim = anim.AnimationState.SetAnimation(0, "standing", true);
        setAnim.MixDuration = 0f; 
        curAnimation = "standing";}
  

    if(player.verticalVelocity > 0 && !player.ground && curAnimation != "floor_jump"){
        setAnim = anim.AnimationState.SetAnimation(0, "floor_jump", false);
        setAnim.MixDuration = 0f; 
        curAnimation = "floor_jump";
    }

    if(player.verticalVelocity < -1 && !player.ground && curAnimation != "falling"){
        setAnim = anim.AnimationState.SetAnimation(0, "falling", false);
        curAnimation = "falling";
    }

    //set animation speed
    if(curAnimation == "running"){
    setAnim.TimeScale = Mathf.Clamp(Mathf.Abs(player.Horizontal),0.5f,1); 

    if(Input.GetButtonDown(player.rightBump) && player.playerGrounded()) {
    setAnim = anim.AnimationState.SetAnimation(0, "dash", false); 
    setAnim.MixDuration = 0f; 
  
    }

}
    
}

void checkifFiring(){
  if(Input.GetButton(player.xButton)){
    setAnim = anim.AnimationState.SetAnimation(1, "shoot", false);
    setAnim.MixDuration = 0f; 
    anim.state.AddEmptyAnimation(1, 0.5f, 0.1f);

    setAnim = anim.AnimationState.SetAnimation(2, "aim", true);
    setAnim.MixDuration = 0f; 
    anim.state.AddEmptyAnimation(2, 0.5f, 0.1f);
   
    
  }

}
}

I got it!

Sorry for the double post. The issue was that I was mistaken about how to set the IK in the Spine project, itself. I thought the IK had to be set to the intended weight at the start of each animation, when I just needed to set it at the start of animations that actually use the IK. Everything seems to be working correctly now. Thanks again for the help!

Very glad that you've figured it out, thanks for getting back to us! 🙂