Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

2D Animation Move Bones Through Script

Discussion in 'Animation' started by Adjaar7, May 4, 2021 at 10:53 AM.

  1. Adjaar7

    Adjaar7

    Joined:
    Jan 6, 2020
    Posts:
    22
    Hi there, I am completely stuck! I have a 2D platformer with a player holding a gun that follows the mouse. This works fine, see Imgur link below. The arms are attached to the gun through IK, but not other body parts use this feature, the rest are simple bones rigging and animated through tweening. For a cleaner and more realistic effect, I want the spine to also aim with the arms, but clamped to max and min values.

    How I have figured I would achieve this is by using the same aiming code as the gun (tutorial from CodeMonkey), but using Mathf.Clamp in order to prevent the spine from having an unnatural range.

    Here is the basic code I have for the spine movement, the numbers on the clamp are more for testing than final numbers.
    Code (CSharp):
    1.     private void LateUpdate()
    2.     {
    3.         Vector3 spineMousePosition = UtilsClass.GetMouseWorldPosition();
    4.         Vector3 spineAimDirection = (spineMousePosition - transform.position).normalized;
    5.         float absoluteAngle = Mathf.Atan2(spineAimDirection.y, spineAimDirection.x) * Mathf.Rad2Deg;
    6.         spineTransform.rotation = Quaternion.Euler(0, 0, Mathf.Clamp(absoluteAngle, 0, 170));
    7.         animator.SetBoneLocalRotation(HumanBodyBones.Spine, spineTransform.localRotation);
    8.     }
    If you check out the second video from the Imgur link at the bottom, you'll notice that this code works....kind of. The bones are technically rotating, but the skin is not following. If I use the animation tab I can animate no problem, and I can grab the spine bone and manually rotate it fine, but the method of rotation through script has caused this issue.

    Is there a simple solution to this? Can I solve this with animator.SetBoneLocalRotation or should I go an entirely different route. Any help, advice or theories are very welcome and appreciated. Thanks!

    Both videos are at this link.
    https://imgur.com/a/DJNZuk5
     
  2. KalOBrien

    KalOBrien

    Unity Technologies

    Joined:
    Apr 20, 2021
    Posts:
    20
    I think its how youve setup your bone hierarchy.

    I would use your HIP spine bone as the base but attach the arms, to the hip area, and then the top spine bone+head bone to the hip separately. At the moment it looks like the code for aiming is conflicting with your new rotation.

    Regarding the mesh not following the bone, go check your bone weights and see if the sprite actually has the spine bone assigned to it.
     
  3. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    383
    Hello @Adjaar7,
    I'm not sure which video you are referring to, but do note that the 2D IK system and the 3D IK system are not the same system. To work with the IK system in 2D, please refer to our 2D IK manual, here. A few years ago we also published a blog post with the focus of the 2D IK system, you can find it here.

    After going through these sources, do let us know about your result and if you have any further questions.
     
  4. Adjaar7

    Adjaar7

    Joined:
    Jan 6, 2020
    Posts:
    22
    The arms are done with 2D IK, and they work perfectly. The spine doesn't have IK, I don't think this situation calls for it. I'm trying to fix the spine so I don't think it is an IK issue.
     
  5. Adjaar7

    Adjaar7

    Joined:
    Jan 6, 2020
    Posts:
    22
    If the arms are attached to the hip, won't it look funny when they aren't attached to the spine (once I get the spine moving of course)?

    The spine bone works. I can grab it and reposition it and the whole sprite moves. I can use keyframes to animate this way, however I'm trying to with script. This wonky bone moving but not the sprite thing is happening with script. The spine does not use IK if that helps.
     
unityunity