Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class Surv_cont : MonoBehaviour
  6. {
  7. public float speed = 15.0f;
  8. public float padding = 1f;
  9. public GameObject bullet;
  10.  
  11. public float bulletSpeed;
  12. public float fireRate = 0.2f;
  13. public float health = 1000;
  14.  
  15. public AudioClip fireSound;
  16. public AudioClip deathSound;
  17. public AudioClip hitSound;
  18.  
  19. float xmin;
  20. float xmax;
  21.  
  22.  
  23. // Use this for initialization
  24. void Start()
  25. {
  26. float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
  27. Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
  28. Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
  29. xmin = leftmost.x + padding;
  30. xmax = rightmost.x - padding;
  31. }
  32.  
  33. void Fire()
  34. {
  35. Vector3 offset = new Vector3(0.25f, 0.8f, 0);
  36. GameObject shot = Instantiate(bullet, transform.position+offset, Quaternion.identity) as GameObject;
  37. shot.rigidbody2D.velocity = new Vector3(0, bulletSpeed, 0);
  38. AudioSource.PlayClipAtPoint(fireSound, transform.position);
  39. }
  40.  
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. if (Input.GetKeyDown(KeyCode.Mouse0))
  45. {
  46. InvokeRepeating("Fire", 0.00001f, fireRate);
  47. }
  48. if (Input.GetKeyUp(KeyCode.Mouse0))
  49. {
  50. CancelInvoke("Fire");
  51. }
  52.  
  53. if (Input.GetKey(KeyCode.A))
  54. {
  55. transform.position += Vector3.left * speed * Time.deltaTime;
  56.  
  57. }
  58. else if (Input.GetKey(KeyCode.D))
  59. {
  60. transform.position += Vector3.right * speed * Time.deltaTime;
  61.  
  62. float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
  63. transform.position = new Vector3(newX, transform.position.y, transform.position.z);
  64. }
  65. }
  66.  
  67. void OnTriggerEnter2D(Collider2D collider)
  68. {
  69. projectile acid = collider.gameObject.GetComponent<projectile>();
  70. if (acid)
  71. {
  72. health -= acid.GetDamage();
  73. acid.Hit();
  74. AudioSource.PlayClipAtPoint(hitSound, transform.position);
  75. if (health <= 0)
  76. {
  77. Death();
  78. }
  79. }
  80. }
  81.  
  82. void Death()
  83. {
  84. AudioSource.PlayClipAtPoint(deathSound, transform.position);
  85. Destroy(gameObject);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement