Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Trajectory : MonoBehaviour {
- //Indicate whether or not the Launch angle should depend on the LaunchPosition's rotation.
- public bool FixedLaunchAngle = false;
- //Indicates whether or not the angle should be inverted.
- public bool InvertLaunchAngle = false;
- public float LaunchAngle = 0;
- //Initial launch velocity
- public float LaunchSpeed = 25;
- //A transform that gives information about position and rotation of launch
- public Transform LaunchPosition;
- public bool Draw = true;
- //Number of lines to be drawn to represent the trajectory
- public int LineCount = 5;
- //Size of each line
- public float LineLength = 0.5f;
- //Distance that will be covered by the preview.
- public float MaxDistance = 5;
- //Lines materials
- public Material PreviewMaterial;
- //Line scrolling speed
- public float ScrollSpeed = 1.5f;
- private GameObject Lineparent;
- public GameObject BulletPrefab;
- public Transform sp;
- public PlayerController tankControl;
- public int layer = 2;
- public float seeSign;
- public GameObject shotButton;
- // Health bar
- public GameObject healthValue;
- public float totalHealth;
- // Visuals
- [SerializeField] Text powerText;
- [SerializeField] Text angleText;
- float angle;
- public bool test = false;
- public float testAngle;
- private void Awake()
- {
- Lineparent = new GameObject("Trajectory LRenderers");
- }
- void Start()
- {
- shotButton = GameObject.Find("FireButton");
- healthValue = GameObject.Find("PlayerHealthValue");
- powerText.GetComponent<Text>();
- angleText.GetComponent<Text>();
- shotButton.GetComponent<Button>().onClick.AddListener(Shot);
- totalHealth = 1;
- if (!LaunchPosition) LaunchPosition = transform;
- ResetRenderers();
- }
- // Update is called once per frame
- void Update()
- {
- Interpolant += Time.deltaTime * ScrollSpeed;
- if (Interpolant >= 1) Interpolant = 0;
- //Check if line count changed.
- if (LineCount != LineRenderers.Count) ResetRenderers();
- LaunchSpeed = tankControl.speedControllerPosition * 20f;
- if(LaunchSpeed <= 0f)
- {
- LaunchSpeed = 0f;
- }
- SetLinePositions();
- float power = tankControl.speedControllerPosition * 100;
- //powerText.text = "POWER \n" + power.ToString("F0");
- powerText.text = "" + tankControl.transform.localEulerAngles.z.ToString("F0");
- angle = tankControl.weapon.localEulerAngles.z;
- angle = (angle > 180) ? angle - 360 : angle;
- angleText.text = "ANGLE \n" + angle.ToString("F0");
- if(tankControl.m_FacingRight)
- {
- powerText.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
- angleText.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
- }
- else
- {
- powerText.transform.localEulerAngles = new Vector3(0f, 180f, 0f);
- angleText.transform.localEulerAngles = new Vector3(0f, 180f, 0f);
- }
- healthValue.GetComponent<Image>().fillAmount = totalHealth;
- }
- //A list to store all the spawned line renderers
- private List<GameObject> LineRenderers = new List<GameObject>();
- //Scroll interpolant , this will go from 0 to 1 then reset to 0
- private float Interpolant = 0;
- //To check if the linecount changed and initiate a reset.
- //Calculates the y coordinate of the projectile given its x coordinate (assuming projectile starts from origin and is on the (Z,Y) plane)
- public float TrajectoryFunction(float x)
- {
- //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.
- if (LaunchAngle == 85) LaunchAngle = 84.99f;
- float alpha = LaunchAngle * (float)Mathf.PI / 180; //transform to radians
- float c = Mathf.Cos(alpha);
- float g = Physics.gravity.y;
- return (g * x * x) / (2 * LaunchSpeed * LaunchSpeed * c * c) + Mathf.Tan(alpha) * x;
- }
- //Respawn the Line renderers
- public void ResetRenderers()
- {
- //Destroy old renderers
- for (int i = 0; i < LineRenderers.Count; i++) Destroy(LineRenderers[i]);
- LineRenderers.Clear();
- //Spawn new renderers
- for (int i = 0; i < LineCount; i++)
- {
- GameObject newLine = new GameObject("Line " + i.ToString());
- newLine.AddComponent(typeof(LineRenderer));
- newLine.GetComponent<LineRenderer>().material = PreviewMaterial;
- newLine.transform.parent = Lineparent.transform;
- LineRenderers.Add(newLine);
- }
- }
- public void SetLinePositions()
- {
- if (Draw)
- {
- //Check if we are shooting right or left based on the angle.
- if (tankControl.m_FacingRight)
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)180))); // 1
- }
- else
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)360))); // 1
- if(angle <= 0)
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // 1
- }
- else if(angle > 55 && angle <= 85)
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // 1
- if (transform.localEulerAngles.z < 355 && transform.localEulerAngles.z > 270)
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)testAngle))); // 1 0, 180, 90, 270, 360
- }
- }
- else if(angle > 0 && angle < 35)
- {
- if (transform.localEulerAngles.z > 20 && transform.localEulerAngles.z < 90)
- {
- seeSign = Mathf.Sign(Mathf.Cos(LaunchAngle * (Mathf.PI / (float)0))); // I SET IT TO 1 IN INSPECTOR
- }
- }
- }
- MaxDistance = LaunchSpeed / 3;
- MaxDistance = Mathf.Clamp(MaxDistance, 1.0f, 3.5f);
- if(LaunchSpeed <= 10)
- {
- LineCount = 5;
- }
- else if (LaunchSpeed >= 10 && LaunchSpeed <= 15)
- {
- LineCount = 7;
- }
- else if (LaunchSpeed > 15)
- {
- LineCount = 10;
- }
- float step = MaxDistance / (LineRenderers.Count) * seeSign;
- for (int i = 0; i < LineRenderers.Count; i++)
- {
- //Reenable renderers
- LineRenderers[i].GetComponent<LineRenderer>().enabled = true;
- LineRenderers[i].GetComponent<LineRenderer>().SetWidth(0.03f, 0.03f);
- if(LaunchAngle >=70)
- {
- //LineRenderers[i].GetComponent<LineRenderer>().SetWidth(0.01f, 0.01f);
- }
- LineRenderers[i].GetComponent<LineRenderer>().sortingOrder = 2;
- float x = (float)i * step + Interpolant * step;
- //LaunchPosition.transform.rotation = tankControl.weapon.rotation;
- Quaternion a = LaunchPosition.transform.rotation;
- //Quaternion a = tankControl.weapon.rotation;
- float ZRotation = a.eulerAngles.z;
- //ZRotation = (ZRotation > 180) ? ZRotation - 360 : ZRotation;
- if (InvertLaunchAngle) ZRotation = -ZRotation;
- //If angle is not fixed , it must use the Zrotation;
- if (!FixedLaunchAngle)
- {
- LaunchAngle = ZRotation;
- //LaunchAngle = tankControl.weapon.localEulerAngles.z;
- }
- // Vector3 Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
- // Vector3 Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
- Vector3 Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
- Vector3 Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
- //Inverse direction based on the angle (So images wont appear facing the wrong way)
- if (seeSign >= 0)
- {
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
- }
- else if(seeSign <= 0)
- {
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
- }
- if(test)
- {
- seeSign = 1;
- }
- /* if (test == true)
- {
- //LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
- //LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
- // seeSign = -1;
- Pos1 = new Vector3(x, (float)TrajectoryFunction(x), LaunchPosition.position.z) + LaunchPosition.position;
- Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), LaunchPosition.position.z) + LaunchPosition.position;
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
- }
- else if (test == false)
- {
- // LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos1);
- // LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos2);
- // seeSign = 1;
- Pos1 = new Vector3(x, (float)TrajectoryFunction(x), -LaunchPosition.position.z) + LaunchPosition.position;
- Pos2 = new Vector3(x + LineLength, TrajectoryFunction(x + LineLength), -LaunchPosition.position.z) + LaunchPosition.position;
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(1, Pos1);
- LineRenderers[i].GetComponent<LineRenderer>().SetPosition(0, Pos2);
- }*/
- }
- }
- else
- {
- }
- if (tankControl.leftController.GetTouchPosition.x != 0)
- {
- //if we don't want to draw, disable all renderers
- for (int i = 0; i < LineRenderers.Count; i++) LineRenderers[i].GetComponent<LineRenderer>().enabled = false;
- }
- }
- public void Shot()
- {
- Rigidbody2D bullet = Instantiate(BulletPrefab, sp.position, sp.rotation).GetComponent<Rigidbody2D>();
- if (tankControl.m_FacingRight)
- {
- bullet.velocity = LaunchSpeed * sp.right;
- }
- else
- {
- bullet.velocity = LaunchSpeed * -sp.right;
- }
- }
- public void Damage ()
- {
- StartCoroutine(WaitExplosion(0.2f));
- }
- IEnumerator WaitExplosion (float explosionTime)
- {
- while(explosionTime > 0)
- {
- explosionTime -= Time.deltaTime;
- tankControl.enabled = false;
- gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
- yield return null;
- }
- tankControl.enabled = true;
- tankControl.onGround = false;
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Bomb")
- {
- Damage();
- float d = Vector3.Distance(collision.transform.position, transform.position);
- print(d);
- float amount;
- if(d >= 0.0f && d < 0.5f)
- {
- collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 1;
- amount = collision.GetComponent<TankDamage>().hitDamage;
- print(collision.GetComponent<TankDamage>().hitDamage);
- }
- else if (d > 0.5f && d < 1.0f)
- {
- collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 2;
- amount = collision.GetComponent<TankDamage>().hitDamage;
- print(collision.GetComponent<TankDamage>().hitDamage);
- }
- else if (d > 1.0f && d < 2.0f)
- {
- collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 3;
- amount = collision.GetComponent<TankDamage>().hitDamage;
- print(collision.GetComponent<TankDamage>().hitDamage);
- }
- collision.GetComponent<TankDamage>().hitDamage = collision.GetComponent<TankDamage>().hitDamage / 100;
- totalHealth = totalHealth - collision.GetComponent<TankDamage>().hitDamage;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement