Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Aim towards crosshair using IK

Discussion in 'General Discussion' started by adhith, Mar 22, 2020.

  1. adhith

    adhith

    Joined:
    Sep 20, 2017
    Posts:
    4
    am in a 3rd person shooter project i need to set the gun aim towards crosshair with IK anyone help me. i don't have any exp in animation stuff. please
     
  2. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    3,217
    I think Sharp Accent on youtube has some tutorials that cover this.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    7,119
    Google "gradient descent inverse kinetmatics" and maybe "itertive inverse kinematics".
    First hit:
    https://www.alanzucconi.com/2017/04/10/gradient-descent/

    Long story short, if you aren't REALLY familiar with matrix operations, vector algebra and trigonometry, I'm not sure if you'll finish it.

    The simplest approach is this:
    1. Define IK chain end and IK goal
    2. Calculate vector difference between chain end and goal.
    3. For each limb of ik chain, calculate ROUGHLY how much you need to move/turn this limb only for end node to reach goal. This is usually done by computing dot product between "difference between target and goal", and movement vector of this node, where movement vector is direction in which the target will move if you start moving this joint. For translation arms this easy, for rotational arms, it is tangent drawn from goal and end of the chain with joint rotational axis being used for reference.
    4. Do this for all joints in the chain and collect desired adjustments....
    5. Then multiply them by small value....
    6. And apply to IK arm.
    7. Repeat 3-6 many times.

    This is iterative inverse kinematics, and I believe it uses newtonian method to solve system of equation (because that's what IK is).

    Does this make sense to you?

    If not, either study more or seek an asset thaet will do the work for you.
     
    adhith likes this.
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,829
    I had an IK aiming system in my Procedural IK Combat and Nav system, Katagon and used the shoulder to wrist to target alignment, setting the wrist IK goal in line between the target and shoulder joint. I then adjusted the barrel of the gun using transform.LookAt(target). The Z axis of the weapon was straight down the barrel with the pivot at the natural rotation point of the weapon when handled in RL.There are several IK solutions on the Asset Store. Set up the arm IK chain, calculate the target vector from the shoulder joint using target.position - shoulderJoint.transform.position, then set the wrist joint forward of the shoulder joint along the target vector by some distance slightly less than the sum of the arm and forearm length. Try not to let the elbow joint get to 0 degrees as you may experience gimbal lock so do not place the wrist goal beyond the summed length of those two joints. This system will allow shooting in most any bio-dynamically feasible pose your character may take during game play.
     
  5. adhith

    adhith

    Joined:
    Sep 20, 2017
    Posts:
    4
    Some what i set the ik but i need that look towards crosshair how i can achieve that
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    7,119
    Run a raycast from camera, and aim at point it hits.
     
  7. Rasly2

    Rasly2

    Joined:
    Feb 19, 2015
    Posts:
    226
    Well you could be like me go buy FinalIK for 80 bucks, will polute your project with junk files, so not ideal, but it works, while we weait for unity kinematikwhatever to catch up.
     
unityunity