Question by
Maou_ · May 15, 2020 at 03:13 AM ·
animationcollisionanimationsikinverse kinematic
Help with Ik Fit
Hello! I'm trying to make a IK system where the foots looks for the ground and adjust the height of my character. It's better to explain using images:
This is what is happen:
And, this is what I want to happen:
I made this lowering my character controller collider radius, It works in this situation but don't works, in things like stairs or slopes and of course, break my entire collision system. Is there anyway to make this without reduce my character controller radius?
My IK script:
public class InverseKinematic : MonoBehaviour
{
public Animator ikAnimations;
public LayerMask whatIsGround;
[Range(0,1f)]
public float DistanceToGround;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnAnimatorIK(int layerIndex)
{
ikAnimations.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f);
ikAnimations.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f);
ikAnimations.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f);
ikAnimations.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f);
//LeftFoot
RaycastHit LeftHit;
RaycastHit RightHit;
if (Physics.Raycast(ikAnimations.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down, out LeftHit, DistanceToGround + 1f, whatIsGround))
{
Vector3 footPos = LeftHit.point;
footPos.y += DistanceToGround;
ikAnimations.SetIKPosition(AvatarIKGoal.LeftFoot, footPos);
ikAnimations.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.LookRotation(transform.forward, LeftHit.normal));
}
//RightFoot
if (Physics.Raycast(ikAnimations.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down, out RightHit, DistanceToGround + 1f, whatIsGround))
{
Vector3 footPos = RightHit.point;
footPos.y += DistanceToGround;
ikAnimations.SetIKPosition(AvatarIKGoal.RightFoot, footPos);
ikAnimations.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.LookRotation(transform.forward, LeftHit.normal));
}
}
}
captura-de-tela-25.png
(397.0 kB)
captura-de-tela-26.png
(401.8 kB)
Comment