Chocolade19721309

Untitled

Jun 3rd, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using TMPro;
  4. using System.Collections;
  5. using System;
  6.  
  7. [RequireComponent(typeof(Animator))]
  8. public class IKControl : MonoBehaviour
  9. {
  10. public List<Transform> lookObjects = new List<Transform>();
  11. public float weightDamping = 1.5f;
  12. private Animator animator;
  13. private Transform lastPrimaryTarget;
  14. private float lerpEndDistance = 0.1f;
  15. private float finalLookWeight = 0;
  16. private bool transitionToNextTarget = false;
  17.  
  18. [Header("UI Reference")]
  19. public TextMeshProUGUI descriptionText; // Reference to the TextMeshProUGUI component
  20. public GameObject descriptionPanel; // Reference to the UI Panel
  21.  
  22. void Start()
  23. {
  24. animator = GetComponent<Animator>();
  25. if (descriptionPanel != null)
  26. {
  27. descriptionPanel.SetActive(false); // Hide the panel initially
  28. }
  29. }
  30.  
  31. void OnAnimatorIK()
  32. {
  33. if (lookObjects.Count > 0)
  34. {
  35. Transform primaryTarget = null;
  36. float closestLookWeight = 0;
  37.  
  38. foreach (Transform target in lookObjects)
  39. {
  40. InteractableItem interactableItem = target.GetComponent<InteractableItem>();
  41. if (interactableItem == null) continue;
  42.  
  43. if(!interactableItem.isInteractable)
  44. {
  45. continue;
  46. }
  47.  
  48. float distance = Vector3.Distance(transform.position, target.position);
  49. if (distance > interactableItem.interactionDistance) continue;
  50.  
  51. Vector3 lookAt = target.position - transform.position;
  52. lookAt.y = 0f;
  53. float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
  54. float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
  55. float someThreshold = 0.8f; // Adjust as needed (closer to 1 for stricter gaze requirement)
  56. if (lookWeight > closestLookWeight && lookWeight > someThreshold)
  57. {
  58. closestLookWeight = lookWeight;
  59. primaryTarget = target;
  60. }
  61. }
  62.  
  63. if (primaryTarget != null)
  64. {
  65. if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
  66. {
  67. transitionToNextTarget = true;
  68. }
  69. }
  70.  
  71. if ((primaryTarget != null) && !transitionToNextTarget)
  72. {
  73. lastPrimaryTarget = primaryTarget;
  74. finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
  75. float bodyWeight = finalLookWeight * 0.75f;
  76. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  77. animator.SetLookAtPosition(primaryTarget.position);
  78.  
  79. InteractableItem interactableItem = primaryTarget.GetComponent<InteractableItem>();
  80. if (interactableItem != null)
  81. {
  82. if (interactableItem.IsAction())
  83. {
  84. // Implement the action logic here, e.g., triggering an animation or interaction
  85. Debug.Log("Performing action: " + interactableItem.description);
  86. }
  87. else
  88. {
  89. // Display description logic
  90. DisplayDescription(interactableItem.description);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. // Clear the description text when not looking at any target
  97. ClearDescription();
  98. }
  99.  
  100. if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
  101. {
  102. finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
  103. float bodyWeight = finalLookWeight * 0.75f;
  104. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  105. animator.SetLookAtPosition(lastPrimaryTarget.position);
  106. if (finalLookWeight < lerpEndDistance)
  107. {
  108. transitionToNextTarget = false;
  109. finalLookWeight = 0f;
  110. lastPrimaryTarget = null;
  111. // Clear the description text when transitioning to no target
  112. ClearDescription();
  113. }
  114. }
  115. }
  116. else
  117. {
  118. // Clear the description text if no objects are in the lookObjects list
  119. ClearDescription();
  120. }
  121. }
  122.  
  123. private void DisplayDescription(string description)
  124. {
  125. if (descriptionText != null && descriptionPanel != null)
  126. {
  127. descriptionPanel.SetActive(true);
  128. descriptionText.text = description;
  129. }
  130. }
  131.  
  132. private void ClearDescription()
  133. {
  134. if (descriptionText != null && descriptionPanel != null)
  135. {
  136. descriptionText.text = "";
  137. descriptionPanel.SetActive(false);
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment