Search Unity

Question Animation Rigging Problems (MultiAim Constraint with weapon)

Discussion in 'Animation' started by Maria_Tonia, Mar 23, 2022.

  1. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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!!!

    weapon gameobject.JPG
     

    Attached Files:

  2. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    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.
     
    Last edited: Mar 23, 2022
  3. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    This is the script that I wrote:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Animations;
    5.  
    6. public class ActivateConstraint : MonoBehaviour
    7. {
    8.  
    9.     public Transform parentTarget;
    10.     public Transform positionTarget;
    11.     public Transform aimTarget;
    12.     public ParentConstraint parentConstraint;
    13.     public PositionConstraint positionConstraint;
    14.     public AimConstraint aimConstraint;
    15.     public ConstraintSource parentConstraintSource;
    16.     public ConstraintSource positionConstraintSource;
    17.     public ConstraintSource aimConstraintSource;
    18.  
    19.         // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.  
    23.         parentConstraint = gameObject.GetComponent<ParentConstraint>();
    24.         positionConstraint = gameObject.GetComponent<PositionConstraint>();
    25.         aimConstraint = gameObject.GetComponent<AimConstraint>();
    26.  
    27.  
    28.         if (parentConstraint != null)
    29.         {
    30.             parentConstraintSource.weight = 1f;
    31.             parentConstraint.constraintActive = true;
    32.             parentConstraint.weight = 1f;
    33.         }
    34.        
    35.  
    36.         if (positionConstraint != null)
    37.         {
    38.             positionConstraintSource.weight = 1f;
    39.             positionConstraint.constraintActive = true;
    40.             positionConstraint.weight = 1f;
    41.         }
    42.        
    43.  
    44.         if (aimConstraint != null)
    45.         {
    46.             aimConstraintSource.weight = 1f;
    47.             aimConstraint.constraintActive = true;
    48.             aimConstraint.weight = 1f;
    49.  
    50.         }
    51.        
    52.  
    53.     }
    54.  
    55.  
    56. }
    57.  
     
  4. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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, ....?
    upload_2022-3-26_22-22-23.png
     
  5. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    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.
     
  6. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    Will try. I have no screen capturing software, but i will google & install one!
     
  7. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
  8. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    I tried to make a video with all the relevant parts. Does that help you?
     
  9. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
    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?
     
  10. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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!
     

    Attached Files:

  11. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
    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.
     
  12. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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?
     

    Attached Files:

    • fun.JPG
      fun.JPG
      File size:
      57.5 KB
      Views:
      222
  13. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
    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 :/ I'm pretty new to animation stuff myself.
    The Unity Asset Store has a couple of free soldier models, but they're low poly.
     
  14. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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.
     
  15. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
    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 :) 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!
     
  16. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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??
     

    Attached Files:

  17. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
    Oh, glad you got it to work! Sometimes it really is best to just start over. Been there, done that many times ;)

    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?
     
  18. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    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!
     
  19. bjnklcn

    bjnklcn

    Joined:
    Jan 12, 2019
    Posts:
    6
  20. Maria_Tonia

    Maria_Tonia

    Joined:
    Feb 28, 2019
    Posts:
    36
    That worked, thanks a lot!
     
    bjnklcn likes this.