Search Unity

make a rigged character gun point to a target without ik

Discussion in 'Animation' started by Al-ras, Jan 7, 2014.

  1. Al-ras

    Al-ras

    Joined:
    Oct 19, 2012
    Posts:
    3
    Hi,

    I'm trying to make a tactical turn based game, like Silent Storm and Jagged Aliance2. The thing is going forward nicely. But, i'm constructing the accuracy and shooting system as follows: I've the Mixamo Swat soldier rigged and animated. Because i dont know how to animate characters or anything about ik, i make the soldier point to a target disabling de Animator component, and rotating the spine by code. Prehistoric way , but a logic one. The problem is that i select the Vector3 i'm targeting, pointing it with the mouse cursor. When the point is selected, the current soldier will turn on th Y axis to face them Okay! the soldier is looking at the target, but the gun is in the top right corner of the soldier, so it never aim that pointt with any precission. I tried rotating the spine, and at long range it is close to accurate. But obviously this is not the way, because the spine forward direction isn't the same of the character, nor the gun! I need to rotate the player, and then the spine to SOME Quaternion that makes the gun point to the target. The gun isn't facing froward, so i've to rotate things around to Get some decent pointing direction. Right now, i can play the game nicely from a distance of 5 units aprox, but if i get closer, the spine of the character rotates up or down in excess, obviously because the calculations i made are wrong. I will not put the code here, because i need to redo everything from scratch so it is wrong. I need some advice to get a good approuch to make this.

    I dont know if this is barely explicatory

    Sorry for my English And Thanks
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    You're basically writing your own IK. You might find it easier to read up on Mecanim IK so you can keep Animator enabled, assuming you have Pro.

    Another option is to use animation blending instead of IK. Since you're using Mixamo, you could get four variations of the aiming animation:
    1. Aiming to the extreme upper left
    2. ...upper right
    3. ...lower left
    4. ...lower right
    And create a 2D blend tree that blends on the horizontal vertical offsets from center.
     
  3. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Thanks TonyLi,

    Unfortunatly i have Unity Free, so i can't use ik inside mecanim. But the Blending tree may be a nice solution, if i get the way to set the exact offset to get the gun direction point to the right position. And at close range will be a rotation limitation in the character, but this could be solved by not allowing the player to shot in the face of the enemies and only punch it or something else.

    I will try this!
     
  4. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,552
    Mecanim IK won't do the trick. SetLookAtPosition would only solve accurately in close range if the gun was positioned exactly at the position of the last bone in the spine which it practically never is...
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    While it wouldn't look nearly as good as Partel Lang's Final IK (which looks like it will be excellent) or the blend tree, if you had Pro you could position the gun perfectly and make the hands follow it using Mecanim IK, and also use SetLookAtPosition to position the spine and head. It's a moot point, though, since you're not using Pro. And, anyway, the blend tree is probably your best option.
     
  6. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,552
    There is something you can do without IK or blend trees though if you have a 1-handed weapon, rotate the spine like you are doing it now, but after that also rotate the upper arm of the gun hand to aim at the target..

    Code (csharp):
    1.  
    2. upperArm.transform.rotation = Quaternion.FromToRotation(gun.forward, aimDirection) * upperArm.transform.rotation;
    3.  
     
  7. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Thanks guys!

    Right now, i've an animation pack for soldier action by mixamo, so there are only a few animations to play with. Taking the code of Partel Lang, i'll have a problem because the pistol animations are two handed and the rifle ones are the same thing. So i've the z axis of the character object rotated to the target, but the problem is that because of the bone configuration in the animation itself, the spine is also rotated slightly to the right and down, the shoulder arm and forearm bones, are also his own rotations,So there is no bones facing straight forward anyway. By code i think it will be very hard to calculate the combined rotation to make the right pointing angle.

    I dont know if you guys know the game Silent Storm(2003. is a mix of jagged alliance2 with WWII and 3d ;)). It is a turn based strategy game in 3d with a lot of guns breakable environment and physics(old ones but still there). My work until now(aiming and shooting system), is reasonably similar to the existing in the game(bullet penetration, body parts impacts, etc...), so i want to reach this functionality to make it as close as possible to the original... I'll post as soon as i can a few captures for you guys to see what i've done until now.

    The last option i will consider is make the gun parent of the character, so i rotate the gun to point correctly, and then recompose the character bones to still touching the floor and things like that. But i believe that it will bring me the same problems!

    ;) Cheers!


    OH, and i realize that i've two accounts, so i appear like Alras and Danirey. Sorry!
     
    Last edited: Jan 8, 2014
  8. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Here is a screenshot of the real Game!

    $546805-557924_20040126_002.jpg

    Here yo can see the position of the sniper!
     
    Last edited: Jan 8, 2014
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Nice looking game!

    I think the blend tree will give you the best-looking results since it uses real human mo-capped data instead of trying to compute it. Since you have the shooter animation pack, check to see if you can download variations on the aim animation. If you can't right off the bat, contact Mixamo. They're very helpful. Since you've already bought the base animation, they should be able to give you access to download variations.
     
  10. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Thanks man! I'll ask them. I'm thinking that the problem is that the animation doesn't fit the purpose for this particular mecanism. They will do very well in first person or third person shooters, but this is turn based, so i need a semi-still animation for aim etc... If the animation is correctly aligned, it will be easier to fix direction blending the aiming to corners animations. I will try that searching for a correct animation and experiment with this. Thanks
     
  11. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    And now we are working with the blending tree, have yo a quick point to start scripting and parse the aim to point at to a value usable with the blend tree in mecanim?

    Thanks again!
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Sorry, no starting script, but you should be able to just compute the angle differences between the character's direction and the target.
     
  13. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Ok! I'll find out!

    Thanks!
     
  14. Danirey

    Danirey

    Joined:
    Apr 3, 2013
    Posts:
    548
    Hi, Guys!


    I've been searching and learning about all this stuff and i think that i'm dead without even noticed that. All this calculations are too hard for me and i've not the knowledge to get deep in that. So i saw the Partel Lang ik solver on the asset store and i'think it will be less painful to pay for it than make it myself (obviously not an option at all). I've seen the aim video and it is exactly what i'm looking for. And all the other options that surely will enhance any game.


    So Partel, It is a complete solution you have there! Well done!

    Now i've a lot of visual EXTRAS that i want to include in my game: Aim of course. Open doors, crates, etc... One hand take the gun mag out inthe correct position, etc.... ;)

    Here is a first look of the game! (Sorry for the quality and shaking. A Long way to go, but i'll make it!)
     
    Last edited: Jan 17, 2014
  15. sas67uss

    sas67uss

    Joined:
    Feb 8, 2020
    Posts:
    81
    Hi FinalIK man
    can you please tell me according to your code, How multiplying world rotation quaternion and rotation of bones, make a currect rotation, like using IK ?? according to that coordinate axis of bones do not coincide with world coordinate axis?
    what is magic of this multiply?