Advertisement
Guest User

Ball

a guest
Dec 15th, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4.  
  5. public class Ball : MonoBehaviour
  6. {
  7.  
  8. #region Private_Variables
  9. // Use this for initialization
  10. private bool isInFloor = false;
  11. private Rigidbody rigid;
  12. private bool isThrowed = false;
  13. private Vector3 initialPosition;
  14. private Quaternion initialRotation;
  15. public Vector3 initialScale;
  16. private IEnumerator jumpCoroutine;
  17. private bool isRotate = false;
  18. private Transform childBall;
  19. private bool isCurve = false;
  20. private float curveFactore = 0f;
  21. private float throwPos = 0f;
  22. private float windFactor = .5f;
  23. private bool isWind = false;
  24. private bool isNormalThrow = false;
  25. private bool isRotateLeft = true;
  26. private bool isParticleFollowBall = false;
  27. public float objectScale = 1.0f;
  28. public float moveSpeed;
  29. Camera cam;
  30.  
  31. GameObject _gameSoloManager;
  32.  
  33.  
  34.  
  35. #endregion
  36.  
  37.  
  38.  
  39. #region Unity_Callbacks
  40.  
  41.  
  42. void Start()
  43. {
  44.  
  45. rigid = GetComponent<Rigidbody>();
  46. //initalize gradually jump coroutine
  47. jumpCoroutine = GraduallyJumpCoroutine(0.4f);
  48.  
  49. childBall = transform.Find("Ball");
  50.  
  51. StartCoroutine(jumpCoroutine);
  52.  
  53.  
  54. // record initial scale, use this as a basis
  55. initialScale = transform.localScale;
  56.  
  57.  
  58. if (_gameSoloManager == null)
  59. _gameSoloManager = ThrowBall.Instance._gameSoloManager;
  60.  
  61. // if no specific camera, grab the default camera
  62. if (cam == null)
  63. cam = ThrowBall.Instance.cam;
  64. }
  65.  
  66. void Update()
  67. {
  68.  
  69. //set Scale as per plane distance
  70. if (!isThrowed)
  71. {
  72. Plane plane = new Plane(cam.transform.forward, cam.transform.position);
  73. float dist = plane.GetDistanceToPoint(transform.position);
  74. transform.localScale = initialScale * dist * objectScale;
  75. }
  76.  
  77. //Rotate Ball When Dragg
  78. if (isRotate)
  79. {
  80. if (isRotateLeft)
  81. childBall.Rotate(Vector3.right * Time.deltaTime * 660);
  82. else
  83. childBall.Rotate(-Vector3.right * Time.deltaTime * 660);
  84. }
  85.  
  86. }
  87.  
  88. void FixedUpdate()
  89. {
  90.  
  91. //apply wind after some distance ball gone
  92. float dist = 0f;
  93. if (ThrowBall.Instance.target)
  94. {
  95. dist = (ThrowBall.Instance.target.position.z - transform.position.z);
  96. }
  97.  
  98. //if throw is cureve then and then only apply wind
  99. if (isCurve && dist <= (throwPos - (throwPos / 9.5f)))
  100. {
  101. if (!isWind)
  102. StartCoroutine(wind(0.5f));
  103. transform.Translate(Vector3.right * -curveFactore * windFactor * Time.deltaTime);
  104. }
  105. }
  106.  
  107. void OnEnable()
  108. {
  109. //set default position when object enable
  110. initialPosition = transform.position;
  111. //initialPosition = cam.transform.position + cam.transform.forward * ThrowBall.Instance.distance + cam.transform.up * ThrowBall.Instance.down;
  112. }
  113.  
  114.  
  115. void OnCollisionEnter(Collision colliderInfo)
  116. {
  117.  
  118. //if object will not hit directly to target
  119. if (isInFloor)
  120. {
  121. if (colliderInfo.gameObject.CompareTag("target"))
  122. {
  123. isCurve = false;
  124. }
  125.  
  126. }
  127.  
  128. //if object hit directly to taget
  129. if (!isInFloor)
  130. {
  131.  
  132. if (colliderInfo.gameObject.CompareTag("floor"))
  133. {
  134. isInFloor = true;
  135. StartCoroutine(stop());
  136.  
  137. }
  138.  
  139. //if hit target
  140. if (colliderInfo.gameObject.CompareTag("target"))
  141. {
  142.  
  143. isInFloor = true;
  144.  
  145. //colliderInfo.gameObject.SetActive(false);
  146.  
  147. _gameSoloManager.GetComponent<GameSoloManager>().CansHit();
  148. _gameSoloManager.GetComponent<GameSoloManager>().AddMoney();
  149. PlayerPrefs.Save();
  150.  
  151. }
  152.  
  153. }
  154. }
  155.  
  156. #endregion
  157. #region Private_Methods
  158. /// <summary>
  159. /// Sets the is throwed.
  160. /// </summary>
  161. /// <param name="flag">If set to <c>true</c> flag.</param>
  162. private void SetIsThrowed(bool flag)
  163. {
  164. if (jumpCoroutine != null)
  165. {
  166. StopCoroutine(jumpCoroutine);
  167. jumpCoroutine = null;
  168. }
  169. isThrowed = flag;
  170. //if object was getting throwing direction then don't rotate
  171. if (ThrowBall.Instance.IsGettingDirection)
  172. isRotate = false;
  173. else
  174. isRotate = true;
  175.  
  176. throwPos = (ThrowBall.Instance.target.position.z - transform.position.z);
  177.  
  178. }
  179.  
  180. /// <summary>
  181. /// Resets the ball.
  182. /// </summary>
  183. public void ResetBall()
  184. {
  185. isThrowed = false;
  186. StopAllCoroutines();
  187.  
  188. ////ball move to initial position
  189. StartCoroutine(MoveBackToInitialPositionCoroutine(0.5f));
  190. }
  191.  
  192. /// <summary>
  193. /// Sets the curve.
  194. /// </summary>
  195. /// <param name="cFactore">C factore.</param>
  196. private void SetCurve(float cFactore)
  197. {
  198. curveFactore = cFactore;
  199. isCurve = true;
  200. }
  201.  
  202. /// <summary>
  203. /// Sets the normal throw.
  204. /// </summary>
  205. /// <param name="cFactore">C factore.</param>
  206. private void SetNormalThrow(float cFactore)
  207. {
  208. curveFactore = cFactore;
  209. isNormalThrow = true;
  210. }
  211. #endregion
  212.  
  213. #region Public_Methods
  214. #endregion
  215.  
  216. #region Properties
  217. #endregion
  218.  
  219. #region Coroutine
  220.  
  221. /// <summary>
  222. /// Wind the specified t.
  223. /// </summary>
  224. /// <param name="t">T.</param>
  225. IEnumerator wind(float t)
  226. {
  227. isWind = true;
  228. float rate = 1.0f / t;
  229. float i = 0f;
  230. while (i < 1.0f)
  231. {
  232. i += rate * Time.deltaTime;
  233. windFactor = Mathf.Lerp(1, 26, i);
  234. yield return 0;
  235. }
  236. }
  237.  
  238. /// <summary>
  239. /// Winds the reverse.
  240. /// </summary>
  241. /// <returns>The reverse.</returns>
  242. /// <param name="t">T.</param>
  243. IEnumerator windReverse(float t)
  244. {
  245. isWind = true;
  246. float rate = 1.0f / t;
  247. float i = 0f;
  248. while (i < 1.0f)
  249. {
  250. i += rate * Time.deltaTime;
  251. windFactor = Mathf.Lerp(26, 0, i);
  252. //Debug.Log("reverse:"+windFactor);
  253. yield return 0;
  254. }
  255. //isWind=false;
  256. }
  257.  
  258. /// <summary>
  259. /// Stop this instance.
  260. /// </summary>
  261. IEnumerator stop()
  262. {
  263. isRotate = false;
  264. yield return new WaitForSeconds(0.5f);
  265. Destroy(gameObject);
  266.  
  267. }
  268.  
  269. /// <summary>
  270. /// Graduallies the jump coroutine.
  271. /// </summary>
  272. /// <returns>The jump coroutine.</returns>
  273. /// <param name="tm">Tm.</param>
  274. IEnumerator GraduallyJumpCoroutine(float tm)
  275. {
  276.  
  277. while (!isThrowed)
  278. {
  279.  
  280. yield return new WaitForSeconds(0.5f);
  281.  
  282. initialPosition = cam.transform.position + cam.transform.forward * ThrowBall.Instance.distance + cam.transform.up * ThrowBall.Instance.down;
  283. Debug.Log(initialPosition);
  284.  
  285. if (ThrowBall.Instance.IsGameStart)
  286. {
  287.  
  288.  
  289. isRotateLeft = !isRotateLeft;
  290. isRotate = true;
  291. float i = 0f;
  292. float rate = 1.0f / tm;
  293. Vector3 from = initialPosition;
  294. Vector3 to = new Vector3(from.x, from.y + 0.02f, from.z);
  295.  
  296. while (i < 1.0f)
  297. {
  298. i += rate * Time.deltaTime;
  299. transform.position = Vector3.Lerp(from, to, i);
  300. //transform.localScale = Vector3.Lerp (fromScale, toScale, i);
  301. yield return 0f;
  302. }
  303.  
  304.  
  305. i = 0f;
  306. rate = 1.0f / (tm / 0.7f);
  307.  
  308. Vector3 bump = from;
  309. bump.y -= 0.02f;
  310.  
  311. while (i < 1.0f)
  312. {
  313. i += rate * Time.deltaTime;
  314. transform.position = Vector3.Lerp(to, bump, i);
  315. // transform.localScale = Vector3.Lerp (toScale, fromScale, i);
  316. yield return 0f;
  317. }
  318.  
  319. isRotate = false;
  320.  
  321. i = 0f;
  322. rate = 1.0f / (tm / 1.1f);
  323.  
  324. while (i < 1.0f)
  325. {
  326. i += rate * Time.deltaTime;
  327. transform.position = Vector3.Lerp(bump, from, i);
  328. yield return 0f;
  329. }
  330.  
  331.  
  332.  
  333. }
  334. }
  335. }
  336.  
  337. /// <summary>
  338. /// Moves the back to initial position coroutine.
  339. /// </summary>
  340. /// <returns>The back to initial position coroutine.</returns>
  341. /// <param name="tm">Tm.</param>
  342. IEnumerator MoveBackToInitialPositionCoroutine(float tm)
  343. {
  344. float i = 0f;
  345. float rate = 1.0f / tm;
  346. Vector3 from = transform.position;
  347. Vector3 to = initialPosition;
  348. while (i < 1.0f)
  349. {
  350. i += rate * Time.deltaTime;
  351. transform.position = Vector3.Lerp(from, to, i);
  352. yield return 0f;
  353. }
  354.  
  355. transform.position = initialPosition;
  356. childBall.localRotation = Quaternion.identity;
  357. isRotate = false;
  358.  
  359. //initalize gradually jump coroutine
  360. jumpCoroutine = GraduallyJumpCoroutine(0.3f);
  361.  
  362. StartCoroutine(jumpCoroutine);
  363. }
  364. #endregion
  365.  
  366. #region RPC
  367. #endregion
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement