Guest User

Untitled

a guest
Dec 6th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PistolShot : MonoBehaviour {
  5.  
  6.  
  7. //Basics
  8. public float speed;
  9.  
  10. public int damageToApply;
  11.  
  12. private bool facingRight = true;
  13.  
  14. Controller2D controller;
  15.  
  16. PlayerDeath player;
  17.  
  18. public GameObject hitEffect;
  19.  
  20. public Rigidbody2D bullet;
  21.  
  22. Vector3 velocity;
  23.  
  24. public GameObject playerObject;
  25.  
  26.  
  27. //Auto destruct timer
  28. public float lifeTime;
  29. float timeToLifeStop;
  30. private bool alive;
  31.  
  32.  
  33. //Terrain interactions
  34. public bool ignoreTerrain;
  35.  
  36. public float groundRange;
  37.  
  38. public LayerMask terrainMask;
  39.  
  40. public bool bounce;
  41.  
  42. public float bounceTime;
  43.  
  44. private float timeUntilBounce;
  45.  
  46. private bool bounced;
  47.  
  48. public bool pierce;
  49.  
  50.  
  51. //Flight dynamics
  52. private bool flight;
  53.  
  54. public bool thrust;
  55.  
  56. public float gravity;
  57.  
  58. private float T;
  59.  
  60. public float initialGravity;
  61.  
  62. private float adjustedGravity;
  63.  
  64. public bool wave;
  65.  
  66. private float sinPos;
  67.  
  68. public float waveDamp;
  69.  
  70. public float waveFreq;
  71.  
  72. public bool inheritVelocity;
  73.  
  74. private Vector2 initialVelocity;
  75.  
  76. public bool reflect;
  77.  
  78. private float Angle;
  79.  
  80. public Vector2 newVelocity;
  81.  
  82. //Smart targeting
  83. public bool flare;
  84.  
  85. public float turnSpeed;
  86.  
  87. public GameObject enemy;
  88.  
  89. public GameObject lastTarget;
  90.  
  91. public bool weird;
  92.  
  93. public bool homing;
  94.  
  95. public float maxLockRange;
  96.  
  97. // Use this for initialization
  98. void Start () {
  99. playerObject = GameObject.FindGameObjectWithTag("Player");
  100.  
  101. alive = true;
  102.  
  103. bullet = GetComponent<Rigidbody2D> ();
  104.  
  105. timeToLifeStop = lifeTime;
  106.  
  107. player = FindObjectOfType<PlayerDeath> ();
  108.  
  109. flight = true;
  110.  
  111. sinPos = Mathf.PI/2;
  112.  
  113. if (wave) {
  114. gravity = 0;
  115. }
  116.  
  117.  
  118. if (inheritVelocity) {
  119. initialVelocity = new Vector2(playerObject.GetComponent<Player>().velocity.x, playerObject.GetComponent<Player>().velocity.y);
  120.  
  121. if(thrust){
  122. GetComponent<Rigidbody2D> ().velocity = new Vector2(playerObject.GetComponent<Player>().velocity.x, 0);
  123. }
  124. }
  125.  
  126. adjustedGravity = initialGravity - (transform.right.y * speed);
  127.  
  128. if (flare) {
  129. enemy = FindNearestTarget();
  130. }
  131.  
  132. if (homing) {
  133. enemy = FindNearestEnemy();
  134. }
  135.  
  136. lastTarget = null;
  137.  
  138. T = 0;
  139.  
  140. }
  141.  
  142. // Update is called once per frame
  143. void Update () {
  144. Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
  145.  
  146. if (input.x > 0 && !facingRight) {
  147.  
  148. facingRight = !facingRight;
  149. }
  150. else if (input.x < 0 && facingRight) {
  151.  
  152. facingRight = !facingRight;
  153. }
  154.  
  155. T += Time.deltaTime;
  156.  
  157. if(reflect){
  158. RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.right, groundRange, terrainMask);
  159.  
  160. if(hit){
  161. if(transform.right.x <=0){
  162. Angle = transform.eulerAngles.z + 2*Vector2.Angle (transform.right, hit.normal);
  163. } else if (transform.right.x >=0){
  164. Angle = transform.eulerAngles.z - 2*Vector2.Angle (transform.right, hit.normal);
  165. }
  166. if(hit.normal.y != 0)
  167. {
  168. if(hit.normal.y >=0){
  169. transform.eulerAngles = new Vector3(0,0, Angle);
  170. } else if(hit.normal.y <= 0){
  171. transform.eulerAngles = new Vector3(0,0, Angle + 180);
  172. }
  173. } else if(hit.normal.y == 0){
  174. if(hit.normal.x == -1){
  175. if(transform.right.y ==0){
  176. transform.eulerAngles = new Vector3(0,0, 225);
  177. } else if (transform.right.y <=0){
  178. transform.eulerAngles = new Vector3(0,0, 225);
  179. } else if (transform.right.y >=0){
  180. transform.eulerAngles = new Vector3(0,0, 135);
  181. }
  182. } else if(hit.normal.x == 1){
  183. if(transform.right.y ==0){
  184. transform.eulerAngles = new Vector3(0,0, 315);
  185. } else if (transform.right.y <=0){
  186. transform.eulerAngles = new Vector3(0,0, 315);
  187. } else if (transform.right.y >=0){
  188. transform.eulerAngles = new Vector3(0,0, 405);
  189. }
  190. }
  191. }
  192. }
  193.  
  194. RaycastHit2D hit2 = Physics2D.Raycast (transform.position, transform.right, groundRange/2, terrainMask);
  195. if(hit2){
  196. Destroy(gameObject);
  197. print ("Fuck it");
  198. }
  199. }
  200.  
  201. if (bounce) {
  202. RaycastHit2D hit = Physics2D.Raycast (transform.position, -Vector2.up, groundRange, terrainMask);
  203.  
  204. if(hit){
  205.  
  206. adjustedGravity = -adjustedGravity;
  207.  
  208. bounce = false;
  209.  
  210. bounced = true;
  211.  
  212. timeUntilBounce = bounceTime;
  213. }
  214. }
  215.  
  216. if (bounced) {
  217. timeUntilBounce -= Time.deltaTime;
  218.  
  219. if(timeUntilBounce <= 0){
  220. bounce = true;
  221. bounced = false;
  222. }
  223. }
  224.  
  225. if (wave) {
  226. sinPos += Time.deltaTime * waveFreq;
  227.  
  228. adjustedGravity = (Mathf.Sin (sinPos)) * waveDamp;
  229. } else {
  230. adjustedGravity += Time.deltaTime * gravity;
  231. }
  232.  
  233.  
  234. if (!alive) {
  235. Destroy (gameObject);
  236. Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
  237. }
  238.  
  239.  
  240. if (flare) {
  241. enemy = FindNearestTarget ();
  242. Vector3 dir = enemy.transform.position - transform.position;
  243. float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
  244.  
  245. Quaternion target = Quaternion.AngleAxis (angle, Vector3.forward);
  246. transform.rotation = Quaternion.Slerp (transform.rotation, target, turnSpeed);
  247. }
  248.  
  249. if (homing) {
  250. enemy = FindNearestEnemy ();
  251. if(enemy == null){
  252. enemy = lastTarget;
  253. }
  254.  
  255. Vector3 dir = enemy.transform.position - transform.position;
  256. float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
  257.  
  258. Quaternion target = Quaternion.AngleAxis (angle, Vector3.forward);
  259. transform.rotation = Quaternion.Slerp (transform.rotation, target, turnSpeed);
  260.  
  261. Debug.DrawLine (transform.position, enemy.transform.position, Color.cyan);
  262.  
  263. Debug.DrawLine (transform.position, lastTarget.transform.position, Color.green);
  264. }
  265.  
  266. }
  267.  
  268. void FixedUpdate() {
  269. if (flight) {
  270. if(!thrust && !wave){
  271. newVelocity = new Vector2 (transform.right.x * speed + initialVelocity.x, (gravity != 0)?-adjustedGravity: transform.right.y * speed);
  272. } else if (thrust){
  273. GetComponent<Rigidbody2D> ().AddForce (transform.right*speed);
  274. } else if (!thrust && wave){
  275. newVelocity = new Vector2 (transform.right.x * speed + (transform.up.x * adjustedGravity) + initialVelocity.x, transform.right.y * speed + (transform.up.y * adjustedGravity));
  276. }
  277.  
  278. transform.position = new Vector3(transform.position.x + (newVelocity.x/50), transform.position.y + (newVelocity.y/50), transform.position.z);
  279. }
  280. }
  281.  
  282.  
  283. void LateUpdate () {
  284.  
  285. if (timeToLifeStop > 0) {
  286. timeToLifeStop -=Time.deltaTime;
  287. alive = true;
  288. }
  289.  
  290. if (timeToLifeStop <= 0)
  291. {
  292. alive = false;
  293. }
  294.  
  295. }
  296.  
  297. void OnTriggerEnter2D(Collider2D other){
  298. if (other.tag == "Terrain" && !ignoreTerrain) {
  299. Destroy (gameObject);
  300. Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
  301. }
  302.  
  303. if (other.tag == "Enemy") {
  304. other.GetComponent<EnemyHealthManager>().damageEnemy(damageToApply);
  305. enemy = lastTarget;
  306. lastTarget = other.gameObject;
  307. if(!pierce){
  308. Destroy (gameObject);
  309. }
  310. Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
  311. if(weird){
  312. homing = true;
  313. ignoreTerrain = true;
  314. }
  315.  
  316. }
  317.  
  318. if (other.tag == "Missile") {
  319. other.GetComponent<EnemyHealthManager>().damageEnemy(damageToApply);
  320. if(!pierce){
  321. Destroy (gameObject);
  322. }
  323. Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
  324. }
  325.  
  326. }
  327.  
  328. GameObject FindNearestTarget(){
  329. GameObject[] gos;
  330. gos = GameObject.FindGameObjectsWithTag("Missile");
  331. GameObject closest = null;
  332. float distance = Mathf.Infinity;
  333. Vector3 position = transform.position;
  334. foreach (GameObject go in gos) {
  335. Vector3 diff = go.transform.position - position;
  336. float curDistance = diff.sqrMagnitude;
  337. if (curDistance < distance) {
  338. if(curDistance < maxLockRange){
  339. closest = go;
  340. }
  341. distance = curDistance;
  342. Debug.DrawLine (transform.position, go.transform.position, Color.green);
  343. }
  344. }
  345. return closest;
  346. }
  347.  
  348. GameObject FindNearestEnemy(){
  349. GameObject[] gos;
  350. gos = GameObject.FindGameObjectsWithTag("Enemy");
  351. GameObject closest = null;
  352. float distance = maxLockRange;
  353. Vector3 position = transform.position;
  354. foreach (GameObject go in gos) {
  355. Vector3 diff = go.transform.position - position;
  356. float curDistance = diff.sqrMagnitude;
  357. if (curDistance < distance) {
  358. if (go.transform.position != lastTarget.transform.position) {
  359. closest = go;
  360. }
  361. distance = curDistance;
  362. }
  363. }
  364. return closest;
  365. }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment