AeleaS Omer (aeleas)

11 replies · posted

Character aiming on wrong direction ?

Hi

I'm trying to make my character aiming auto at target using IK. But the result was not that good at all. What ever I do to fix the character armies I fail. Character armies pointing far from target. Here is my script.

Script link

Could anyone please tell me what is wrong ?

  • crew

    The script looks fine to me. Do you have a video or something showing how they are aiming? The image you posted looks like it's aiming correctly so I'm not really sure what you're having issues with. 

    • jgonzalez , Thank you for your reply.

      Here is my scene file 26mb. Please look at it, and tell me where is the problem. 

      https://www.sendspace.com/file/f6vvt3

      If you moved target, you will see that character armies pointing at wrong direction !!

      I don't know why and I couldn't solve it.

    • crew

      aaeleas It was working fine for me, he points in the right direction. Your Ik script just includes the look at portion, which means his upper body will point in the direction of the target and follow it's position/rotation. The only time he didn't look at the target was when it was out of range, usually when it (the target) was moved behind him or off to the side too far out of sight.

      The only thing I can think of that might be slightly off is that he's aiming a little higher than the target. I have an IK script where I have that tweaked a bit further. Since it's a pain to add the entire script here, I'll post the relevant portion:

      target = ikReference.position;
      target.y = target.y + aimAdjustment;
      animator.SetLookAtPosition (target);
      animator.SetLookAtWeight (aimWeight, bodyWeight, headWeight, eyesWeight, clampWeight);


      the portion "target.y = target.y + aimAdjustment" allows me to add or subtract to ensure the character is aiming nearly dead on with the target. If you also wanted to control the arms through IK you would have to add in separate targets for each hand (wrist) and it would look like this in script:


      if (leftHandle != null) {
      animator.SetIKPosition (AvatarIKGoal.LeftHand, leftHandle.transform.position);
      animator.SetIKRotation (AvatarIKGoal.LeftHand, leftHandle.transform.rotation);
      animator.SetIKPositionWeight (AvatarIKGoal.LeftHand, aimWeight);
      animator.SetIKRotationWeight (AvatarIKGoal.LeftHand, aimWeight);
      }
      
      if (rightHandle != null) {
      animator.SetIKPosition (AvatarIKGoal.RightHand, rightHandle.transform.position);
      animator.SetIKRotation (AvatarIKGoal.RightHand, rightHandle.transform.rotation);
      animator.SetIKPositionWeight (AvatarIKGoal.RightHand, aimWeight);
      animator.SetIKRotationWeight (AvatarIKGoal.RightHand, aimWeight);
      }
      }



      Hopefully that code is fairly self explanatory. I have a transform for each of the hands. Using my script results in this: 

      with adjustment added in you can see he aims much closer to where the target actually is. Below is the default without any kind of aim adjustment:

      If he was holding a rifle he'd be aiming slightly above the sphere. If there's another issue let me know, but that's all I noticed. 

    • jgonzalez , thank you for what you did for me.

      Both scripts have no issue except first line:

      "target = ikReference.position;"

      what is "ikReference" ?

      I read it as Vector3 and wrote this script.

      https://pastebin.com/exeLA43m

      Could you please upload the scene to me to see your work ? Its every important to solve this issue for me.


    • crew

      aaeleas IK Reference would be the Transform object you want your character to follow. In your script you named it "target" but I had a separate variable called target. It should be a Vector3 so your script should be identical in how it's used. I'll try to get the script posted here later today. 

    • jgonzalez, very good. but please upload the file also. I need it . Please.

  • crew

    So there's a weird bug in the community posts when posting large amounts of text so I just uploaded it to Pastebin here: https://pastebin.com/W5caSVW4