Search Unity

Find object inside IK script ?

Discussion in 'Scripting' started by skyLark9, Jul 13, 2018.

  1. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    I was following IK tutorial and i come to point that instead of drag&post object "rightHandObj" into script bar. I used find function to find it. First, I notes this !!

    Second, full arm stretch forward only !!
    I got no error.
    https://docs.unity3d.com/Manual/InverseKinematics.html
    Code (CSharp):
    1.     protected Animator animator;
    2.     public bool ikActive = false;
    3.     public Transform rightHandObj;
    4.     public Transform lookObj = null;
    5.     public bool hand_on;
    6.     void Start ()
    7.     {
    8.         animator = GetComponent<Animator>();
    9.     }
    10.  
    11.     void update {
    12.      
    13.            rightHandObj = GameObject.Find("right_hand_1").transform;
    14.          
    15.     }
    16.     //a callback for calculating IK
    17.     void OnAnimatorIK()
    18.     {
    19.         if(animator) {
    20.          
    21.             if(ikActive) {
    22.  
    23.                 if(lookObj != null) {
    24.                     animator.SetLookAtWeight(1);
    25.                     animator.SetLookAtPosition(lookObj.position);
    26.                 }
    27.  
    28.                 if(rightHandObj != null) {
    29.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
    30.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);
    31.                     animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
    32.                     animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
    33.                 }    
    34.              
    35.             }
    36.          
    37.             else {      
    38.                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
    39.                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0);
    40.                 animator.SetLookAtWeight(0);
    41.             }
    42.         }
    43.     }  
    If I dropped object inside script bar and delete find line, script run without any problem.
     
    Last edited: Jul 17, 2018
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is your question? Are you receiving an error?
     
  3. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    Is not touching "lookObj". I think it was a glitch or bug.
    Today after I turn on my PC, I play the engine and every thing working fine !! o_O

    I'm still using 2018.1.

    Update:
    Its a bug. Now i get back to the same issue.
    I will update to 2018.2.
     
    Last edited: Jul 14, 2018
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Where are you setting lookObj in your code? Currently you are initializing it to null. You are setting a value for Right Hand Obj in the Inspector, but not lookObj.
     
  5. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    Yes. Drag and drop.
    Thanks god that I notes it. Or i will spend full day reading and searching.
    No need to worry.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry I don't quite follow. Are you able to answer my question regarding lookObj?
     
  7. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    P.S. script edited.
    Sorry for misunderstanding.
    In line 23, You will see that body weight rotate to LookObj. There is no range from 0 to 1. I post the lookObj from the Inspector.
    About right_hand_1, I want this script find object call "right_hand_1" and post it on "rightHandObj" bar or space [image above]. No need for "setting a value".
    As i said before, I get no error from the script. but it doesn't do his job. what i mean, right hand doesn't grab cylinder as this image show.


    It not that bug only I saw. here another bug with animation window.

    there are not animation elements at all. but if i turnoff/on unity, all things will get back run well without single error !!!