Search Unity

Question using animation event to move a child object towards a target the child releasing is not smooth

Discussion in 'Animation' started by haimmoshe, Feb 22, 2022.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    I recorded a short video clip of the untiy editor showing the problem.
    The problem is at the seconds 39-40.

    What I'm trying to do is at specific point in the animation using animation event to send(throw) the child object towards a target using animation curve and a lerp.

    Everything is working but the releasing of the child object from the parent in the animation event.
    I tried to increase the child moving starting speed but it didn't work and didn't fix the problem.

    The animation is working fine the releasing part is the problem.

    I'm using animation curve and lerp for easing out/in the child object.



    This script is attached to the player where the animator controller is and the animation event is :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ThrowObject : MonoBehaviour
    7. {
    8.     public MoveToTarget moveToTarget;
    9.  
    10.     public void ThrowEvent()
    11.     {
    12.         moveToTarget.go = true;
    13.     }
    14. }
    15.  
    And this code is attached to the child object that start moving when the animation event begin :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using Cinemachine;
    7.  
    8. public class MoveToTarget : MonoBehaviour
    9. {
    10.     public enum TransitionState
    11.     {
    12.         None,
    13.         MovingTowards,
    14.         MovingBackward
    15.     }
    16.  
    17.     public Transform destinationTransform;
    18.     public bool isChild = false;
    19.     public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    20.     public float duration = 10.0f;
    21.     public bool go = false;
    22.     public Text[] debbugingUiTexts;
    23.     public Transform parentPos;
    24.  
    25.     private float t;
    26.     private Transform originTransform;
    27.     private float timer;
    28.     private TransitionState state = TransitionState.MovingTowards;
    29.     private Vector3 originPosition;
    30.     private SphereCollider col;
    31.     private bool enableCollider = true;
    32.     private bool updateOriginPosition = true;
    33.     private float rounded;
    34.     private float durationCounter;
    35.  
    36.     void Start()
    37.     {
    38.         t = 0.0f;
    39.  
    40.         curve.postWrapMode = WrapMode.Once;
    41.         col = GetComponent<SphereCollider>();
    42.     }
    43.  
    44.     void Update()
    45.     {
    46.         var v1 = destinationTransform.position - transform.position;
    47.         if (v1.magnitude < 0.01f)
    48.         {
    49.             t = 0.0f;
    50.             originPosition = transform.position;
    51.             state = TransitionState.MovingBackward;
    52.         }
    53.  
    54.         if (Input.GetKeyDown(KeyCode.B))
    55.         {
    56.             t = 0.0f;
    57.             originPosition = transform.position;
    58.             state = TransitionState.MovingBackward;
    59.         }
    60.  
    61.         if (go)
    62.         {
    63.             transform.parent = null;
    64.  
    65.             if (updateOriginPosition == true)
    66.             {
    67.                 originPosition = transform.position;
    68.                 updateOriginPosition = false;
    69.             }
    70.  
    71.             if (col != null && enableCollider)
    72.             {
    73.                 Destroy(col);
    74.                 enableCollider = false;
    75.             }
    76.  
    77.             switch (state)
    78.             {
    79.                 case TransitionState.MovingTowards:
    80.  
    81.                     t += Time.deltaTime;
    82.                     float s = t / duration;
    83.  
    84.                     durationCounter += Time.deltaTime;
    85.                     rounded = Mathf.Round(durationCounter);
    86.                     debbugingUiTexts[0].text = rounded.ToString();
    87.  
    88.                     transform.position = Vector3.Lerp(originPosition,
    89.                         destinationTransform.position, curve.Evaluate(s));
    90.  
    91.                     break;
    92.  
    93.                 case TransitionState.MovingBackward:
    94.  
    95.                     if (transform.position != parentPos.position)
    96.                     {
    97.                         t += Time.deltaTime;
    98.                         float s1 = t / duration;
    99.  
    100.                         durationCounter += Time.deltaTime;
    101.                         rounded = Mathf.Round(durationCounter);
    102.                         debbugingUiTexts[0].text = rounded.ToString();
    103.  
    104.                         transform.position = Vector3.Lerp(originPosition,
    105.                             parentPos.position,
    106.                             curve.Evaluate(s1));
    107.                     }
    108.  
    109.                     break;
    110.  
    111.                 default:
    112.                     this.enabled = false;
    113.                     return;
    114.             }
    115.         }
    116.     }
    117. }
    118.