Search Unity

Capsule collider not moving at the same speed as the animation

Discussion in 'Animation' started by Hacker69, Feb 17, 2019.

  1. Hacker69

    Hacker69

    Joined:
    Dec 6, 2018
    Posts:
    2
    The animation has root motion applied, and the error that i'm getting is that the speed of the capsule collider does not match the speed of the character animation and offsets it hence messing up the camera view and other things. Here is the video for you to see what i'm talking about -
    .

    This is the code im using to give velocity to the capsule collider to be the same as the animation.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace SA
    6. {
    7.     public class AnimatorHook : MonoBehaviour
    8.     {
    9.         Animator anim;
    10.         StateManager states;
    11.  
    12.         public void Init(StateManager st)
    13.         {
    14.             states = st;
    15.             anim = st.anim;
    16.         }    
    17.  
    18.         void OnAnimatorMove()
    19.         {
    20.             if (!states.canMove)
    21.                 anim.ApplyBuiltinRootMotion();
    22.  
    23.             states.rigid.drag = 0;
    24.             float multiplier = 1;
    25.  
    26.             Vector3 delta = anim.deltaPosition;
    27.             delta.y = 0;          
    28.             Vector3 v = (delta * multiplier) / states.delta;
    29.             states.rigid.velocity = v;
    30.         }
    31.  
    32.  
    33.     }
    34. }
    35.  
    This is the main code for applying root motion and handling various other stuff :-

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace SA
    6. {
    7.  
    8.     public class StateManager : MonoBehaviour
    9.     {
    10.         [Header("Init")]
    11.         public GameObject activeModel;
    12.  
    13.         [Header("Inputs")]
    14.         public float vertical;
    15.         public float horizontal;
    16.         public float moveAmount;
    17.         public Vector3 moveDir;
    18.         public bool rt, rb, lt, lb;
    19.  
    20.         [Header("Stats")]
    21.         public float moveSpeed = 5f;
    22.         public float runSpeed = 8f;
    23.         public float rotateSpeed = 20;
    24.         public float toGround = 0.5f;
    25.  
    26.         [Header("States")]
    27.         public bool onGround;
    28.         public bool run;
    29.         public bool lockOn;
    30.         public bool inAction;
    31.         public bool canMove;
    32.  
    33.         [Header("Other")]
    34.         public EnemyTarget lockOnTarget;
    35.  
    36.         [HideInInspector]
    37.         public Animator anim;
    38.  
    39.         [HideInInspector]
    40.         public Rigidbody rigid;
    41.         [HideInInspector]
    42.         public AnimatorHook a_hook;
    43.  
    44.         [HideInInspector]
    45.         public float delta;
    46.         [HideInInspector]
    47.         public LayerMask ignoreLayers;
    48.  
    49.         float _actionDelay;
    50.  
    51.         public void Init()
    52.         {
    53.             SetupAnimator();
    54.             rigid = GetComponent<Rigidbody>();
    55.             rigid.angularDrag = 999;
    56.             rigid.drag = 4;
    57.             rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
    58.  
    59.             a_hook = activeModel.AddComponent<AnimatorHook>();
    60.             a_hook.Init(this);
    61.  
    62.             gameObject.layer = 8;
    63.             ignoreLayers = ~(1 << 9);
    64.  
    65.             anim.SetBool("onGround",true);
    66.         }
    67.  
    68.         void SetupAnimator()
    69.         {          
    70.             if(activeModel == null)
    71.             {
    72.                 anim = GetComponentInChildren<Animator>();
    73.                 if(anim == null)
    74.                 {
    75.                     Debug.Log("no model found");
    76.                 }
    77.                 else
    78.                 {
    79.                     activeModel = anim.gameObject;
    80.                 }
    81.             }
    82.             if(anim == null)
    83.             {
    84.                 anim = activeModel.GetComponent<Animator>();
    85.             }
    86.             //anim.applyRootMotion = false;
    87.         }
    88.  
    89.         public void FixedTick(float d)
    90.         {
    91.             delta = d;
    92.             rigid.drag = (moveAmount > 0 || !onGround) ? 0 : 4;
    93.  
    94.             DetectAction();
    95.  
    96.             if (inAction)
    97.             {
    98.                 // anim.applyRootMotion = true;
    99.  
    100.                 _actionDelay += delta;
    101.                 if(_actionDelay > 0.3f)
    102.                 {
    103.                     inAction = false;
    104.                     _actionDelay = 0;
    105.                 }
    106.                 else
    107.                 {
    108.                     return;
    109.                 }
    110.             }
    111.  
    112.             canMove = anim.GetBool("canMove");
    113.  
    114.             if (!canMove)
    115.             {
    116.                 return;
    117.             }
    118.             //anim.applyRootMotion = false;
    119.          
    120.  
    121.             float targetSpeed = moveSpeed;
    122.  
    123.             if (run)
    124.                 targetSpeed = runSpeed;          
    125.          
    126.  
    127.             if(onGround)
    128.                 rigid.velocity = moveDir * (targetSpeed * moveAmount);
    129.  
    130.            /* if (run)
    131.                 lockOn = false;
    132.                 */
    133.  
    134.             Vector3 targetDir = (lockOn == false) ? moveDir : lockOnTarget.transform.position - transform.position;
    135.             targetDir.y = 0;
    136.             if (targetDir == Vector3.zero)
    137.                 targetDir = transform.forward;
    138.             Quaternion tr = Quaternion.LookRotation(targetDir);
    139.             Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, delta * moveAmount * rotateSpeed);
    140.             transform.rotation = targetRotation;
    141.  
    142.             anim.SetBool("lockon", lockOn);
    143.  
    144.             if (lockOn == false)
    145.                 HandleMovementAnimations();
    146.             else
    147.                 HandleLockOnAnimations(moveDir);
    148.         }
    149.  
    150.         public void DetectAction()
    151.         {
    152.             if (canMove == false)
    153.                 return;
    154.  
    155.             if (rb == false && rt == false && lt == false && lb == false)
    156.                 return;
    157.  
    158.             string targetAnim = null;
    159.  
    160.             if (rb)
    161.                 targetAnim = "Sword And Shield Attack";
    162.             if (rt)
    163.                 targetAnim = "Stable Sword Outward Slash";
    164.             if (lb)
    165.                 targetAnim = "Standing Melee Attack Horizontal";
    166.             if (lt)
    167.                 targetAnim = "Sword And Shield Slash (1)";
    168.  
    169.             if (string.IsNullOrEmpty(targetAnim))
    170.                 return;
    171.  
    172.             canMove = false;
    173.             inAction = true;
    174.             anim.CrossFade(targetAnim,0.2f);
    175.             //rigid.velocity = Vector3.zero;
    176.         }
    177.  
    178.         public void Tick(float d)
    179.         {
    180.             delta = d;
    181.             onGround = OnGround();
    182.  
    183.             anim.SetBool("onGround", onGround);
    184.         }
    185.  
    186.         void HandleMovementAnimations()
    187.         {
    188.             anim.SetBool("run", run);
    189.             anim.SetFloat("Vertical", moveAmount ,0.4f,delta);
    190.         }
    191.  
    192.         void HandleLockOnAnimations(Vector3 moveDir)
    193.         {
    194.             Vector3 relativeDir = transform.InverseTransformDirection(moveDir);
    195.             float h = relativeDir.x;
    196.             float v = relativeDir.z;
    197.  
    198.             anim.SetFloat("Vertical", v, 0.2f, delta);
    199.             anim.SetFloat("Horizontal", h, 0.2f, delta);
    200.  
    201.  
    202.         }
    203.  
    204.         public bool OnGround()
    205.         {
    206.             bool r = false;
    207.             Vector3 origin = transform.position + (Vector3.up * toGround);
    208.             Vector3 dir = -Vector3.up;
    209.             float dis = toGround + 0.3f;
    210.             RaycastHit hit;
    211.             if(Physics.Raycast(origin,dir,out hit,dis))
    212.             {
    213.                 r = true;
    214.                 Vector3 targetPosition = hit.point;
    215.                 transform.position = targetPosition;
    216.             }
    217.             return r;
    218.         }
    219.     }
    220. }
    221.  
    These are the two main pieces of code that control the character. The other two pieces of code basically take input and controll the camera thats it, so i didnt think it would be necessary for me to upload those codes
     
    Last edited: Feb 17, 2019