Chocolade19721309

Untitled

Jun 3rd, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 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. if (lookWeight > closestLookWeight)
  56. {
  57. closestLookWeight = lookWeight;
  58. primaryTarget = target;
  59. }
  60. }
  61.  
  62. if (primaryTarget != null)
  63. {
  64. if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
  65. {
  66. transitionToNextTarget = true;
  67. }
  68. }
  69.  
  70. if ((primaryTarget != null) && !transitionToNextTarget)
  71. {
  72. lastPrimaryTarget = primaryTarget;
  73. finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
  74. float bodyWeight = finalLookWeight * 0.75f;
  75. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  76. animator.SetLookAtPosition(primaryTarget.position);
  77.  
  78. InteractableItem interactableItem = primaryTarget.GetComponent<InteractableItem>();
  79. if (interactableItem != null)
  80. {
  81. if (interactableItem.IsAction())
  82. {
  83. // Implement the action logic here, e.g., triggering an animation or interaction
  84. Debug.Log("Performing action: " + interactableItem.description);
  85. }
  86. else
  87. {
  88. // Display description logic
  89. DisplayDescription(interactableItem.description);
  90. }
  91. }
  92. }
  93. else
  94. {
  95. // Clear the description text when not looking at any target
  96. ClearDescription();
  97. }
  98.  
  99. if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
  100. {
  101. finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
  102. float bodyWeight = finalLookWeight * 0.75f;
  103. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  104. animator.SetLookAtPosition(lastPrimaryTarget.position);
  105. if (finalLookWeight < lerpEndDistance)
  106. {
  107. transitionToNextTarget = false;
  108. finalLookWeight = 0f;
  109. lastPrimaryTarget = null;
  110. // Clear the description text when transitioning to no target
  111. ClearDescription();
  112. }
  113. }
  114. }
  115. else
  116. {
  117. // Clear the description text if no objects are in the lookObjects list
  118. ClearDescription();
  119. }
  120. }
  121.  
  122. private void DisplayDescription(string description)
  123. {
  124. if (descriptionText != null && descriptionPanel != null)
  125. {
  126. descriptionPanel.SetActive(true);
  127. descriptionText.text = description;
  128. }
  129. }
  130.  
  131. private void ClearDescription()
  132. {
  133. if (descriptionText != null && descriptionPanel != null)
  134. {
  135. descriptionText.text = "";
  136. descriptionPanel.SetActive(false);
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment