Advertisement
Guest User

Das Script, danke

a guest
Apr 28th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Script which controls the ball
  5. /// </summary>
  6. [RequireComponent(typeof(Rigidbody))]
  7. public class BallControl : MonoBehaviour
  8. {
  9. public static BallControl instance;
  10.  
  11. [SerializeField] private LineRenderer lineRenderer; //reference to lineRenderer child object
  12. [SerializeField] private float MaxForce; //maximum force that an be applied to ball
  13. [SerializeField] private float forceModifier = 10000f; //multipliers of force
  14. [SerializeField] private GameObject areaAffector; //reference to sprite object which show area around ball to click
  15. [SerializeField] private LayerMask rayLayer; //layer allowed to be detected by ray
  16.  
  17. private float force; //actuale force which is applied to the ball
  18. private Rigidbody rgBody; //reference to rigidbody attached to this gameobject
  19. /// <summary>
  20. /// The below variables are used to decide the force to be applied to the ball
  21. /// </summary>
  22. private Vector3 startPos, endPos;
  23. private bool canShoot = false, ballIsStatic = true; //bool to make shooting stopping ball easy
  24. private Vector3 direction; //direction in which the ball will be shot
  25. public GameObject[] driveParticles;
  26. public float speedForDriveParticles;
  27. public float lookAtRotationSpeed;
  28. public WinConditions[] winConditions;
  29. public int currentWinCondition;
  30. public int winConditionsMet;
  31.  
  32. private void Awake()
  33. {
  34. if (instance == null)
  35. {
  36. instance = this;
  37. }
  38. else
  39. {
  40. Destroy(gameObject);
  41. }
  42.  
  43. rgBody = GetComponent<Rigidbody>(); //get reference to the rigidbody
  44. }
  45.  
  46. // Update is called once per frame
  47. void Update()
  48. {
  49. if(winConditions[currentWinCondition].objectsitNeedsToHit <= winConditionsMet)
  50. {
  51. LevelManager.instance.LevelComplete();
  52. }
  53. if (rgBody.velocity == Vector3.zero && !ballIsStatic) //if velocity is zero and ballIsStatic is false
  54. {
  55. ballIsStatic = true; //set ballIsStatic to true
  56. LevelManager.instance.ShotTaken(); //inform LevelManager of shot taken
  57. rgBody.angularVelocity = Vector3.zero; //set angular velocity to zero
  58. areaAffector.SetActive(true); //activate areaAffector
  59. }
  60. }
  61.  
  62. private void FixedUpdate()
  63. {
  64. if (canShoot) //if canSHoot is true
  65. {
  66. canShoot = false; //set canShoot to false
  67. ballIsStatic = false; //set ballIsStatic to false
  68. direction = new Vector3(startPos.x - endPos.x, 0, startPos.z - endPos.z); //get the direction between 2 vectors from start to end pos
  69.  
  70. rgBody.AddForce(direction * force, ForceMode.Impulse); //add force to the ball in given direction
  71. areaAffector.SetActive(false); //deactivate areaAffector
  72. UIManager.instance.PowerBar.fillAmount = 0; //reset the powerBar to zero
  73. force = 0; //reset the force to zero
  74. startPos = endPos = Vector3.zero; //reset the vectors to zero
  75. }
  76. if(rgBody.velocity.magnitude >= speedForDriveParticles)
  77. {
  78. for (int i = 0; i < driveParticles.Length; i++)
  79. {
  80. driveParticles[i].SetActive(true);
  81. }
  82. }
  83. if(rgBody.velocity.magnitude < speedForDriveParticles)
  84. {
  85. for (int i = 0; i < driveParticles.Length; i++)
  86. {
  87. driveParticles[i].SetActive(false);
  88. }
  89. }
  90. }
  91.  
  92. // Unity native Method to detect colliding objects
  93. private void OnTriggerEnter(Collider other)
  94. {
  95. if (other.name == "Destroyer") //if the object name is Destroyer
  96. {
  97. LevelManager.instance.LevelFailed(); //Level Failed
  98. }
  99. else if (other.name == "Hole") //if the object name is Hole
  100. {
  101. LevelManager.instance.LevelComplete(); //Level Complete
  102. }
  103. }
  104.  
  105. public void MouseDownMethod()
  106. {
  107. if(!ballIsStatic) return;
  108. startPos = ClickedPoint(); //get the vector in word space
  109. lineRenderer.gameObject.SetActive(true); //activate lineRenderer
  110. lineRenderer.SetPosition(0, lineRenderer.transform.localPosition); //set its 1st position
  111. }
  112.  
  113. public void MouseNormalMethod()
  114. {
  115. if(!ballIsStatic) return;
  116.  
  117. endPos = ClickedPoint();
  118. //transform.LookAt(new Vector3(0, -endPos.y, ));
  119. //transform.rotation *= Quaternion.Euler(1, 0, 1);
  120. var lookPos = -endPos - transform.position;
  121. lookPos.y = 0;
  122. var rotation = Quaternion.LookRotation(lookPos);
  123. transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * lookAtRotationSpeed);
  124. force = Mathf.Clamp(Vector3.Distance(endPos, startPos) * forceModifier, 0, MaxForce); //calculate the force
  125. UIManager.instance.PowerBar.fillAmount = force / MaxForce; //set the powerBar image fill amount
  126. //we convert the endPos to local pos for ball as lineRenderer is child of ball
  127. lineRenderer.SetPosition(1, transform.InverseTransformPoint(endPos)); //set its 1st position
  128. }
  129.  
  130. public void MouseUpMethod() //method called by InputManager
  131. {
  132. if(!ballIsStatic) return; //no mouse detection if ball is moving
  133. canShoot = true; //set canShoot true
  134. lineRenderer.gameObject.SetActive(false); //deactive lineRenderer
  135. }
  136.  
  137. /// <summary>
  138. /// Method used to convert the mouse position to the world position in respect to Level
  139. /// </summary>
  140. Vector3 ClickedPoint()
  141. {
  142. Vector3 position = Vector3.zero; //get a new Vector3 varialbe
  143. var ray = Camera.main.ScreenPointToRay(Input.mousePosition); //create a ray from camera in mouseposition direction
  144. RaycastHit hit = new RaycastHit(); //create a RaycastHit
  145. if (Physics.Raycast(ray, out hit, Mathf.Infinity, rayLayer)) //check for the hit
  146. {
  147. position = hit.point; //save the hit point in position
  148. }
  149. return position; //return position
  150. }
  151.  
  152. #if UNITY_EDITOR
  153.  
  154. private void OnDrawGizmosSelected()
  155. {
  156. Gizmos.color = Color.red;
  157. Gizmos.DrawWireSphere(transform.position, 1.5f);
  158. }
  159.  
  160. #endif
  161.  
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement