AIプログラムとかUnityゲーム開発について

探索や学習などを活用したAI系ゲームを作りたいと思います。

武器によって手の位置をIKで微調整

f:id:yasu9780:20161117192522p:plain

 PMCAで女子キャラを増やしました。メガネのおばさんキャラも作ったので、お店のNPC店員にしようと思います。 
 右手に持たせる武器の塊位置がPMCAキャラで共通化できるので、キャラを増やした場合の作業が少なくて楽です。

 使用するアニメーションはピストルを構えながら歩くモーションですが、両手で銃を持つような位置に両手が来ます。
 ただし、PMCAキャラでは右手の位置が左に行きすぎて両手がクロスするような位置になってしまっていました。

 humanoidのモデルによってアニメーション時の右手や左手の位置が微妙に違うので、それを微調整します(マッスルなどの影響?)
 このへんを調整しつつ、ロケットランチャーは重心的に右手を手前に引きたいですし、マシンガンやライフルは左手を前にだして銃身を支えたい。
 アニメーションステートが狙い移動の場合に、銃によって右手・左手の位置をIKで調整します。

    void OnAnimatorIK()
    {
        if ( ( charaNo==7 || charaNo==1 ) // PMCA & Alicia
            && anim.GetCurrentAnimatorStateInfo(0).IsName("AimWalk"))
        {
            if (weapon >= 4 && weapon <= 5)//smg
            {
                SetIk(AvatarIKGoal.RightHand, Vector3.right * 0.03f, Quaternion.Euler(Vector3.zero));
                SetIk(AvatarIKGoal.LeftHand, Vector3.right * 0.05f + Vector3.forward * 0.05f+Vector3.up*0.05f, Quaternion.Euler(Vector3.zero));
            }
            if (weapon == 7 || weapon == 3)//pistol
            {
                SetIk(AvatarIKGoal.RightHand, Vector3.right * 0.03f+Vector3.down*0.08f, Quaternion.Euler(Vector3.zero));
                SetIk(AvatarIKGoal.LeftHand, Vector3.right * 0.06f+Vector3.down*0.1f, Quaternion.Euler(Vector3.zero));
            }
            if (weapon == 6) // rocketLauncher
            {
                SetIk(AvatarIKGoal.LeftHand, -Vector3.right * 0.1f + Vector3.forward * 0.05f+Vector3.down*0.1f, Quaternion.Euler(Vector3.zero));
                SetIk(AvatarIKGoal.RightHand, -Vector3.forward * 0.2f+Vector3.down*0.2f, Quaternion.Euler(Vector3.zero));
            }
        }
    }

    void SetIk(AvatarIKGoal goal, Vector3 pos, Quaternion rot)
    {
        anim.SetIKPosition(goal, anim.GetIKPosition(goal) + pos);
        anim.SetIKPositionWeight(goal, 1f);
        //anim.SetIKRotation(goal, rot);
        //anim.SetIKRotationWeight(goal, 1f);
    }