Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class WeaponAttack : MonoBehaviour {
  5. public GameObject oneHandSpawn, twoHandSpawn, bullet;
  6. GameObject curWeapon;
  7. public bool gun = false;
  8. float timer = 0.1f,timerReset=0.1f;
  9. PlayerAnimate pa;
  10. SpriteContainer sc;
  11.  
  12. float weaponChange = 0.5f;
  13. bool changingWeapon = false;
  14. bool oneHanded = false;
  15.  
  16. void Start () {
  17. pa = this.GetComponent<PlayerAnimate> ();
  18. sc = GameObject.FindGameObjectWithTag ("GameController").GetComponent<SpriteContainer> ();
  19. }
  20.  
  21.  
  22. void Update () {
  23.  
  24.  
  25. if (timer > 0) {
  26. timer -= Time.deltaTime;
  27. }
  28.  
  29. if(Input.GetMouseButton(0) && timer <=0)
  30. {
  31. attack ();
  32. }
  33. if(Input.GetMouseButtonDown(0))
  34. {
  35. pa.resetCounter ();
  36. }
  37. if (Input.GetMouseButtonUp (0)) {
  38. pa.resetCounter ();
  39. }
  40.  
  41. if (Input.GetMouseButtonDown (1) && changingWeapon == false) {
  42. dropWeapon ();
  43. }
  44.  
  45. if(changingWeapon==true)
  46. {
  47. weaponChange -= Time.deltaTime;
  48. if(weaponChange<=0)
  49. {
  50. changingWeapon = false;
  51. }
  52. }
  53. }
  54.  
  55. public void setWeapon(GameObject cur, string name, float fireRate,bool gun, bool oneHanded)
  56. {
  57.  
  58. changingWeapon = true;
  59. curWeapon = cur;
  60. pa.setNewTorso (sc.getWeaponWalk(name),sc.getWeapon(name));
  61. this.gun = gun;
  62. timerReset = fireRate;
  63. timer = timerReset;
  64. this.oneHanded = oneHanded;
  65. }
  66.  
  67.  
  68. public void attack()
  69. {
  70.  
  71.  
  72. pa.attack ();
  73. if (gun == true) {
  74. Bullet bl = bullet.GetComponent<Bullet> (); //creation of bullet with own direction set
  75. Vector3 dir;
  76. dir.x = Vector2.right.x; //rotation of bullet
  77. dir.y = Vector2.right.y;
  78. dir.z = 0;
  79. bl.setVals (dir, "Player"); //knows who created the bullet
  80. if (oneHanded == true) {
  81. Instantiate (bullet, oneHandSpawn.transform.position, this.transform.rotation);
  82. } else {
  83. Instantiate (bullet, twoHandSpawn.transform.position, this.transform.rotation);
  84. }
  85.  
  86. timer = timerReset;
  87. } else {
  88. //melee attack
  89. pa.attack ();
  90. RaycastHit2D ray = Physics2D.Raycast (new Vector2(this.transform.position.x,this.transform.position.y),new Vector2(transform.right.x,transform.right.y)); //create a line from the player wich can hit an enemy
  91. Debug.DrawRay (new Vector2(this.transform.position.x,this.transform.position.y),new Vector2(transform.right.x,transform.right.y),Color.green);
  92. if (curWeapon == null && ray.collider.gameObject.tag == "Enemy") {
  93. EnemyAttacked ea = ray.collider.gameObject.GetComponent<EnemyAttacked> ();
  94. ea.knockDownEnemy ();
  95. } else if (ray.collider != null) {
  96. if (ray.collider.gameObject.tag == "Enemy") { //if player have meelee weapon -> instant kill
  97. EnemyAttacked ea = ray.collider.gameObject.GetComponent<EnemyAttacked> ();
  98. ea.killMelee ();
  99. }
  100. }
  101. }
  102.  
  103. }
  104.  
  105. public GameObject getCur()
  106. {
  107. return curWeapon;
  108. }
  109.  
  110. public void dropWeapon()
  111. {
  112.  
  113. curWeapon.transform.position = this.transform.position;
  114. curWeapon.SetActive (true);
  115. setWeapon (null, "", 0.5f, false, false);
  116.  
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement