- Home /
Question by
Wesley21spelde · Mar 03 at 10:57 AM ·
raycastikplacementfoot
Foot_IK Problems (Foot Placement Raycast
hi guys i have a question aboute this IK system i did follow a tutorial for this script but i am running in to a problem The IK Works but not correct i added a Debug.Draw so can see where the ray`s are and i seems that the Ik only works when the hit.transform.tag == "Walkable" hits the middle of the raycast so i basicaly starts the Ik on my players knee when it hits the tagged object does enyone know a sollution like am other offset or somthing? here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Foot_Ik : MonoBehaviour
{
public Animator anim;
public LayerMask layerMask; // Select all layers that foot placement applies to.
[Range(0, 1f)]
public float DistanceToGround; // Distance from where the foot transform is to the lowest possible position of the foot.
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.M))
anim.SetBool("Walk", !anim.GetBool("Walk"));
}
public void OnAnimatorIK(int layerIndex)
{
if (anim)
{ // Only carry out the following code if there is an Animator set.
// Set the weights of left and right feet to the current value defined by the curve in our animations.
anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, anim.GetFloat("IKLeftFootWeight"));
anim.SetIKRotationWeight(AvatarIKGoal.LeftFoot, anim.GetFloat("IKLeftFootWeight"));
anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, anim.GetFloat("IKRightFootWeight"));
anim.SetIKRotationWeight(AvatarIKGoal.RightFoot, anim.GetFloat("IKRightFootWeight"));
// Left Foot
// We cast our ray from above the foot in case the current terrain/floor is above the foot position.
Ray ray = new Ray(anim.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hit, DistanceToGround + 1f, layerMask))
{
Debug.DrawRay(anim.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down, Color.green);
// We're only concerned with objects that are tagged as "Walkable"
if (hit.transform.tag == "Walkable")
{
Debug.Log("raycast");
Vector3 footPosition = hit.point; // The target foot position is where the raycast hit a walkable object...
footPosition.y += DistanceToGround; // ... taking account the distance to the ground we added above.
anim.SetIKPosition(AvatarIKGoal.LeftFoot, footPosition);
anim.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.LookRotation(transform.forward, hit.normal));
Debug.Log("we did send out a raycast Left Foot");
print("Hit");
}
}
// Right Foot
ray = new Ray(anim.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down);
if (Physics.Raycast(ray, out hit, DistanceToGround + 1f, layerMask))
{
Debug.DrawRay(anim.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down, Color.blue);
if (hit.transform.tag == "Walkable")
{
Vector3 footPosition = hit.point;
footPosition.y += DistanceToGround;
anim.SetIKPosition(AvatarIKGoal.RightFoot, footPosition);
anim.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.LookRotation(transform.forward, hit.normal));
Debug.Log("we did send out a raycast Right Foot");
print("Hit");
}
}
}
}
}
naamloos-3.jpg
(252.7 kB)
Comment