Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TestLegLift : MonoBehaviour
- {
- [SerializeField] Vector3 _leftFootTarget;
- [SerializeField] Vector3 _rightFootTarget;
- [Range(0f, 1f)]
- [SerializeField] float _IKWeight = 0f;
- Animator _animator;
- [Header("Detection Settings")]
- [SerializeField] private Vector3 _boxSize = new Vector3(0.5f, 0.5f, 0.5f);
- [SerializeField] private float _detectionDistance = 2.0f;
- [SerializeField] private LayerMask _obstacleLayer;
- [SerializeField] private float _lerpSpeed = 5f;
- private float _targetIKWeight = 0f;
- private void Start()
- {
- _animator = GetComponent<Animator>();
- StartCoroutine(CheckLegElevation());
- }
- private IEnumerator CheckLegElevation()
- {
- while(true)
- {
- Vector3 direction = transform.forward;
- Vector3 origin = transform.position + Vector3.up * 0.5f; // Adjust the origin height as needed
- bool hitObstacle = Physics.BoxCast(origin - direction * _boxSize.z, _boxSize / 2, direction, out RaycastHit hitInfo, Quaternion.LookRotation(direction), _detectionDistance, _obstacleLayer);
- _targetIKWeight = hitObstacle ? 1f : 0f;
- yield return new WaitForSeconds(0.1f);
- }
- }
- private void OnDrawGizmos()
- {
- Vector3 direction = transform.forward;
- Vector3 origin = transform.position + Vector3.up * 0.5f;
- // Draw the starting position
- Gizmos.color = Color.green;
- Gizmos.DrawWireCube(origin - direction * _boxSize.z, _boxSize);
- // Draw the end position
- Gizmos.color = Color.red;
- Gizmos.DrawWireCube(origin + direction * _detectionDistance, _boxSize);
- // Draw the connection lines to show the "tunnel"
- Gizmos.color = Color.yellow;
- Gizmos.DrawLine(origin, origin + direction * _detectionDistance);
- }
- private void OnAnimatorIK(int layerIndex)
- {
- _IKWeight = Mathf.MoveTowards(_IKWeight, _targetIKWeight, Time.deltaTime * _lerpSpeed);
- if (_IKWeight > 0)
- {
- _animator.SetIKPosition(AvatarIKGoal.LeftFoot, _leftFootTarget);
- _animator.SetIKPosition(AvatarIKGoal.RightFoot, _rightFootTarget);
- }
- _animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, _IKWeight);
- _animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, _IKWeight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment