Advertisement
Guest User

Ball

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