Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using System.Collections;
- using System;
- [RequireComponent(typeof(Animator))]
- public class IKControl : MonoBehaviour
- {
- public List<Transform> lookObjects = new List<Transform>();
- public float weightDamping = 1.5f;
- private Animator animator;
- private Transform lastPrimaryTarget;
- private float lerpEndDistance = 0.1f;
- private float finalLookWeight = 0;
- private bool transitionToNextTarget = false;
- [Header("UI Reference")]
- public TextMeshProUGUI descriptionText; // Reference to the TextMeshProUGUI component
- public GameObject descriptionPanel; // Reference to the UI Panel
- void Start()
- {
- animator = GetComponent<Animator>();
- if (descriptionPanel != null)
- {
- descriptionPanel.SetActive(false); // Hide the panel initially
- }
- }
- void OnAnimatorIK()
- {
- if (lookObjects.Count > 0)
- {
- Transform primaryTarget = null;
- float closestLookWeight = 0;
- foreach (Transform target in lookObjects)
- {
- InteractableItem interactableItem = target.GetComponent<InteractableItem>();
- if (interactableItem == null) continue;
- if(!interactableItem.isInteractable)
- {
- continue;
- }
- float distance = Vector3.Distance(transform.position, target.position);
- if (distance > interactableItem.interactionDistance) continue;
- Vector3 lookAt = target.position - transform.position;
- lookAt.y = 0f;
- float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
- float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
- if (lookWeight > closestLookWeight)
- {
- closestLookWeight = lookWeight;
- primaryTarget = target;
- }
- }
- if (primaryTarget != null)
- {
- if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
- {
- transitionToNextTarget = true;
- }
- }
- if ((primaryTarget != null) && !transitionToNextTarget)
- {
- lastPrimaryTarget = primaryTarget;
- finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
- float bodyWeight = finalLookWeight * 0.75f;
- animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
- animator.SetLookAtPosition(primaryTarget.position);
- InteractableItem interactableItem = primaryTarget.GetComponent<InteractableItem>();
- if (interactableItem != null)
- {
- if (interactableItem.IsAction())
- {
- // Implement the action logic here, e.g., triggering an animation or interaction
- Debug.Log("Performing action: " + interactableItem.description);
- }
- else
- {
- // Display description logic
- DisplayDescription(interactableItem.description);
- }
- }
- }
- else
- {
- // Clear the description text when not looking at any target
- ClearDescription();
- }
- if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
- {
- finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
- float bodyWeight = finalLookWeight * 0.75f;
- animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
- animator.SetLookAtPosition(lastPrimaryTarget.position);
- if (finalLookWeight < lerpEndDistance)
- {
- transitionToNextTarget = false;
- finalLookWeight = 0f;
- lastPrimaryTarget = null;
- // Clear the description text when transitioning to no target
- ClearDescription();
- }
- }
- }
- else
- {
- // Clear the description text if no objects are in the lookObjects list
- ClearDescription();
- }
- }
- private void DisplayDescription(string description)
- {
- if (descriptionText != null && descriptionPanel != null)
- {
- descriptionPanel.SetActive(true);
- descriptionText.text = description;
- }
- }
- private void ClearDescription()
- {
- if (descriptionText != null && descriptionPanel != null)
- {
- descriptionText.text = "";
- descriptionPanel.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment