DugganSC

Untitled

Jun 30th, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TestLegLift : MonoBehaviour
  6. {
  7.     [SerializeField] Vector3 _leftFootTarget;
  8.     [SerializeField] Vector3 _rightFootTarget;
  9.  
  10.     [Range(0f, 1f)]
  11.     [SerializeField] float _IKWeight = 0f;
  12.  
  13.     Animator _animator;
  14.  
  15.     [Header("Detection Settings")]
  16.     [SerializeField] private Vector3 _boxSize = new Vector3(0.5f, 0.5f, 0.5f);
  17.     [SerializeField] private float _detectionDistance = 2.0f;
  18.     [SerializeField] private LayerMask _obstacleLayer;
  19.     [SerializeField] private float _lerpSpeed = 5f;
  20.     private float _targetIKWeight = 0f;
  21.  
  22.     private void Start()
  23.     {
  24.         _animator = GetComponent<Animator>();
  25.  
  26.         StartCoroutine(CheckLegElevation());
  27.     }
  28.  
  29.     private IEnumerator CheckLegElevation()
  30.     {
  31.         while(true)
  32.         {
  33.             Vector3 direction = transform.forward;
  34.             Vector3 origin = transform.position + Vector3.up * 0.5f; // Adjust the origin height as needed
  35.  
  36.             bool hitObstacle = Physics.BoxCast(origin - direction * _boxSize.z, _boxSize / 2, direction, out RaycastHit hitInfo, Quaternion.LookRotation(direction), _detectionDistance, _obstacleLayer);
  37.  
  38.             _targetIKWeight = hitObstacle ? 1f : 0f;
  39.  
  40.             yield return new WaitForSeconds(0.1f);
  41.         }
  42.     }
  43.  
  44.     private void OnDrawGizmos()
  45.     {
  46.         Vector3 direction = transform.forward;
  47.         Vector3 origin = transform.position + Vector3.up * 0.5f;
  48.  
  49.         // Draw the starting position
  50.         Gizmos.color = Color.green;
  51.         Gizmos.DrawWireCube(origin - direction * _boxSize.z, _boxSize);
  52.  
  53.         // Draw the end position
  54.         Gizmos.color = Color.red;
  55.         Gizmos.DrawWireCube(origin + direction * _detectionDistance, _boxSize);
  56.  
  57.         // Draw the connection lines to show the "tunnel"
  58.         Gizmos.color = Color.yellow;
  59.         Gizmos.DrawLine(origin, origin + direction * _detectionDistance);
  60.     }
  61.  
  62.     private void OnAnimatorIK(int layerIndex)
  63.     {
  64.         _IKWeight = Mathf.MoveTowards(_IKWeight, _targetIKWeight, Time.deltaTime * _lerpSpeed);
  65.         if (_IKWeight > 0)
  66.         {
  67.             _animator.SetIKPosition(AvatarIKGoal.LeftFoot, _leftFootTarget);
  68.             _animator.SetIKPosition(AvatarIKGoal.RightFoot, _rightFootTarget);
  69.         }
  70.  
  71.         _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, _IKWeight);
  72.         _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, _IKWeight);
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment