Search Unity

IK glitch ?

Discussion in 'Scripting' started by Quast, Jan 18, 2019.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi.

    I'm scripting character IK to look at object "enemy" direction. If enemy is above him, he look at enemy. The issue here is when enemy goes out of character range. Character should look forward, and he do. But for one second, you can see a kind of glitch movement. Here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookingScript : MonoBehaviour
    6. {
    7.  
    8.     private Animator animator;
    9.     private PlayerMovement _active; // check f enemy is inside character range or not
    10.     public bool ikActive = false;
    11.     public GameObject rightHandObj;
    12.     public GameObject leftHandObj;
    13.     public Transform lookObj = null;
    14.     public float ad, sd;
    15.  
    16.     void Start()
    17.     {
    18.    
    19.         _active = GetComponent<PlayerMovement>();
    20.         animator = GetComponent<Animator>();
    21.  
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.  
    27.         lookObj = _active.enemy; // enemy object to look at.
    28.  
    29.         if (_active.actives == false && lookObj) // this to give smooth movement for IK.
    30.         {
    31.             ikActive = true;
    32.             ad = ad + Time.deltaTime;
    33.             sd = sd + Time.deltaTime;
    34.             if (ad >= 1)
    35.             {
    36.                 ad = 1;
    37.             }
    38.             if (sd >= 0.15f)
    39.             {
    40.                 sd = 0.15f;
    41.             }
    42.  
    43.         }
    44.         else
    45.         {
    46.             sd = sd - Time.deltaTime;
    47.             ad = ad - Time.deltaTime;
    48.             ikActive = false;
    49.             if (ad <= 0)
    50.             {
    51.                 ad = 0.0f;
    52.             }
    53.             if (sd <= 0)
    54.             {
    55.  
    56.                 sd = 0.0f;
    57.             }
    58.         }
    59.  
    60.     } // end Uploade
    61.  
    62.     void OnAnimatorIK()
    63.     {
    64.         if (animator)
    65.         {
    66.  
    67.             if (ikActive == true)
    68.             {
    69.  
    70.                 if (lookObj != null)
    71.                 {
    72.                     animator.SetLookAtWeight(ad, sd, 0, 0, 0);
    73.                     animator.SetLookAtPosition(lookObj.position);
    74.  
    75.                 }
    76.  
    77.                 if (rightHandObj != null)
    78.                 {
    79.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    80.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    81.                     animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.transform.position);
    82.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.transform.rotation);
    83.                 }
    84.  
    85.                 if (leftHandObj != null)
    86.                 {
    87.                     animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    88.                     animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    89.                     animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.transform.position);
    90.                     animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.transform.rotation);
    91.                 }
    92.  
    93.             }
    94.  
    95.             else
    96.             {
    97.  
    98.                 animator.SetLookAtWeight(ad, sd, 0, 0, 0);
    99.  
    100.                 if (rightHandObj != null)
    101.                 {
    102.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
    103.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
    104.                     animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.transform.position);
    105.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.transform.rotation);
    106.                 }
    107.  
    108.                 if (leftHandObj != null)
    109.                 {
    110.                     animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
    111.                     animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
    112.                     animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.transform.position);
    113.                     animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.transform.rotation);
    114.                 }
    115.  
    116.             } // end else
    117.  
    118.         }
    119.     } // end IK
    120.  
    121. }
    122.  
    123.