Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- public class Surv_cont : MonoBehaviour
- {
- public float speed = 15.0f;
- public float padding = 1f;
- public GameObject bullet;
- public float bulletSpeed;
- public float fireRate = 0.2f;
- public float health = 1000;
- public AudioClip fireSound;
- public AudioClip deathSound;
- public AudioClip hitSound;
- float xmin;
- float xmax;
- // Use this for initialization
- void Start()
- {
- float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
- Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
- Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
- xmin = leftmost.x + padding;
- xmax = rightmost.x - padding;
- }
- void Fire()
- {
- Vector3 offset = new Vector3(0.25f, 0.8f, 0);
- GameObject shot = Instantiate(bullet, transform.position+offset, Quaternion.identity) as GameObject;
- shot.rigidbody2D.velocity = new Vector3(0, bulletSpeed, 0);
- AudioSource.PlayClipAtPoint(fireSound, transform.position);
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Mouse0))
- {
- InvokeRepeating("Fire", 0.00001f, fireRate);
- }
- if (Input.GetKeyUp(KeyCode.Mouse0))
- {
- CancelInvoke("Fire");
- }
- if (Input.GetKey(KeyCode.A))
- {
- transform.position += Vector3.left * speed * Time.deltaTime;
- }
- else if (Input.GetKey(KeyCode.D))
- {
- transform.position += Vector3.right * speed * Time.deltaTime;
- float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
- transform.position = new Vector3(newX, transform.position.y, transform.position.z);
- }
- }
- void OnTriggerEnter2D(Collider2D collider)
- {
- projectile acid = collider.gameObject.GetComponent<projectile>();
- if (acid)
- {
- health -= acid.GetDamage();
- acid.Hit();
- AudioSource.PlayClipAtPoint(hitSound, transform.position);
- if (health <= 0)
- {
- Death();
- }
- }
- }
- void Death()
- {
- AudioSource.PlayClipAtPoint(deathSound, transform.position);
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement