Advertisement
NomadicWarrior

Trajectory

Sep 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.43 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Trajectory : MonoBehaviour {
  7.  
  8. //Indicate whether or not the Launch angle should depend on the LaunchPosition's rotation.
  9. public bool FixedLaunchAngle = false;
  10. //Indicates whether or not the angle should be inverted.
  11. public bool InvertLaunchAngle = false;
  12.  
  13. public float LaunchAngle = 0;
  14. //Initial launch velocity
  15. public float LaunchSpeed = 25;
  16. //A transform that gives information about position and rotation of launch
  17. public Transform LaunchPosition;
  18. public bool Draw = true;
  19.  
  20. //Number of lines to be drawn to represent the trajectory
  21. public int LineCount = 5;
  22. //Size of each line
  23. public float LineLength = 0.5f;
  24. //Distance that will be covered by the preview.
  25. public float MaxDistance = 5;
  26. //Lines materials
  27. public Material PreviewMaterial;
  28. //Line scrolling speed
  29. public float ScrollSpeed = 1.5f;
  30.  
  31. private GameObject Lineparent;
  32.  
  33. public GameObject BulletPrefab;
  34. public Transform sp;
  35.  
  36. public PlayerController tankControl;
  37. public int layer = 2;
  38.  
  39. public float seeSign;
  40.  
  41. public GameObject shotButton;
  42.  
  43. // Health bar
  44. public GameObject healthValue;
  45. public float totalHealth;
  46.  
  47. // Visuals
  48. [SerializeField] Text powerText;
  49. [SerializeField] Text angleText;
  50.  
  51. float angle;
  52.  
  53. public bool test = false;
  54.  
  55. public float testAngle;
  56.  
  57. private void Awake()
  58. {
  59. Lineparent = new GameObject("Trajectory LRenderers");
  60. }
  61.  
  62. void Start()
  63. {
  64. shotButton = GameObject.Find("FireButton");
  65. healthValue = GameObject.Find("PlayerHealthValue");
  66. powerText.GetComponent<Text>();
  67. angleText.GetComponent<Text>();
  68. shotButton.GetComponent<Button>().onClick.AddListener(Shot);
  69. totalHealth = 1;
  70. if (!LaunchPosition) LaunchPosition = transform;
  71. ResetRenderers();
  72. }
  73.  
  74. // Update is called once per frame
  75. void Update()
  76. {
  77. Interpolant += Time.deltaTime * ScrollSpeed;
  78. if (Interpolant >= 1) Interpolant = 0;
  79.  
  80. //Check if line count changed.
  81. if (LineCount != LineRenderers.Count) ResetRenderers();
  82. LaunchSpeed = tankControl.speedControllerPosition * 20f;
  83.  
  84. if(LaunchSpeed <= 0f)
  85. {
  86. LaunchSpeed = 0f;
  87. }
  88. SetLinePositions();
  89.  
  90. float power = tankControl.speedControllerPosition * 100;
  91. //powerText.text = "POWER \n" + power.ToString("F0");
  92. powerText.text = "" + tankControl.transform.localEulerAngles.z.ToString("F0");
  93. angle = tankControl.weapon.localEulerAngles.z;
  94. angle = (angle > 180) ? angle - 360 : angle;
  95. angleText.text = "ANGLE \n" + angle.ToString("F0");
  96.  
  97. if(tankControl.m_FacingRight)
  98. {
  99. powerText.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
  100. angleText.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
  101. }
  102. else
  103. {
  104. powerText.transform.localEulerAngles = new Vector3(0f, 180f, 0f);
  105. angleText.transform.localEulerAngles = new Vector3(0f, 180f, 0f);
  106. }
  107.  
  108. healthValue.GetComponent<Image>().fillAmount = totalHealth;
  109. }
  110.  
  111.  
  112. //A list to store all the spawned line renderers
  113. private List<GameObject> LineRenderers = new List<GameObject>();
  114. //Scroll interpolant , this will go from 0 to 1 then reset to 0
  115. private float Interpolant = 0;
  116. //To check if the linecount changed and initiate a reset.
  117.  
  118. //Calculates the y coordinate of the projectile given its x coordinate (assuming projectile starts from origin and is on the (Z,Y) plane)
  119. public float TrajectoryFunction(float x)
  120. {
  121. //The tangent function is not defined for x = pi/2 , we can cheat a little by changing it to value close to 90 degrees and get approximately a vertical line.
  122. if (LaunchAngle == 85) LaunchAngle = 84.99f;
  123.  
  124. float alpha = LaunchAngle * (float)Mathf.PI / 180; //transform to radians
  125. float c = Mathf.Cos(alpha);
  126. float g = Physics.gravity.y;
  127.  
  128. return (g * x * x) / (2 * LaunchSpeed * LaunchSpeed * c * c) + Mathf.Tan(alpha) * x;
  129. }
  130.  
  131. //Respawn the Line renderers
  132. public void ResetRenderers()
  133. {
  134. //Destroy old renderers
  135. for (int i = 0; i < LineRenderers.Count; i++) Destroy(LineRenderers[i]);
  136. LineRenderers.Clear();
  137. //Spawn new renderers
  138. for (int i = 0; i < LineCount; i++)
  139. {
  140. GameObject newLine = new GameObject("Line " + i.ToString());
  141. newLine.AddComponent(typeof(LineRenderer));
  142. newLine.GetComponent<LineRenderer>().material = PreviewMaterial;
  143. newLine.transform.parent = Lineparent.transform;
  144. LineRenderers.Add(newLine);
  145. }
  146. }
  147.  
  148. public void SetLinePositions()
  149. {
  150. if (Draw)
  151. {
  152. //Check if we are shooting right or left based on the angle.
  153. if (tankControl.m_FacingRight)
  154. {
  155. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)180))); // 1
  156. }
  157. else
  158. {
  159. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)360))); // 1
  160. if(angle <= 0)
  161. {
  162. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // 1
  163. }
  164.  
  165. else if(angle > 55 && angle <= 85)
  166. {
  167. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // 1
  168. if (transform.localEulerAngles.z < 355 && transform.localEulerAngles.z > 270)
  169. {
  170. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)testAngle))); // 1 0, 180, 90, 270, 360
  171. }
  172. }
  173.  
  174. else if(angle > 0 && angle < 35)
  175. {
  176. if (transform.localEulerAngles.z > 20 && transform.localEulerAngles.z < 90)
  177. {
  178. seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // I SET IT TO 1 IN INSPECTOR
  179. }
  180. }
  181. }
  182.  
  183. MaxDistance = LaunchSpeed / 3;
  184. MaxDistance = Mathf.Clamp(MaxDistance, 1.0f, 3.5f);
  185.  
  186. if(LaunchSpeed <= 10)
  187. {
  188. LineCount = 5;
  189. }
  190. else if (LaunchSpeed >= 10 && LaunchSpeed <= 15)
  191. {
  192. LineCount = 7;
  193. }
  194. else if (LaunchSpeed > 15)
  195. {
  196. LineCount = 10;
  197. }
  198.  
  199. float step = MaxDistance / (LineRenderers.Count) * seeSign;
  200. for (int i = 0; i < LineRenderers.Count; i++)
  201. {
  202. //Reenable renderers
  203. LineRenderers[i].GetComponent<LineRenderer>().enabled = true;
  204. LineRenderers[i].GetComponent<LineRenderer>().SetWidth(0.03f, 0.03f);
  205.  
  206. if(LaunchAngle >=70)
  207. {
  208. //LineRenderers[i].GetComponent<LineRenderer>().SetWidth(0.01f, 0.01f);
  209. }
  210.  
  211. LineRenderers[i].GetComponent<LineRenderer>().sortingOrder = 2;
  212. float x = (float)i * step + Interpolant * step;
  213. //LaunchPosition.transform.rotation = tankControl.weapon.rotation;
  214. Quaternion a = LaunchPosition.transform.rotation;
  215. //Quaternion a = tankControl.weapon.rotation;
  216. float ZRotation = a.eulerAngles.z;
  217. //ZRotation = (ZRotation > 180) ? ZRotation - 360 : ZRotation;
  218. if (InvertLaunchAngle) ZRotation = -ZRotation;
  219. //If angle is not fixed , it must use the Zrotation;
  220. if (!FixedLaunchAngle)
  221. {
  222. LaunchAngle = ZRotation;
  223. //LaunchAngle = tankControl.weapon.localEulerAngles.z;
  224. }
  225. // Vector3 Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
  226. // Vector3 Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
  227.  
  228. Vector3 Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
  229. Vector3 Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
  230.  
  231. //Inverse direction based on the angle (So images wont appear facing the wrong way)
  232.  
  233. if (seeSign >= 0)
  234. {
  235. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
  236. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
  237. }
  238. else if(seeSign <= 0)
  239. {
  240. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
  241. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
  242. }
  243.  
  244. if(test)
  245. {
  246. seeSign = 1;
  247. }
  248.  
  249. /* if (test == true)
  250. {
  251. //LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
  252. //LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
  253. // seeSign = -1;
  254.  
  255. Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
  256. Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
  257.  
  258. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
  259. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
  260. }
  261.  
  262. else if (test == false)
  263. {
  264. // LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
  265. // LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
  266. // seeSign = 1;
  267.  
  268. Pos1 = new Vector3(x, (float)TrajectoryFunction(x), -LaunchPosition.position.z) + LaunchPosition.position;
  269. Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), -LaunchPosition.position.z) + LaunchPosition.position;
  270.  
  271. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
  272. LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
  273. }*/
  274. }
  275. }
  276. else
  277. {
  278.  
  279. }
  280.  
  281. if (tankControl.leftController.GetTouchPosition.x != 0)
  282. {
  283. //if we don't want to draw, disable all renderers
  284. for (int i = 0; i < LineRenderers.Count; i++) LineRenderers[i].GetComponent<LineRenderer>().enabled = false;
  285. }
  286. }
  287.  
  288. public void Shot()
  289. {
  290. Rigidbody2D bullet = Instantiate(BulletPrefab, sp.position, sp.rotation).GetComponent<Rigidbody2D>();
  291. if (tankControl.m_FacingRight)
  292. {
  293. bullet.velocity = LaunchSpeed * sp.right;
  294. }
  295. else
  296. {
  297. bullet.velocity = LaunchSpeed * -sp.right;
  298. }
  299. }
  300.  
  301. public void Damage ()
  302. {
  303. StartCoroutine(WaitExplosion(0.2f));
  304. }
  305.  
  306. IEnumerator WaitExplosion (float explosionTime)
  307. {
  308. while(explosionTime > 0)
  309. {
  310. explosionTime -= Time.deltaTime;
  311. tankControl.enabled = false;
  312. gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
  313. yield return null;
  314. }
  315. tankControl.enabled = true;
  316. tankControl.onGround = false;
  317. }
  318.  
  319. private void OnTriggerEnter2D(Collider2D collision)
  320. {
  321. if (collision.tag == "Bomb")
  322. {
  323. Damage();
  324.  
  325. float d = Vector3.Distance(collision.transform.position, transform.position);
  326. print(d);
  327. float amount;
  328.  
  329. if(d >= 0.0f && d < 0.5f)
  330. {
  331. collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 1;
  332. amount = collision.GetComponent<TankDamage>().hitDamage;
  333. print(collision.GetComponent<TankDamage>().hitDamage);
  334. }
  335. else if (d > 0.5f && d < 1.0f)
  336. {
  337. collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 2;
  338. amount = collision.GetComponent<TankDamage>().hitDamage;
  339. print(collision.GetComponent<TankDamage>().hitDamage);
  340. }
  341. else if (d > 1.0f && d < 2.0f)
  342. {
  343. collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 3;
  344. amount = collision.GetComponent<TankDamage>().hitDamage;
  345. print(collision.GetComponent<TankDamage>().hitDamage);
  346. }
  347. collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 100;
  348. totalHealth = totalHealth - collision.GetComponent<TankDamage>().hitDamage;
  349. }
  350. }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement