I am trying to make a procedurally animated person, but the IK is not working. When I move the hip bone, the leg IK does not follow the target properly. Here is the editor:
Video:
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProcedualHandler : MonoBehaviour
{
[SerializeField] Transform leftLegTarget;
[SerializeField] float leftDistance;
[SerializeField] Transform leftCaster;
[SerializeField] Transform rightLegTarget;
[SerializeField] float rightDistance;
[SerializeField] Transform rightCaster;
RaycastHit leftHit;
RaycastHit rightHit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Ray leftRay = new Ray(leftCaster.position, Vector3.down);
Ray leftGroundingRay = new Ray(leftCaster.position, Vector3.down);
Ray rightRay = new Ray(rightCaster.position, Vector3.down);
Ray rightGroundingRay = new Ray(rightCaster.position, Vector3.down);
if(Physics.Raycast(leftGroundingRay, out RaycastHit leftGroundHit))
{
leftLegTarget.position = new Vector3(leftLegTarget.position.x, leftGroundHit.point.y, leftLegTarget.position.z);
}
if(Physics.Raycast(leftRay, out leftHit) && Vector3.Distance(leftLegTarget.position, leftHit.point) > leftDistance)
{
leftLegTarget.position = leftHit.point;
}
if(Physics.Raycast(rightGroundingRay, out RaycastHit rightGroundHit))
{
rightLegTarget.position = new Vector3(rightLegTarget.position.x, rightGroundHit.point.y, rightLegTarget.position.z);
}
if(Physics.Raycast(rightRay, out rightHit) && Vector3.Distance(rightLegTarget.position, rightHit.point) > rightDistance)
{
rightLegTarget.position = rightHit.point;
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawCube(leftCaster.position, new Vector3(0.1f, 0.1f, 0.1f));
Gizmos.DrawCube(rightCaster.position, new Vector3(0.1f, 0.1f, 0.1f));
Gizmos.DrawLine(leftCaster.position, leftHit.point);
Gizmos.DrawLine(rightCaster.position, rightHit.point);
}
}
I don’t care that the procedural animation does not work yet, I am just trying to fix the bad IK.
Thank you!