Jump to content
  • Advertisement
Transcendent

C# Capsule collider not moving at the same speed as the animation(UNITY 3D)

Recommended Posts

So this is the problem that I have :- https://youtu.be/kU8Dm5bDJXg

This is the code i am using:-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace SA
{
    public class AnimatorHook : MonoBehaviour
    {
        Animator anim;
        StateManager states;
 
        public void Init(StateManager st)
        {
            states = st;
            anim = st.anim;
        }    
 
        void OnAnimatorMove()
        {
            if (!states.canMove)
                anim.ApplyBuiltinRootMotion();
 
            states.rigid.drag = 0;
            float multiplier = 1;
 
            Vector3 delta = anim.deltaPosition;
            delta.y = 0;          
            Vector3 v = (delta * multiplier) / states.delta;
            states.rigid.velocity = v;
        }
 
 
    }
}

For additional reference see the following code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace SA
{
 
    public class StateManager : MonoBehaviour
    {
        [Header("Init")]
        public GameObject activeModel;
 
        [Header("Inputs")]
        public float vertical;
        public float horizontal;
        public float moveAmount;
        public Vector3 moveDir;
        public bool rt, rb, lt, lb;
 
        [Header("Stats")]
        public float moveSpeed = 5f;
        public float runSpeed = 8f;
        public float rotateSpeed = 20;
        public float toGround = 0.5f;
 
        [Header("States")]
        public bool onGround;
        public bool run;
        public bool lockOn;
        public bool inAction;
        public bool canMove;
 
        [Header("Other")]
        public EnemyTarget lockOnTarget;
 
        [HideInInspector]
        public Animator anim;
 
        [HideInInspector]
        public Rigidbody rigid;
        [HideInInspector]
        public AnimatorHook a_hook;
 
        [HideInInspector]
        public float delta;
        [HideInInspector]
        public LayerMask ignoreLayers;
 
        float _actionDelay;
 
        public void Init()
        {
            SetupAnimator();
            rigid = GetComponent<Rigidbody>();
            rigid.angularDrag = 999;
            rigid.drag = 4;
            rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
 
            a_hook = activeModel.AddComponent<AnimatorHook>();
            a_hook.Init(this);
 
            gameObject.layer = 8;
            ignoreLayers = ~(1 << 9);
 
            anim.SetBool("onGround",true);
        }
 
        void SetupAnimator()
        {          
            if(activeModel == null)
            {
                anim = GetComponentInChildren<Animator>();
                if(anim == null)
                {
                    Debug.Log("no model found");
                }
                else
                {
                    activeModel = anim.gameObject;
                }
            }
            if(anim == null)
            {
                anim = activeModel.GetComponent<Animator>();
            }
            //anim.applyRootMotion = false;
        }
 
        public void FixedTick(float d)
        {
            delta = d;
            rigid.drag = (moveAmount > 0 || !onGround) ? 0 : 4;
 
            DetectAction();
 
            if (inAction)
            {
                // anim.applyRootMotion = true;
 
                _actionDelay += delta;
                if(_actionDelay > 0.3f)
                {
                    inAction = false;
                    _actionDelay = 0;
                }
                else
                {
                    return;
                }
            }
 
            canMove = anim.GetBool("canMove");
 
            if (!canMove)
            {
                return;
            }
            //anim.applyRootMotion = false;
         
 
            float targetSpeed = moveSpeed;
 
            if (run)
                targetSpeed = runSpeed;          
         
 
            if(onGround)
                rigid.velocity = moveDir * (targetSpeed * moveAmount);
 
           /* if (run)
                lockOn = false;
                */
 
            Vector3 targetDir = (lockOn == false) ? moveDir : lockOnTarget.transform.position - transform.position;
            targetDir.y = 0;
            if (targetDir == Vector3.zero)
                targetDir = transform.forward;
            Quaternion tr = Quaternion.LookRotation(targetDir);
            Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, delta * moveAmount * rotateSpeed);
            transform.rotation = targetRotation;
 
            anim.SetBool("lockon", lockOn);
 
            if (lockOn == false)
                HandleMovementAnimations();
            else
                HandleLockOnAnimations(moveDir);
        }
 
        public void DetectAction()
        {
            if (canMove == false)
                return;
 
            if (rb == false && rt == false && lt == false && lb == false)
                return;
 
            string targetAnim = null;
 
            if (rb)
                targetAnim = "Sword And Shield Attack";
            if (rt)
                targetAnim = "Stable Sword Outward Slash";
            if (lb)
                targetAnim = "Standing Melee Attack Horizontal";
            if (lt)
                targetAnim = "Sword And Shield Slash (1)";
 
            if (string.IsNullOrEmpty(targetAnim))
                return;
 
            canMove = false;
            inAction = true;
            anim.CrossFade(targetAnim,0.2f);
            //rigid.velocity = Vector3.zero;
        }
 
        public void Tick(float d)
        {
            delta = d;
            onGround = OnGround();
 
            anim.SetBool("onGround", onGround);
        }
 
        void HandleMovementAnimations()
        {
            anim.SetBool("run", run);
            anim.SetFloat("Vertical", moveAmount ,0.4f,delta);
        }
 
        void HandleLockOnAnimations(Vector3 moveDir)
        {
            Vector3 relativeDir = transform.InverseTransformDirection(moveDir);
            float h = relativeDir.x;
            float v = relativeDir.z;
 
            anim.SetFloat("Vertical", v, 0.2f, delta);
            anim.SetFloat("Horizontal", h, 0.2f, delta);
 
 
        }
 
        public bool OnGround()
        {
            bool r = false;
            Vector3 origin = transform.position + (Vector3.up * toGround);
            Vector3 dir = -Vector3.up;
            float dis = toGround + 0.3f;
            RaycastHit hit;
            if(Physics.Raycast(origin,dir,out hit,dis))
            {
                r = true;
                Vector3 targetPosition = hit.point;
                transform.position = targetPosition;
            }
            return r;
        }
    }
}

I've been stuck on this for too long, any help will be highly appreciated

Share this post


Link to post
Share on other sites
Advertisement
17 minutes ago, Mostafa Yahia said:

Be Sure that any of the object child have this collider 

and make x,y,z = 0 in the middle of the object 

I have ensured everything you mentioned above and it still does not work

Share this post


Link to post
Share on other sites
11 hours ago, Mostafa Yahia said:

ok create another gameobject and test animation with it with the same script and check if the problem still exist 

Thanks for your replies, but it is still not working

Share this post


Link to post
Share on other sites

What role does 'canMove' have in this? if you set canMove false in your controller so it early outs on FixedTick what is preventing the animation of the ybot child object from continuing and moving it away from its parent?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!