Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. 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:
    6
    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:
    5,181
    I think Sharp Accent on youtube has some tutorials that cover this.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,323
    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:
    3,793
    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.
     
    KirinDr likes this.
  5. adhith

    adhith

    Joined:
    Sep 20, 2017
    Posts:
    6
    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:
    13,323
    Run a raycast from camera, and aim at point it hits.
     
  7. Rasly233

    Rasly233

    Joined:
    Feb 19, 2015
    Posts:
    264
    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.
     
  8. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    You can use the inbuilt armIK as it now has elbow hints to stabilize the limb. I recently put BioIK thru the ringer as the base for driving 5 animatonic puppets for a stage show. Final IK proved unstable for that.
     
    MadeFromPolygons likes this.
  9. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,877
    Was BioIK decent based on your usage, or now after having used it would you also again look at something else?
     
  10. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    BioIK worked great because I could isolate the Z axis and get an offset set prior to any sim regardless of the underlying targeted rigs bone axis. This allowed me to extract a ratchet position for the servo step motors for creating and sending byte packets without much further processing of the signal. BioIK is a robotics IK and uses genetic swarms extinguishing unlikely limb IK angles and promoting higher probability goal angles to the next generation. Worked better than any of the Final IK algorithms.
     
    MadeFromPolygons likes this.