Search Unity

SetIKPosition: shooting

Discussion in 'Animation' started by leizzer, May 5, 2014.

  1. leizzer

    leizzer

    Joined:
    Jun 26, 2013
    Posts:
    39
    Hello.

    I'm using mecanim in my project.

    The character moves correctly and when the fire button is pressed I use SetIKPosition to raise the hand toward the mouse cursor.

    My problem is that it starts shooting before the arm reach the correct position.

    This is a part form my script attached to my character:

    Code (csharp):
    1.  
    2.  
    3. private var cursor : Vector3;
    4. internal var animator : Animator;
    5.  
    6. function Start () {
    7.     animator = GetComponent(Animator);
    8. }
    9.  
    10. function Update () {
    11.    
    12.     // Look at
    13.     if (Input.GetButton('Fire1')){
    14.         animator.SetLookAtPosition(cursor);
    15.         animator.SetLookAtWeight(lookWeight, 0.5f, 1.0f, 1.0f, 1.0f);
    16.     }
    17.    
    18.         // Shoots the current gun
    19.     GunHandling();
    20. }
    21.  
    22. function OnAnimatorIK(layerIndex: int) {
    23.        // Point the hand and arm toward the cursor point
    24.        if (Input.GetButton('Fire1')){
    25.         animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
    26.         animator.SetIKPosition(AvatarIKGoal.RightHand, cursor);                              
    27.        }
    28. }
    29.  
    30. function FixedUpdate(){
    31.     // Update the cursors position
    32.     var playerPlane = new Plane(Vector3.right, transform.position);
    33.  
    34.     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    35.  
    36.     var hitdist = 0.0;
    37.  
    38.     if (playerPlane.Raycast (ray, hitdist)) {
    39.         cursor = ray.GetPoint(hitdist);
    40.     }
    41. }
    42.  
    43. function GunHandling(){
    44.     if (Input.GetButton("Fire1")){
    45.         StartCoroutine(currentGun.Shoot());
    46.     }
    47. }
    48.  
    49.  
    For example: the character is in idle position (arms rest), then I hit the fire button and shoots while the arm is moving to the goal position in SetIKPosition (this is te problem). So it ends up with the first shoot coming from the hips (where the hand was then the arms was resting).

    I tried to move the GunHandling inside OnAnimatorIK but the thing becomes worst.

    Can anyone give me a hint?
    Thank you very much.
     
    Last edited: May 16, 2014
  2. leizzer

    leizzer

    Joined:
    Jun 26, 2013
    Posts:
    39
    I added a flag inside OnAnimatorIK and while firing in GunHandling.

    Code (csharp):
    1.  
    2.  
    3. private var onPosition : boolean = false;
    4.  
    5. function OnAnimatorIK(layerIndex: int) {
    6.  
    7.        // Point the hand and arm toward the cursor point
    8.  
    9.        if (Input.GetButton('Fire1')){
    10.            animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
    11.            animator.SetIKPosition(AvatarIKGoal.RightHand, cursor);
    12.                          
    13.            onPosition = true;
    14.        }else{
    15.            onPosition = false;
    16.        }
    17.  
    18. }
    19.  
    20.  
    21. function GunHandling(){
    22.  
    23.     if (Input.GetButton("Fire1")  onPosition){
    24.  
    25.         StartCoroutine(currentGun.Shoot());
    26.  
    27.     }
    28.  
    29. }
    30.