Advertisement
Vazili_KZ

Foot IK

Mar 9th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. namespace AGD.Core.IK
  7. {
  8.     /// <summary>
  9.     /// To be placed on a character in order to add a foot IK grounding behaviour
  10.     /// Requires an Animator with a Layer set to IK Pass
  11.     /// </summary>
  12.     [RequireComponent(typeof(Animator))]
  13.     public class FootIK : MonoBehaviour
  14.     {
  15.         private Animator _animator;
  16.  
  17.         [Tooltip("Set to false and this script won't run.")]
  18.         [SerializeField] private bool applyFootIK = true;
  19.        
  20.         [Tooltip("Select the layer to which the foot IK will be applied")]
  21.         [SerializeField] private LayerMask targetLayer;
  22.         [Tooltip("the distance from the IK Foot Transform to the lowest point of the foot")]
  23.         [Range(0,1)][SerializeField] private float distanceToGround = 0;
  24.        
  25.  
  26.         private void Start()
  27.         {
  28.             _animator = GetComponent<Animator>();
  29.         }
  30.  
  31.         private void OnAnimatorIK(int layerIndex)
  32.         {
  33.             if (!applyFootIK) return;
  34.             if (!_animator) return;
  35.             // Set the weights of left foot to the current value defined by the curve in the animations.
  36.             _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, _animator.GetFloat("IKLeftFootWeight"));
  37.             _animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, _animator.GetFloat("IKLeftFootWeight"));
  38.            
  39.             //Left Foot
  40.             RaycastHit hitInfo;
  41.             //a ray starting from 1 unit above the IK Foot transform, and going downward on the Y Axis
  42.             Ray ray = new Ray(_animator.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down);
  43.             Debug.DrawRay(_animator.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down * (1f + distanceToGround), Color.red);
  44.  
  45.             if (Physics.Raycast(ray, out hitInfo, 1f + distanceToGround, targetLayer))
  46.             {
  47.                 var footPosition = hitInfo.point; //The point in space where the ray hit the targetLayer ie: where the foot should be placed.
  48.                 footPosition.y += distanceToGround; //lifting the foot back up to compensate for the distanceToGround used above.
  49.                 _animator.SetIKPosition(AvatarIKGoal.LeftFoot, footPosition);
  50.                 //Rotating the foot in a direction so that Vector3.up's rotation matches the hit surface's normal rotation.
  51.                 _animator.SetIKRotation(AvatarIKGoal.LeftFoot, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));
  52.             }
  53.            
  54.             // Set the weights of left foot to the current value defined by the curve in the animations.
  55.             _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, _animator.GetFloat("IKRightFootWeight"));
  56.             _animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, _animator.GetFloat("IKRightFootWeight"));
  57.            
  58.             //Right Foot
  59.             //a ray starting from 1 unit above the IK Foot transform, and going downward on the Y Axis
  60.             ray = new Ray(_animator.GetIKPosition(AvatarIKGoal.RightFoot) + Vector3.up, Vector3.down);
  61.             Debug.DrawRay(_animator.GetIKPosition(AvatarIKGoal.LeftFoot) + Vector3.up, Vector3.down * (1f + distanceToGround), Color.red);
  62.  
  63.             if (Physics.Raycast(ray, out hitInfo, 1f + distanceToGround, targetLayer))
  64.             {
  65.                 var footPosition = hitInfo.point; //The point in space where the ray hit the targetLayer ie: where the foot should be placed.
  66.                 footPosition.y += distanceToGround; //lifting the foot back up to compensate for the distanceToGround used above.
  67.                
  68.                 _animator.SetIKPosition(AvatarIKGoal.RightFoot, footPosition);
  69.                 //Rotating the foot in a direction so that Vector3.up's rotation matches the hit surface's normal rotation.
  70.                 _animator.SetIKRotation(AvatarIKGoal.RightFoot, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));
  71.             }
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement