Guest User

Untitled

a guest
Jul 11th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using TMPro;
  8.  
  9. [RequireComponent(typeof(Animator))]
  10. public class IKControl : MonoBehaviour
  11. {
  12. public List<InteractableItem> lookObjs = new List<InteractableItem>();
  13. public TextMeshProUGUI text;
  14. public float weightDamping = 1.5f;
  15. public bool RightHandToTarget = true;
  16. public float throwSpeed;
  17. public bool handFinishedMove = false;
  18. public MoveNavi moveNavi;
  19. public GameObject descriptionTextImage;
  20. public GameObject naviParent;
  21. public DimLights dimLights;
  22. public Light[] naviLights;
  23. public bool startMovingNAVI = false;
  24. public InteractableItem target;
  25. public bool toTarget = false;
  26. public bool hasSent = false;
  27. public GameObject navi;
  28. public InteractableItem leftHand;
  29.  
  30. private List<InteractableItem> allDetectedItems;
  31. private Animator animator;
  32. private InteractableItem lastPrimaryTarget;
  33. private float lerpEndDistance = 0.1f;
  34. private float finalLookWeight = 0;
  35. private bool transitionToNextTarget = false;
  36. private List<string> objectsNames = new List<string>();
  37. private bool setNaviRotation = false;
  38.  
  39. private void Awake()
  40. {
  41.  
  42. }
  43.  
  44. void Start()
  45. {
  46. animator = GetComponent<Animator>();
  47. allDetectedItems = new List<InteractableItem>();
  48.  
  49. dimLights.lightsToDim = naviLights;
  50. }
  51.  
  52. // Callback for calculating IK
  53. void OnAnimatorIK()
  54. {
  55. if (lookObjs != null)
  56. {
  57. lookObjs.RemoveAll(x => x == null);
  58.  
  59. InteractableItem primaryTarget = null;
  60.  
  61. float closestLookWeight = 0;
  62.  
  63. // Here we find the target which is closest (by angle) to the players view line
  64. allDetectedItems.Clear();
  65. foreach (InteractableItem target in lookObjs)
  66. {
  67. Vector3 lookAt = target.transform.position - transform.position;
  68. lookAt.y = 0f;
  69.  
  70. // Filter out all objects that are too far away
  71. if (lookAt.magnitude > target.distance) continue;
  72.  
  73. float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
  74. float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
  75. if (lookWeight > 0.1f && lookWeight > closestLookWeight)
  76. {
  77. closestLookWeight = lookWeight;
  78. primaryTarget = target;
  79. }
  80.  
  81. allDetectedItems.Add(target);
  82. }
  83.  
  84. if (allDetectedItems.Count == 2)
  85. {
  86. objectsNames = allDetectedItems.Select(item => item.transform.name).ToList();
  87. if (objectsNames.Contains("NAVI") && objectsNames.Contains("Security Keypad Interactable"))
  88. {
  89. InteractableItem navi = allDetectedItems.Single(item => item.transform.name == "NAVI");
  90. InteractableItem securitykeypad = allDetectedItems.Single(item => item.transform.name == "Security Keypad Interactable");
  91. var crate = GameObject.Find("Crate_0_0");
  92.  
  93. if (crate.gameObject.GetComponent<UnlockCrate>().HasOpened())
  94. {
  95. primaryTarget = navi;
  96. target = primaryTarget;
  97. startMovingNAVI = true;
  98. }
  99. else
  100. {
  101. primaryTarget = securitykeypad;
  102. }
  103. }
  104. }
  105.  
  106. InteractWithTarget(primaryTarget, closestLookWeight);
  107. }
  108. }
  109.  
  110. private void InteractWithTarget(InteractableItem primaryTarget, float closestLookWeight)
  111. {
  112. if (primaryTarget != null)
  113. {
  114. if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
  115. {
  116. // Here we start a new transition because the player looks already to a target but
  117. // we have found another target the player should look at
  118. transitionToNextTarget = true;
  119. }
  120. }
  121.  
  122. // The player is in a neutral look position but has found a new target
  123. if ((primaryTarget != null) && !transitionToNextTarget)
  124. {
  125. if (primaryTarget.IsAnyAction())
  126. {
  127. RightHandToTarget = true;
  128. }
  129.  
  130. lastPrimaryTarget = primaryTarget;
  131. finalLookWeight = Mathf.Lerp(finalLookWeight, 1f, Time.deltaTime * weightDamping);
  132. float bodyWeight = finalLookWeight * .1f;
  133. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  134. animator.SetLookAtPosition(primaryTarget.transform.position);
  135.  
  136. if (RightHandToTarget && primaryTarget.IsAnyAction())
  137. {
  138. Vector3 relativePos = primaryTarget.transform.position - transform.position;
  139. Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
  140.  
  141. if (primaryTarget.interactableMode == InteractableItem.InteractableMode.ActionWithoutThrow)
  142. {
  143. animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
  144. animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
  145. animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 1f * closestLookWeight);
  146. animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
  147. }
  148.  
  149. if (primaryTarget.interactableMode == InteractableItem.InteractableMode.Action)
  150. {
  151. animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
  152. animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
  153. animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.1f * closestLookWeight);
  154. animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
  155. }
  156.  
  157. // -> new code block
  158. if (finalLookWeight > 0.95f) // here you can play with a value between 0.95f -> 1.0f
  159. {
  160. if (primaryTarget.interactableMode == InteractableItem.InteractableMode.Action
  161. && hasSent == false)
  162. {
  163. target = primaryTarget;
  164. toTarget = true;
  165. hasSent = true;
  166. primaryTarget.description = "";
  167. // Here I need to find where to make hasSent false again.
  168. }
  169. }
  170. else
  171. {
  172. hasSent = false;
  173. }
  174.  
  175. if (finalLookWeight > 0.9f)
  176. {
  177. handFinishedMove = true;
  178. }
  179. }
  180. }
  181.  
  182. // Let the player smoothly look away from the last target to the neutral look position
  183. if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
  184. {
  185. finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
  186. float bodyWeight = finalLookWeight * .75f;
  187. animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
  188. animator.SetLookAtPosition(lastPrimaryTarget.transform.position);
  189.  
  190. if (RightHandToTarget)
  191. {
  192. Vector3 relativePos = lastPrimaryTarget.transform.position - transform.position;
  193. Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
  194. animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
  195. animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
  196. animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.5f * closestLookWeight);
  197. animator.SetIKPosition(AvatarIKGoal.RightHand, lastPrimaryTarget.transform.position);
  198. }
  199.  
  200. if (finalLookWeight < lerpEndDistance)
  201. {
  202. transitionToNextTarget = false;
  203. finalLookWeight = 0f;
  204. lastPrimaryTarget = null;
  205. transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
  206. }
  207. }
  208.  
  209. // Show primary object found by the player
  210. if (primaryTarget != null)
  211. {
  212. if (primaryTarget.description != "")
  213. {
  214. descriptionTextImage.SetActive(true);
  215. text.text = primaryTarget.description;
  216. }
  217. }
  218. else
  219. {
  220. text.text = "";
  221. descriptionTextImage.SetActive(false);
  222. }
  223. }
  224.  
  225. private void Update()
  226. {
  227. if(MenuController.LoadSceneForSavedGame == true && setNaviRotation == false)
  228. {
  229. transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
  230. setNaviRotation = true;
  231. }
  232. }
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment