Advertisement
TheMagzuz

Weapon script

Feb 22nd, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Weapon : MonoBehaviour {
  5. public float fireRate = 0;
  6. public float Damage = 10;
  7. public LayerMask whatToHit;
  8. public Transform BulletTrailPrefab;
  9. private float timeToSpawnEffect = 0;
  10. public float effectSpawnRate = 10;
  11. private float timeToFire = 0;
  12.  
  13. Transform firePoint;
  14. // Use this for initialization
  15. private void Awake () {
  16. firePoint = transform.FindChild ("firePoint");
  17. if (firePoint == null) {
  18. Debug.LogError ("Firepoint not found, something went wrong");
  19.  
  20. }
  21.  
  22. }
  23.  
  24. // Update is called once per frame
  25. void Update () {
  26. if (fireRate == 0) {
  27. if (Input.GetButtonDown ("Fire1")) {
  28. Shoot ();
  29.  
  30. }
  31. }
  32. else {
  33. if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) {
  34. timeToFire = Time.time + 1/fireRate;
  35. Shoot ();
  36. }
  37. }
  38. }
  39.  
  40. void Shoot() {
  41. Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
  42. Vector2 firePointPosition = new Vector2 (firePoint.position.x,firePoint.position.y);
  43. RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
  44. if (Time.time >= timeToSpawnEffect) {
  45. Effect ();
  46. timeToSpawnEffect = Time.time + 1/effectSpawnRate;
  47. }
  48. Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100,Color.cyan);
  49. if (hit.collider != null) {
  50. Debug.DrawLine (firePointPosition, hit.point, Color.red);
  51. Debug.Log ("We hit" + hit.collider.name + " and did " + Damage +" damage");
  52. }
  53.  
  54. }
  55. void Effect () {
  56. Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement