Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by kmay11005 · Nov 16, 2020 at 11:06 PM · script.glitchinggun movementprocedural animationgun-animation

Script creating glitchy movement

I have a FPS character game object, which consists of just the arms. I made this IK script off of a tutorial.`{ public int chainLength = 2;

 public Transform target;
 public Transform pole;

 public int iterations = 10;

 public float marginOfError = 0.001f;

 [Range(0, 1)]
 public float snapBackStrength = 1f;

 protected float[] bonesLength;
 protected float completeLength;
 protected Transform[] bones;
 protected Vector3[] positions;
 protected Vector3[] startDirection;
 protected Quaternion[] startRotationBone;
 protected Quaternion startRotationTarget;
 protected Quaternion startRotationRoot;

 public bool ikActive = false;

 // Start is called before the first frame update
 void Start()
 {
     if (ikActive)
     {
         Init();
     }
 }

 // Update is called once per frame
 void Update()
 {
     
 }

 void LateUpdate()
 {
     if (ikActive)
     {
         ResolveIK();
     }
 }

 void Init()
 {
     bones = new Transform[chainLength + 1];
     positions = new Vector3[chainLength + 1];
     bonesLength = new float[chainLength];
     startDirection = new Vector3[chainLength + 1];
     startRotationBone = new Quaternion[chainLength + 1];

     startRotationTarget = target.rotation;
     completeLength = 0;

     var current = transform;
     for(var i = bones.Length - 1; i>= 0; i--)
     {
         bones[i] = current;
         startRotationBone[i] = current.rotation;

         if (i == bones.Length - 1)
         {
             startDirection[i] = target.position - current.position;
         }

         else
         {
             startDirection[i] = bones[i + 1].position - current.position;
             bonesLength[i] = (bones[i + 1].position - current.position).magnitude;
             completeLength += bonesLength[1];
         }

         current = current.parent;
     }
 }

 void ResolveIK()
 {
     if (target == null)
     {
         return;
     }

     if (bonesLength.Length != chainLength)
     {
         Init();
     }

     for (int i = 0; i < bones.Length; i++)
     {
         positions[i] = bones[i].position;
     }

     var rootRotation = (bones[0].parent != null) ? bones[0].parent.rotation : Quaternion.identity;
     var rootRotationDifference = rootRotation * Quaternion.Inverse(startRotationRoot);
    
     if ((target.position - bones[0].position).magnitude >= completeLength)
     {
         var direction = (target.position - bones[0].position).normalized;

         for (int i = 1; i < positions.Length; i++)
         {
             positions[i] = positions[i - 1] + direction * bonesLength[i - 1];
         }
     }
       
     else
     {
         for (int i = 0; i < positions.Length - 1; i++)
         {
             positions[i + 1] = Vector3.Lerp(positions[i + 1], positions[i] + rootRotationDifference * startDirection[i], snapBackStrength);
         }

         for (int iteration = 0; iteration < iterations; iteration++)
         {
             for (int i = positions.Length - 1; i > 0; i--)
             {
                 if (i == positions.Length - 1)
                 {
                     positions[i] = target.position;
                 }

                 else
                 {
                     positions[i] = positions[i + 1] + (positions[i] - positions[i + 1]).normalized * bonesLength[i];
                 }
             }

             for (int i = 1; i < positions.Length; i++)
             {
                 positions[i] = positions[i - 1] + (positions[i] - positions[i - 1]).normalized * bonesLength[i - 1];
             }

             if ((positions[positions.Length - 1]).magnitude < marginOfError)
             {
                 break;
             }
         }
     }

     if (pole != null)
     {
         for(int i = 1; i < positions.Length - 1; i++)
         {
             var plane = new Plane(positions[i + 1] - positions[i - 1], positions[i - 1]);
             var projectedPole = plane.ClosestPointOnPlane(pole.position);
             var projectedBone = plane.ClosestPointOnPlane(positions[i]);
             var angle = Vector3.SignedAngle(projectedBone - positions[i - 1], projectedPole - positions[i -1], plane.normal);
             positions[i] = Quaternion.AngleAxis(angle, plane.normal) * (positions[i] - positions[i - 1] + positions[i - 1]);
         }
     }

     for (int i = 0; i < positions.Length; i++)
     {
         if (i == positions.Length - 1)
         {
             bones[i].rotation = target.rotation * Quaternion.Inverse(startRotationTarget) * startRotationBone[i];
         }

         else
         {
             bones[i].rotation = Quaternion.FromToRotation(startDirection[i], positions[i + 1] - positions[i]) * startRotationBone[i];
         }

         bones[i].position = positions[i];
     }
 }

} The hierarchy looks like this:

  1. FPS Person

  2. (the arm mesh)

  3. FPS armature

  4.  camera bone
    
  5.     shoulder bone.L
    
  6.         upper arm bone.L
    
  7.            lower arm bone.L
    
  8.               hand.L
    
  9.      shoulder bone.L
    
  10.         upper arm bone.L
    
  11.            lower arm bone.L
    
  12.               hand.L
    
  13.                  weapon holder (empty object)
    
  14.                     assault rifle
    
  15.                         (assault rifle mesh)
    
  16.                          assault rifle armature (gun bone and magazine bone)
    
  17.                          left hand position    (empty game object)
    
  18.                          right hand position (empty game object)
    
    

I have the IK script attached to each of the hand bones with a chain length of 2, with no pole set. I have the target for the left hand as the left hand position, and I have the target for the right hand as the right hand position. The right hand position is in the exact same location as the hand bone.

The left hand IK works fine. For some reason, when I rotate the right hand position, the hand moves, but it moves really fast and in a jagged way, so that it looks glitchy. It also moves up and down even if I'm not rotating the gun.

Why is this happening, and how can I fix this?

Comment
Add comment · Show 1
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
avatar image kmay11005 · Nov 15, 2020 at 05:08 PM 0
Share

I'd preferably not rotate the weapon holder itself, as I plan to have multiple weapons as a child object for weapon switching and I don't want any weird rotations for the other weapons.

0 Replies

· Add your reply
  • Sort: 

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

243 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

Related Questions

Cant call void function from another script 0 Answers

Adda cooldown to a Shooting Script 0 Answers

How to rotate a Pivot with a front facing Player with a mouse?,Does anyone know the code for front pivot movement using mouse to aim? 1 Answer

How to get rich text from TextMeshPro input field? 0 Answers

How to solve java.lang.ClassNotFoundException ? 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