Search Unity

IK Implementation

Discussion in 'Scripting' started by Hris54, Jan 16, 2022.

  1. Hris54

    Hris54

    Joined:
    Dec 6, 2021
    Posts:
    65
    Hi everyone,
    I created a climb system using raycast that detects obstacle, its location , ledges location and I am able to successfully jump with Vector3.Lerp(), but the problem is I know very little about IK, I created a IK script for hands but its not working properly. Can anyone help me with this please?
    Thank You
    My IK Code-IK.cs
     

    Attached Files:

    • IK.cs
      File size:
      945 bytes
      Views:
      201
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Actually the problem is nobody here can read your mind.

    Hm. Not working properly...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Hris54 likes this.
  3. Hris54

    Hris54

    Joined:
    Dec 6, 2021
    Posts:
    65
    Okay my bad, it's my second time asking anything online . I couldn't find it anywhere else . I didn't know about code snippets so I uploaded my script file. The problem is the hands placements are wrong, LedgePos is the position i want my hands to be, and how can one Implement hand rotation? I hope I am now clear with my code and my problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class IK : MonoBehaviour {
    5.     Animator anim;
    6.     float leftHand_Weight;
    7.     float rightHand_Weight;
    8.   PlayerController pc;
    9.     private void Start()
    10.     {
    11.         pc=GetComponent<PlayerController>();
    12.         anim = GetComponent<Animator>();
    13.     }
    14.     private void OnAnimatorIK(int layerIndex)
    15.     {
    16.  
    17.      
    18.  
    19.         leftHand_Weight=anim.GetFloat("Left Hand");
    20.         rightHand_Weight=anim.GetFloat("Right Hand");
    21.  
    22.      
    23.    
    24.  
    25.         anim.SetIKPositionWeight(AvatarIKGoal.LeftHand,  leftHand_Weight);
    26.      
    27.         anim.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHand_Weight);
    28.      
    29.         anim.SetIKPosition(AvatarIKGoal.LeftHand, pc.LedgePos+new Vector3(-.4f,1.5f,0f));
    30.         anim.SetIKPosition(AvatarIKGoal.RightHand, pc.LedgePos+new Vector3(.4f,1.5f,0f));
    31.    
    32.    
    33.  
    34.     }
    35.  
    36. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    I would strip it down to the point where you have a Transform for the ledge anchor point that you're tracking with IK and then perhaps just one arm.

    Most likely the solution will just need you to pre-flip that anchor point to be something different based on what the IK expects.

    I would also recommend getting away from adding magic numbers in like lines 29 and 30 do and instead use a separate transform or offset authored into the scene or prefab. Otherwise your code will break if the underlying geometry changes.