- Home /
Keeping 4-Legged entity grounded
INITIAL PROBLEM: Unable to keep all 4 feet grounded on sloped terrain. IK is build for humanoids not creatures with 4 legs. I've tried various methods to solve it, nothing really great.
UPDATE 1: So far this is my solution, also I should mention I included this in my root behaviour system because all my animals are missing root motion, and I added this via curve. Also was hoping to tie in an IK solution here but that didn't work out. SO I defined the front and back of the entity and attempted to make sure it stayed... contoured lol. Seriously a janky solution, doesn't look bad. Also was getting erroneous behaviour with the Z axis and sometimes the X axis was going to far in a single direction, so I clamped the X to a float+- and clamped Z to zero , but unfortunately this means the animal can never contour at an angle that's left or rift of the animal. Luckily this game isn't AAA so it it looks fine as it, but this solution is terrible in my professional opinion.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class RootMotionController : MonoBehaviour
{
private Animator animator;
private MLSpace.NPCScript npc;
public Vector2 forwardAxisClamp = new Vector2(-20, 20);
public float forwardRotationSpeed = 30.0f;
public float RootMotionSpeedModifier = 1.0f;
public float footGroundDistance = 0.05f;
public float FootCheckDistance = 0.5f;
private const float fullCircle = 360f;
void Awake()
{
npc = GetComponent<MLSpace.NPCScript>();
animator = GetComponent<Animator>();
}
public Transform frontTransform, backTransform;
void OnAnimatorMove()
{
if (animator)
{
Vector3 newPosition = transform.position;
newPosition += transform.forward *( animator.GetFloat("Motion") * RootMotionSpeedModifier) * Time.smoothDeltaTime;
transform.position = newPosition;
}
}
void Update()
{
if (!npc.isDead)
{
Ray ray = new Ray(frontTransform.position, Vector3.down);
Ray ray2 = new Ray(backTransform.position, Vector3.down);
RaycastHit hit;
RaycastHit hit2;
Physics.Raycast(ray, out hit, FootCheckDistance);
Physics.Raycast(ray2, out hit2, FootCheckDistance);
if (hit.distance > hit2.distance)
{
if (hit.distance > footGroundDistance)
{
transform.Rotate(forwardRotationSpeed * Time.deltaTime, 0, 0);
}
}
else
{
if (hit2.distance > footGroundDistance)
{
transform.Rotate(-forwardRotationSpeed * Time.deltaTime, 0, 0);
}
}
Vector3 rot = transform.localEulerAngles;
rot.z = 0;
if (rot.x < fullCircle/2)
{
if (rot.x > forwardAxisClamp.y)
{
rot.x = forwardAxisClamp.y;
}
}
else
{
if (rot.x < fullCircle + forwardAxisClamp.x)
{
rot.x = fullCircle + forwardAxisClamp.x;
}
}
transform.localEulerAngles = rot;
}
}
}
Also it should be noted that the fullCircle modifier is because localEulers don't go below zero, the revert back to 360 and move down, if above 360, they move to 1 and greater. My simples solution was that if it was <180 degrees, it was obviously on the 1 and great side of things, and if it was above 180 degrees it was on the 360 and less degrees.