Search Unity

Trouble using an animation C# job to aim the weapon (pitch only)

Discussion in 'Animation' started by jehovah0121qq, Jan 29, 2019.

  1. jehovah0121qq

    jehovah0121qq

    Joined:
    Nov 14, 2013
    Posts:
    68
    Unity 2018.3.0f2

    I'm using PlayableGraph to control the animation of my character. I have a basic clip for the character holding a gun aiming. I want to use the pitch angle to correct the direction of the gun. In the following Job, I rotate the shoulder to aim the gun correctly, but what I see on the screen is the shoulder jumping forever between two rotations.

    Code (CSharp):
    1. public struct AnimJob_WeaponLookAt : IAnimationJob
    2.     {
    3.         // Pitch angle.
    4.         public float Pitch;
    5.  
    6.         // The shoulder whose hand holds the weapon.
    7.         public TransformStreamHandle HoldingShoulder;
    8.  
    9.         // Root node of the weapon.
    10.         public TransformSceneHandle WeaponTransform;
    11.  
    12.         // Root node of the character.
    13.         public TransformSceneHandle RootTransform;
    14.  
    15.         public void ProcessAnimation(AnimationStream stream)
    16.         {
    17.             var input = stream.GetInputStream(0);
    18.             var pitchRot = Quaternion.Euler(Pitch, 0, 0);
    19.             var weaponSrcRot = WeaponTransform.GetRotation(input);
    20.             var weaponFromDir = weaponSrcRot * Vector3.forward;
    21.             var weaponToDir = pitchRot * RootTransform.GetRotation(input) * Vector3.forward;
    22.             var shoulderOriginalRot = HoldingShoulder.GetRotation(input);
    23.             var shoulderRotDelta = Quaternion.FromToRotation(weaponFromDir, weaponToDir);
    24.             HoldingShoulder.SetRotation(stream, shoulderRotDelta * shoulderOriginalRot);
    25.         }
    26.  
    27.         public void ProcessRootMotion(AnimationStream stream)
    28.         {
    29.             // Empty.
    30.         }
    31.     }
    Anyone could give me some advice, please? Thanks.

    Notes:
    - The gun has no animation. It's just a prefab attached to a point at the character's hand.
    - The character is humanoid.