Search Unity

Question FPS Microgame Gun Holding Issue

Discussion in 'Animation Rigging' started by PlayTenc, Sep 7, 2023.

  1. PlayTenc

    PlayTenc

    Joined:
    Feb 8, 2022
    Posts:
    1
    Im using Unity FPS Microgame and I have issue with Character weapon holding.

    I created Rig and Two Bone IK Constraint for Both hands. I set Root, Mid, Tip and Target im setting via script.


    Code (CSharp):
    1. public void UpdateIKTargets(Transform leftHandTarget, Transform rightHandTarget)
    2.         {
    3.             // Set the left and right hand IK targets based on the active weapon's targets.
    4.             if (GetActiveWeapon() != null)
    5.             {
    6.                 leftHandTarget = GetActiveWeapon().LeftHandIKTarget;
    7.                 rightHandTarget = GetActiveWeapon().RightHandIKTarget;
    8.             }
    9.  
    10.             // Set the left and right hand IK targets for the constraints.
    11.             LeftHandIK.data.target = leftHandTarget;
    12.             RightHandIK.data.target = rightHandTarget;
    13.             LeftHandIK.weight = 1;
    14.             RightHandIK.weight = 1;
    15.             RigBuilder.Build();
    16.         }
    In every gun i created game objects for Left and Right gun and if i hit play i see that my script set Target to gun.

    upload_2023-9-7_11-58-40.png

    But the problem is that Character is not holding weapon

    upload_2023-9-7_12-0-35.png

    I assume problem is that gun adds under Main Camera and not Character Root (Right hand for e.g.)

    upload_2023-9-7_12-2-22.png


    What i tried:
    1. Place FirstPersonSocket or WeaponSocket under Root Hands (Then i face issue with camera controll or weapon controll)
    2. Update hands position with gun (its not only that code):

    Code (CSharp):
    1.  
    2. // Set only the y value, keep x and z as 0
    3.                 Vector3 newLeftPosition = new Vector3(0f, m_InputHandler.GetLookInputsHorizontal() * m_PlayerCharacterController.RotationSpeed * 0.4f, 0f);
    4.                 Vector3 newRightPosition = new Vector3(0f, m_InputHandler.GetLookInputsHorizontal() * m_PlayerCharacterController.RotationSpeed * 0.4f, 0f);
    5.                 // Apply the offset to leftHandTarget and rightHandTarget
    6.                 GetActiveWeapon().LeftHandIKTarget.localPosition = newLeftPosition;
    7.                 GetActiveWeapon().RightHandIKTarget.localPosition = newRightPosition;
    But the problem lays in WeaponParen local position cuz it set one time and rest of the game everysingle game object position will be same.
    Code (CSharp):
    1.  
    2. void LateUpdate()
    3.         {
    4.  
    5.  WeaponParentSocket.localPosition = m_WeaponMainLocalPosition + m_WeaponBobLocalPosition + m_WeaponRecoilLocalPosition;
    6. }
    7.  
    Maybe someone solved this problem in FPS microgame?