Search code examples
c#unity-game-enginegame-physicsgame-development

Unity Make Hands Move with weapon


I have a unity scene with a character holding a weapon. The weapon is a child of the camera and so moves with the camera allowing the player to aim the weapon. My issue is that the 3d model of the character has hands which i wish to be holding on to the weapon. Initially, to get past this issue, I used IK's to place the hands on the gun, this worked but didn't allow me to aim the gun as it would be fixed to the hands. How would I go about keeping the hands attached to the weapon whilst still being able to aim?

The player look script is attached for refrence:

public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;

public int Limits = 80;
public Transform playerBody;

float xRotation = 0f;

void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
    float mouseX = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, (Limits * -1), Limits);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
}

If anymore information is needed please let me know, thank you.


Solution

  • FIX:

    Parent gun to hand, use animation rigging IK's to follow a target (I used multi aim constraints), do this for both the hand and the spine. Set the source object as a sphere, either disable it from being visible in the camera or just make it very small, then make it a child of the camera, place it at X=0, Y=0 and Z=>0.