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