Animation Rigging Problems (MultiAim Constraint with weapon)

Hello everone,

i am still quite new, so maybe one of you can help me.
What i want to achieve: In a RTS game, i want a character to point the gun directly at an enemy, while all animations run normally (besides the aiming). I have a character and walk/run/aim animations from mixamo.com.

I have set an AimTarget gameobject (which is the center of the spider) and constrained the weapon to it.
As shown in the screenshot, the red line would be my goal, but the blue line shows where the rifle is pointing instead. It’s always a few degrees off the target (and rolled sideways).
Is the pivot point of the weapon wrong? Or any ideas? I attached three screenshots, also of the contraint and the hierarchy.

Thanks!!!


So its been a min. since I did this, but my recollection is that you have to establish the aim constraint on instantiation or it has some wonky behavior. So you want to have the aim object in the character rig and the aim constraint setup but inactive, then on aim you want to activate the constraint while lerping the aiming object to the target.

This is the script that I wrote:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;

public class ActivateConstraint : MonoBehaviour
{

    public Transform parentTarget;
    public Transform positionTarget;
    public Transform aimTarget;
    public ParentConstraint parentConstraint;
    public PositionConstraint positionConstraint;
    public AimConstraint aimConstraint;
    public ConstraintSource parentConstraintSource;
    public ConstraintSource positionConstraintSource;
    public ConstraintSource aimConstraintSource;

        // Start is called before the first frame update
    void Start()
    {
 
        parentConstraint = gameObject.GetComponent<ParentConstraint>();
        positionConstraint = gameObject.GetComponent<PositionConstraint>();
        aimConstraint = gameObject.GetComponent<AimConstraint>();


        if (parentConstraint != null)
        {
            parentConstraintSource.weight = 1f;
            parentConstraint.constraintActive = true;
            parentConstraint.weight = 1f;
        }
       

        if (positionConstraint != null)
        {
            positionConstraintSource.weight = 1f;
            positionConstraint.constraintActive = true;
            positionConstraint.weight = 1f;
        }
       

        if (aimConstraint != null)
        {
            aimConstraintSource.weight = 1f;
            aimConstraint.constraintActive = true;
            aimConstraint.weight = 1f;

        }
       

    }


}

Thanks for your replay SethMeshko, but unfortunately the gun is still aiming a little off. Doesn it have to do with the parent Gameobject, the pivot/attach points, …?

Can you make a video walking us through the setup for the gun? There are a lot of different ways that this could be handled, to help I need to know how you are currently doing it.

Will try. I have no screen capturing software, but i will google & install one!

cx28b4

I tried to make a video with all the relevant parts. Does that help you?

I doubt this will solve your problem, but I noticed you’ve set the Up Axis on the Multi-Aim Constraint to X. Judging from the gun model, shouldn’t it be Y? Not sure though.
Personally, I’d try isolating the gun from the character. If it works fine in that case, maybe there is some other Animation Clip or something influencing the gun?

Hmm, i found out, that if i replace the animation controller with an empty one it seems to work (the character falls somehow half through the terrain and is stuck with the hips in the ground). Is it possible, that because i have two animation layers (base layer contains rifle idle animation and second layer contains aim animation for upper body mask) it is not working? (see Screenshot) Thanks!

Is the weight of the IK layer set to 1? If it’s less than 1 that might explain that offset you’re seeing.
So long as you’re not orienting the upper body towards the target with IK as well, I don’t think that should influence where the gun is pointed.

I think that’s just the default pose for Humanoid models when they don’t have any animation applied to them.

The handgrip IK was set to 0 (so it doesn’t influence the aim) and the animation layer was set to 1. This animation rigging is really driving me crazy… Maybe it’s because of the Mixamo.com character and animations. Is there a different source where i can get free soldier characters and animations?

One more thing you could try: If I understand your use case correctly, I don’t think it’s necessary to aim the gun using Animation Rigging. I think Transform.LookAt should be enough for what you’re trying to achieve. Your guy still won’t be facing the right direction but I guess that’s a separate problem.

Besides that, I can’t really think of anything else at the moment, sorry :confused: I’m pretty new to animation stuff myself.
The Unity Asset Store has a couple of free soldier models, but they’re low poly.

Thanks a lot for your effort, bjnklcn! Good point with the alternative to the aim constraint, i will give it a try! Humanoid animation is a nightmare for beginners, i have to say (foot sliding, syncing navmesh agent with the animations, etc.). I also read somewhere that the order of the constraint rigs is important as well, perhaps i will give it a last try.

You’re welcome! I agree, dealing with animation is quite a bit more involved than I had anticipated. It’s all just a matter of practice though :slight_smile: And yes, the order of Rigs and Constraints does matter, see here.
If you do figure out what the problem was, I’d be interested to know!

I somehow got it to work! But i have no clue what is different. I created everything from scratch again. The Rig setup was the same… so maybe something with the animation clips or the animator (?). But now i ran into another issue: not my character is hovering above the ground in playmode. All transforms are set to 0, except the root transform (hips); but even when i change it, it hovers again. In develop mode it stands perfectly on the ground. How can i place it there in playmode as well? And how are the feet kept on the ground later on, when i have hills in my terrain??


Oh, glad you got it to work! Sometimes it really is best to just start over. Been there, done that many times :wink:

I recently also had the problem that my character was floating a few inches off the ground. In my case it was the Character Controller that caused it. If the skin width is set too high, it can cause the character to hover. (I didn’t want to reduce the skin width, because that can cause other problems, so I just added an offset to the Y coordinate.)
I don’t know if you use a Character Controller, but I think you have a Nav Mesh Agent, right? That seems to be another common cause for that sort of problem. Maybe this post can help?

Yes, i have a navmeshagent. Strange, i only reimportet the animations, did the rigging, and now the character is floating… If i add a y-offset, then the character sinks in the ground in develop mode, and in game mode floats anyways. But i will read the other post!

I haven’t really worked with NavMeshAgent yet, so I’m not sure how valid any of this is. I just happend to come across this when I was dealing with the hovering problem myself. Either way, it seems like you might want to set the base offset: character floats above ground when in game mode

That worked, thanks a lot!

1 Like