Search Unity

Question Is there a way to override the animation of the target when moving an object to the target ?

Discussion in 'Animation' started by Chocolade, Sep 2, 2021.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    The player hand is moving a bit all the time because the player have animator controller and when the player is in idle everything in the player is a bit moving. He is in idle but not standing still it's idle animation.

    Now I move an object to the player hand and because the player hand is moving when I drag the player away from the moving object when the game is running and updating the target in Update the moving object is jumping up down because he keep following the player hand.

    Is there a way to override the animation of the hand or make it looks more natural following ?

    Code (csharp):
    1.  
    2. using Cinemachine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class MoveToTarget : MonoBehaviour
    8. {
    9.     public enum TransitionState
    10.     {
    11.         None,
    12.         MovingTowards,
    13.         Transferring
    14.     }
    15.  
    16.     public Transform destinationTransform;
    17.     public bool isChild = false;
    18.     public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    19.     public float duration = 10.0f;
    20.     public GameObject mainCam;
    21.     public CinemachineFreeLook freeLookCamGameplay;
    22.     public CinemachineFreeLook freeLookCamPickUp;
    23.     public UnlockCrate unlockCrate;
    24.     public InteractableItem interactableItem;
    25.  
    26.     private float t;
    27.     private Transform originTransform;
    28.     private float timer;
    29.     private TransitionState state = TransitionState.MovingTowards;
    30.     private Vector3 originPosition;
    31.  
    32.     void Start()
    33.     {
    34.         t = 0.0f;
    35.  
    36.         curve.postWrapMode = WrapMode.Once;
    37.         originPosition = transform.position;
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         if (unlockCrate.HasOpened())
    43.         {
    44.             interactableItem.enabled = true;
    45.  
    46.             originTransform = destinationTransform;
    47.  
    48.             switch (state)
    49.             {
    50.                 case TransitionState.MovingTowards:
    51.                     var v = destinationTransform.position - transform.position;
    52.                     if (v.magnitude < 0.001f)
    53.                     {
    54.                         SwitchCameras();
    55.  
    56.                         state = TransitionState.Transferring;
    57.                         originTransform = destinationTransform;
    58.                         timer = 0;
    59.                         return;
    60.                     }
    61.  
    62.                     t += Time.deltaTime;
    63.                     float s = t / duration;
    64.  
    65.                     transform.position = Vector3.Lerp(originPosition,
    66.                         destinationTransform.position, curve.Evaluate(s));
    67.  
    68.                     break;
    69.  
    70.                 case TransitionState.Transferring:
    71.                     timer += Time.deltaTime;
    72.                     this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
    73.                     if (timer >= 1.0f)
    74.                     {
    75.                         this.transform.parent = destinationTransform;
    76.                         transform.localPosition = new Vector3(0, 0, 0);
    77.                         isChild = true;
    78.                         state = TransitionState.None;
    79.                         this.enabled = false;
    80.                         return;
    81.                     }
    82.                     break;
    83.  
    84.                 default:
    85.                     this.enabled = false;
    86.                     return;
    87.             }
    88.         }
    89.     }
    90.  
    91.     private void SwitchCameras()
    92.     {
    93.         var brain = mainCam.GetComponent<CinemachineBrain>();
    94.         brain.m_DefaultBlend.m_Time = 1f;
    95.         freeLookCamGameplay.enabled = true;
    96.         freeLookCamPickUp.enabled = false;
    97.     }
    98. }
    99.  
    At this line 45 I'm making the update so the moving object will keep following the player hand even if the player is moving away from the moving object or maybe moving closer to the moving object :

    Code (csharp):
    1.  
    2. originTransform = destinationTransform;
    3.  
    but it does not look that natural and if I move the player toward the moving object than it's getting worst.

    The goal is to create some kind of chasing effect or make the moving object to reach the player the problem is that it's not looking that good because the player animation. The chasing or the following is working it's just not looks that good. The following/chasing effect should be more smooth I think.

    Not linear like straight line but something that will less follow the animated target. The moving object is copying the target animation movements too.
     
  2. KaiserKaur

    KaiserKaur

    Joined:
    Aug 16, 2021
    Posts:
    6
    I have had similar problem and just ditched everything.... :(
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    I think I solved it.

    If you want I will add here all my scripts because I didn't before and also a small short video clip showing what I did how it's working so you can see if you like it. You can change anything you want in the scripts later

    For me it's working perfect. I can select if I want to make a chasing mode or a child mode in child mode the object will just move to a target non moving target and will become child of the target smooth.

    In chase mode the object will chase the player smooth and than will become a child of a target while the player is moving. I think a nice chasing mode.

    It will take me sometime to upload it here everything but I will today.