Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PistolShot : MonoBehaviour {
- //Basics
- public float speed;
- public int damageToApply;
- private bool facingRight = true;
- Controller2D controller;
- PlayerDeath player;
- public GameObject hitEffect;
- public Rigidbody2D bullet;
- Vector3 velocity;
- public GameObject playerObject;
- //Auto destruct timer
- public float lifeTime;
- float timeToLifeStop;
- private bool alive;
- //Terrain interactions
- public bool ignoreTerrain;
- public float groundRange;
- public LayerMask terrainMask;
- public bool bounce;
- public float bounceTime;
- private float timeUntilBounce;
- private bool bounced;
- public bool pierce;
- //Flight dynamics
- private bool flight;
- public bool thrust;
- public float gravity;
- private float T;
- public float initialGravity;
- private float adjustedGravity;
- public bool wave;
- private float sinPos;
- public float waveDamp;
- public float waveFreq;
- public bool inheritVelocity;
- private Vector2 initialVelocity;
- public bool reflect;
- private float Angle;
- public Vector2 newVelocity;
- //Smart targeting
- public bool flare;
- public float turnSpeed;
- public GameObject enemy;
- public GameObject lastTarget;
- public bool weird;
- public bool homing;
- public float maxLockRange;
- // Use this for initialization
- void Start () {
- playerObject = GameObject.FindGameObjectWithTag("Player");
- alive = true;
- bullet = GetComponent<Rigidbody2D> ();
- timeToLifeStop = lifeTime;
- player = FindObjectOfType<PlayerDeath> ();
- flight = true;
- sinPos = Mathf.PI/2;
- if (wave) {
- gravity = 0;
- }
- if (inheritVelocity) {
- initialVelocity = new Vector2(playerObject.GetComponent<Player>().velocity.x, playerObject.GetComponent<Player>().velocity.y);
- if(thrust){
- GetComponent<Rigidbody2D> ().velocity = new Vector2(playerObject.GetComponent<Player>().velocity.x, 0);
- }
- }
- adjustedGravity = initialGravity - (transform.right.y * speed);
- if (flare) {
- enemy = FindNearestTarget();
- }
- if (homing) {
- enemy = FindNearestEnemy();
- }
- lastTarget = null;
- T = 0;
- }
- // Update is called once per frame
- void Update () {
- Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
- if (input.x > 0 && !facingRight) {
- facingRight = !facingRight;
- }
- else if (input.x < 0 && facingRight) {
- facingRight = !facingRight;
- }
- T += Time.deltaTime;
- if(reflect){
- RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.right, groundRange, terrainMask);
- if(hit){
- if(transform.right.x <=0){
- Angle = transform.eulerAngles.z + 2*Vector2.Angle (transform.right, hit.normal);
- } else if (transform.right.x >=0){
- Angle = transform.eulerAngles.z - 2*Vector2.Angle (transform.right, hit.normal);
- }
- if(hit.normal.y != 0)
- {
- if(hit.normal.y >=0){
- transform.eulerAngles = new Vector3(0,0, Angle);
- } else if(hit.normal.y <= 0){
- transform.eulerAngles = new Vector3(0,0, Angle + 180);
- }
- } else if(hit.normal.y == 0){
- if(hit.normal.x == -1){
- if(transform.right.y ==0){
- transform.eulerAngles = new Vector3(0,0, 225);
- } else if (transform.right.y <=0){
- transform.eulerAngles = new Vector3(0,0, 225);
- } else if (transform.right.y >=0){
- transform.eulerAngles = new Vector3(0,0, 135);
- }
- } else if(hit.normal.x == 1){
- if(transform.right.y ==0){
- transform.eulerAngles = new Vector3(0,0, 315);
- } else if (transform.right.y <=0){
- transform.eulerAngles = new Vector3(0,0, 315);
- } else if (transform.right.y >=0){
- transform.eulerAngles = new Vector3(0,0, 405);
- }
- }
- }
- }
- RaycastHit2D hit2 = Physics2D.Raycast (transform.position, transform.right, groundRange/2, terrainMask);
- if(hit2){
- Destroy(gameObject);
- print ("Fuck it");
- }
- }
- if (bounce) {
- RaycastHit2D hit = Physics2D.Raycast (transform.position, -Vector2.up, groundRange, terrainMask);
- if(hit){
- adjustedGravity = -adjustedGravity;
- bounce = false;
- bounced = true;
- timeUntilBounce = bounceTime;
- }
- }
- if (bounced) {
- timeUntilBounce -= Time.deltaTime;
- if(timeUntilBounce <= 0){
- bounce = true;
- bounced = false;
- }
- }
- if (wave) {
- sinPos += Time.deltaTime * waveFreq;
- adjustedGravity = (Mathf.Sin (sinPos)) * waveDamp;
- } else {
- adjustedGravity += Time.deltaTime * gravity;
- }
- if (!alive) {
- Destroy (gameObject);
- Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
- }
- if (flare) {
- enemy = FindNearestTarget ();
- Vector3 dir = enemy.transform.position - transform.position;
- float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
- Quaternion target = Quaternion.AngleAxis (angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp (transform.rotation, target, turnSpeed);
- }
- if (homing) {
- enemy = FindNearestEnemy ();
- if(enemy == null){
- enemy = lastTarget;
- }
- Vector3 dir = enemy.transform.position - transform.position;
- float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
- Quaternion target = Quaternion.AngleAxis (angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp (transform.rotation, target, turnSpeed);
- Debug.DrawLine (transform.position, enemy.transform.position, Color.cyan);
- Debug.DrawLine (transform.position, lastTarget.transform.position, Color.green);
- }
- }
- void FixedUpdate() {
- if (flight) {
- if(!thrust && !wave){
- newVelocity = new Vector2 (transform.right.x * speed + initialVelocity.x, (gravity != 0)?-adjustedGravity: transform.right.y * speed);
- } else if (thrust){
- GetComponent<Rigidbody2D> ().AddForce (transform.right*speed);
- } else if (!thrust && wave){
- newVelocity = new Vector2 (transform.right.x * speed + (transform.up.x * adjustedGravity) + initialVelocity.x, transform.right.y * speed + (transform.up.y * adjustedGravity));
- }
- transform.position = new Vector3(transform.position.x + (newVelocity.x/50), transform.position.y + (newVelocity.y/50), transform.position.z);
- }
- }
- void LateUpdate () {
- if (timeToLifeStop > 0) {
- timeToLifeStop -=Time.deltaTime;
- alive = true;
- }
- if (timeToLifeStop <= 0)
- {
- alive = false;
- }
- }
- void OnTriggerEnter2D(Collider2D other){
- if (other.tag == "Terrain" && !ignoreTerrain) {
- Destroy (gameObject);
- Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
- }
- if (other.tag == "Enemy") {
- other.GetComponent<EnemyHealthManager>().damageEnemy(damageToApply);
- enemy = lastTarget;
- lastTarget = other.gameObject;
- if(!pierce){
- Destroy (gameObject);
- }
- Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
- if(weird){
- homing = true;
- ignoreTerrain = true;
- }
- }
- if (other.tag == "Missile") {
- other.GetComponent<EnemyHealthManager>().damageEnemy(damageToApply);
- if(!pierce){
- Destroy (gameObject);
- }
- Instantiate (hitEffect, bullet.transform.position, bullet.transform.rotation);
- }
- }
- GameObject FindNearestTarget(){
- GameObject[] gos;
- gos = GameObject.FindGameObjectsWithTag("Missile");
- GameObject closest = null;
- float distance = Mathf.Infinity;
- Vector3 position = transform.position;
- foreach (GameObject go in gos) {
- Vector3 diff = go.transform.position - position;
- float curDistance = diff.sqrMagnitude;
- if (curDistance < distance) {
- if(curDistance < maxLockRange){
- closest = go;
- }
- distance = curDistance;
- Debug.DrawLine (transform.position, go.transform.position, Color.green);
- }
- }
- return closest;
- }
- GameObject FindNearestEnemy(){
- GameObject[] gos;
- gos = GameObject.FindGameObjectsWithTag("Enemy");
- GameObject closest = null;
- float distance = maxLockRange;
- Vector3 position = transform.position;
- foreach (GameObject go in gos) {
- Vector3 diff = go.transform.position - position;
- float curDistance = diff.sqrMagnitude;
- if (curDistance < distance) {
- if (go.transform.position != lastTarget.transform.position) {
- closest = go;
- }
- distance = curDistance;
- }
- }
- return closest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment