Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ruggia · Jul 22, 2020 at 03:28 PM · scripting problem3dfpsaimmodel importing

FPS - Attach a gun to hands and move arms when aiming up and down

I'm kinda new to Unity, and I'm trying to adapt a Pistol to this Mixamo Character with some Mixamo Animations. I want the pistol to move with the camera ( CSGO aim system ) and also I want the arms to move with the gun. I saw something about IK and various posts but can't find/apply the solution. Can someone help?

Mixamo Character: The Boss

Mixamo Animations: Pistol Idle ( the one that doesn't move ), Pistol Run ( The slower one )

Pistol : One of these https://assetstore.unity.com/packages/3d/props/guns/modern-guns-handgun-129821

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Weaver13 · Jul 23, 2020 at 05:37 AM

So there's a difference between IK and just general tracking. I think the shoulders will be doing most of the work so I will just focus on that problem. If you really want to mess with IK, there are a lot of posts here that do an effective simple version of that, and you will have to modify the code heavily for your specific Transforms. There is also an Asset called HeadLookController (look it up) which is a gem. That script contains this method which I use all the time:

 public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis) {
         // Project A and B onto the plane orthogonal target axis
         dirA = dirA - Vector3.Project(dirA, axis);
         dirB = dirB - Vector3.Project(dirB, axis);
  
         // Find (positive) angle between A and B
         float angle = Vector3.Angle(dirA, dirB);
  
         // Return angle multiplied with 1 or -1
         return angle * (Vector3.Dot(axis, Vector3.Cross(dirA, dirB)) < 0 ? -1 : 1);
     }

Just for rotating the arms up and down, assuming the X axis is the one pointing out and also assuming that the Y axis is the bone's pointing vector, you would just use it like this:

 Vector3 armPivot = arm.right; //may have to multiply by -1
 Vector3 armPointing = arm.up;
 Vector3 toDir = Camera.main.transform.forward;
 //Now we do from armPointing to toDir
 float amtToUp = AngleAroundAxis(armPointing, toDir, armPivot);
 arm.Rotate(amtToUp, armPivot, Space.World);
 
 //Now for the horizontal, assuming pivot is on Z:
 armPivot = arm.forward;
 armPointing = arm.up;
 toDir = Camera.main.transform.forward;
 float amtToSide = AngleAroundAxis(armPointing, toDir, armPivot);
 arm.Rotate(amtToSide, armPivot, Space.World);

As for IK, it's more or less the same thing with smartly placed limits on different segments in the chain. Like for example, the arm would only go 0.5 that amount where as the forearm would then go the full amount afterwards. The trouble becomes fixing backward bending elbows, so you should probably stick to just shoulders for now.

Edit: I forgot to mention that you would call these changes in LateUpdate(); Alternatively if the player rotates horizontally and you want to ignore everything I've mentioned, you might play around with using Animation Layers and having a second layer with an arm mask that constantly plays an upwards facing motion and you would programmatically adjust the layer weight to reflect how high the arms go.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

299 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS recoil 1 Answer

My player's camera displays jerky objects 2 Answers

Problem with multiplayer unity. 1 Answer

Animations interupting Aim-Script 0 Answers

How can I add recoil to my gun? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges