Advertisement
aceroy

Project2

Mar 2nd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.29 KB | None | 0 0
  1. //JM
  2. public class PlayerController : MonoBehaviour {
  3.  
  4.     public int health;
  5.     public float speed;
  6.     public float sensitivity;
  7.     float mouse;
  8.     float horiz;
  9.     float vert;
  10.     Rigidbody rig;
  11.     public List<GameObject> weapons = new List<GameObject>();
  12.     public GameObject currentWeapon;
  13.     public GameObject pistolShotSpawn;
  14.     public GameObject rifleShotSpawn;
  15.     public GameObject launcherShotSpawn;
  16.     public int weapon;
  17.     public GameObject friendly;
  18.     public GameObject pistolShot;
  19.     public GameObject launcherShot;
  20.     public GameObject rifleShot;
  21.  
  22.  
  23.  
  24.     void Start () {
  25.         rig = this.GetComponent<Rigidbody>();
  26.         currentWeapon = weapons[0];
  27.         weapon = 0;
  28.     }
  29.    
  30.     void Update () {
  31.         vert = Input.GetAxis("Vertical");
  32.         horiz = Input.GetAxis("Horizontal");
  33.         mouse = Input.GetAxis("Mouse X");
  34.         if(Input.GetKeyDown(KeyCode.E))
  35.         {
  36.             weapon++;
  37.             currentWeapon.SetActive(false);
  38.             if (weapon > weapons.Count-1)
  39.                 weapon = 0;
  40.             currentWeapon = weapons[weapon];
  41.             currentWeapon.SetActive(true);
  42.         }
  43.         if(Input.GetKeyDown(KeyCode.Q))
  44.         {
  45.             weapon--;
  46.             currentWeapon.SetActive(false);
  47.             if (weapon < 0)
  48.                 weapon = weapons.Count-1;
  49.             currentWeapon = weapons[weapon];
  50.             currentWeapon.SetActive(true);
  51.         }
  52.         if (health <= 0)
  53.             Destroy(this.gameObject);
  54.     }
  55.  
  56.     private void FixedUpdate()
  57.     {
  58.         Move();
  59.         Shoot();
  60.         if(Input.GetKeyDown(KeyCode.C))
  61.             transform.rotation = new Quaternion(0, transform.rotation.y, 0, 0);
  62.  
  63.     }
  64.  
  65.     private void Move()
  66.     {
  67.         rig.MovePosition(transform.position + (transform.forward * vert * speed) + (transform.right * horiz * speed));
  68.         rig.MoveRotation(rig.rotation * Quaternion.Euler(new Vector3(0, mouse * sensitivity, 0)));
  69.     }
  70.  
  71.     private void Shoot()
  72.     {
  73.         if (Input.GetKeyDown(KeyCode.Space))
  74.         {
  75.             if (weapons[weapon].name == "Pistol")
  76.             {
  77.                 Instantiate(pistolShot, pistolShotSpawn.transform.position, pistolShotSpawn.transform.rotation);            
  78.             }
  79.             if (weapons[weapon].name == "Launcher")
  80.             {
  81.                 Instantiate(launcherShot, launcherShotSpawn.transform.position, launcherShotSpawn.transform.rotation);
  82.             }
  83.             if (weapons[weapon].name == "Rifle")
  84.             {
  85.                 Instantiate(rifleShot, rifleShotSpawn.transform.position, rifleShotSpawn.transform.rotation);
  86.             }
  87.         }
  88.     }
  89.  
  90.     private void OnTriggerEnter(Collider other)
  91.     {
  92.         if (other.CompareTag("Enemy") && other.isTrigger == false)
  93.         {
  94.             Debug.Log("triggerenter" + other.name);
  95.             friendly.GetComponent<FriendlyScript>().enemySpotted = true;
  96.             friendly.GetComponent<FriendlyScript>().spottedEnemy = other.gameObject;
  97.             if(other.name == "Enemy1")
  98.             other.GetComponent<Enemy1>().playerClose = true;
  99.         }
  100.     }
  101.  
  102.     private void OnTriggerExit(Collider other)
  103.     {
  104.         if (other.CompareTag("Enemy") && other.isTrigger == false)
  105.         {
  106.             friendly.GetComponent<FriendlyScript>().enemySpotted = false;
  107.             friendly.GetComponent<FriendlyScript>().spottedEnemy = null;
  108.             other.GetComponent<Enemy1>().playerClose = false;
  109.         }
  110.     }
  111. }
  112. public class ProjectileScript : MonoBehaviour {
  113.  
  114.     public int damage;
  115.     public float speed;
  116.  
  117.     private void Start()
  118.     {
  119.         gameObject.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * speed;
  120.     }
  121.  
  122.     private void Update()
  123.     {
  124.         Destroy(this.gameObject, 5.0f);
  125.     }
  126.  
  127.     private void OnTriggerEnter(Collider other)
  128.     {
  129.         if (other.isTrigger != true)
  130.         {
  131.             if (other.CompareTag("Enemy"))
  132.             {
  133.                 other.GetComponent<EnemyBase>().health -= damage;
  134.             }
  135.             if (other.CompareTag("Friendly"))
  136.             {
  137.                 other.GetComponent<FriendlyScript>().health -= damage;
  138.             }
  139.             if (other.CompareTag("Player"))
  140.             {
  141.                 other.GetComponent<PlayerController>().health -= damage;
  142.             }
  143.  
  144.             Destroy(this.gameObject);
  145.         }
  146.     }
  147. }
  148. public class HealthPickup : MonoBehaviour {
  149.  
  150.     public int healthBonus;
  151.  
  152.     private void OnTriggerEnter(Collider other)
  153.     {
  154.         if (other.CompareTag("Player") && other.isTrigger == false)
  155.         {
  156.             other.GetComponent<PlayerController>().health += healthBonus;
  157.             Destroy(this.gameObject);
  158.         }
  159.     }
  160. }
  161. public class EnemyBase : MonoBehaviour{
  162.  
  163.     public NavMeshAgent agent;
  164.     public int health;
  165.     public GameObject player;
  166.  
  167.  
  168.     public void Death()
  169.     {
  170.         if(health<=0)
  171.         Destroy(this.gameObject);
  172.     }
  173. }
  174. public class Enemy1 : EnemyBase {
  175.  
  176.  
  177.     public bool inRadius = false;
  178.     public bool playerClose = false;
  179.     public GameObject bullet;
  180.     public Transform shotSpawn;
  181.     public float lastShot;
  182.     public float fireRate;
  183.  
  184.     private void Start()
  185.     {
  186.         agent = GetComponent<NavMeshAgent>();
  187.     }
  188.  
  189.     private void FixedUpdate()
  190.     {
  191.         if (inRadius == true && playerClose == false)
  192.         {
  193.             agent.isStopped = false;
  194.             agent.destination = player.transform.position;
  195.         }
  196.         if (playerClose)
  197.         {
  198.             transform.LookAt(player.transform.position);
  199.         }
  200.     }
  201.  
  202.     private void Update()
  203.     {
  204.         if(playerClose)
  205.         {
  206.             if (Time.realtimeSinceStartup - fireRate > lastShot)
  207.             {
  208.                 Shoot();
  209.                 lastShot = Time.realtimeSinceStartup;
  210.             }
  211.         }
  212.         Death();
  213.     }
  214.  
  215.     private void OnTriggerEnter(Collider other)
  216.     {
  217.         if(other.CompareTag("Player") && other.isTrigger == false)
  218.         {
  219.             inRadius = true;
  220.         }
  221.     }
  222.  
  223.     private void OnTriggerExit(Collider other)
  224.     {
  225.         if (other.CompareTag("Player") && other.isTrigger == false)
  226.         {
  227.             inRadius = false;
  228.         }
  229.     }
  230.  
  231.     private void Shoot()
  232.     {
  233.         Instantiate(bullet, shotSpawn.transform.position, shotSpawn.transform.rotation);
  234.     }
  235.  
  236. }
  237. public class Enemy2 : EnemyBase {
  238.  
  239.  
  240.     public List<GameObject> waypoints = new List<GameObject>();
  241.     public int waypoint;
  242.     public bool chasingPlayer = false;
  243.  
  244.     private void Start()
  245.     {
  246.         agent = GetComponent<NavMeshAgent>();
  247.         agent.destination = waypoints[0].transform.position;
  248.         waypoint = 1;
  249.     }
  250.  
  251.     private void Update()
  252.     {
  253.         if (chasingPlayer)
  254.         {
  255.             agent.destination = player.transform.position;
  256.         }
  257.         else
  258.         {
  259.             agent.destination = waypoints[waypoint].transform.position;
  260.  
  261.         }
  262.         if(waypoint > waypoints.Count-1)
  263.         {
  264.             waypoint = 0;
  265.         }
  266.         Death();
  267.     }
  268.  
  269.     private void OnTriggerEnter(Collider other)
  270.     {
  271.         if(other.gameObject == player && other.isTrigger == false)
  272.         {
  273.             chasingPlayer = true;
  274.         }
  275.        
  276.     }
  277.  
  278.     private void OnTriggerExit(Collider other)
  279.     {
  280.         if (other.gameObject == player && other.isTrigger == false)
  281.             chasingPlayer = false;
  282.     }
  283.     private void OnCollisionEnter(Collision collision)
  284.     {
  285.         if(collision.gameObject.CompareTag("Waypoint") && collision.collider.isTrigger == true)
  286.         {
  287.             Debug.Log("HELLO?!");
  288.             waypoint++;
  289.         }
  290.     }
  291. }
  292. public class Sword : MonoBehaviour {
  293.  
  294.     public int damage;
  295.  
  296.     private void OnTriggerExit(Collider other)
  297.     {
  298.         if(other.CompareTag("Player") && other.isTrigger == false)
  299.         {
  300.             other.GetComponent<PlayerController>().health -= damage;
  301.         }
  302.     }
  303.  
  304. }
  305. public class CamerScript : MonoBehaviour {
  306.  
  307.     public GameObject player;
  308.  
  309.     void Update () {
  310.         transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 0.73f, player.transform.position.z);
  311.         transform.rotation = player.transform.rotation;
  312.     }
  313. }
  314. public class Waypoint : MonoBehaviour {
  315.  
  316.     public GameObject enemy2;
  317.  
  318.     private void OnTriggerEnter(Collider other)
  319.     {
  320.         Debug.Log("trigger" + other.name);
  321.         if(other.gameObject == enemy2 && other.isTrigger == false)
  322.         {
  323.             other.gameObject.GetComponent<Enemy2>().waypoint++;
  324.             Debug.Log("waypoint++");
  325.         }
  326.     }
  327. }
  328. public class WeaponPickup : MonoBehaviour {
  329.  
  330.     public GameObject weapon;
  331.  
  332.     private void OnTriggerEnter(Collider other)
  333.     {
  334.         if(other.gameObject.CompareTag("Player") && other.isTrigger == false)
  335.         {
  336.             other.GetComponent<PlayerController>().weapons.Add(weapon);
  337.             Destroy(this.gameObject);
  338.         }
  339.     }
  340. }
  341. public class FriendlyScript : MonoBehaviour {
  342.  
  343.     public GameObject player;
  344.     public bool enemySpotted;
  345.     public int health;
  346.     public GameObject spottedEnemy;
  347.     NavMeshAgent agent;
  348.     public float fireRate;
  349.     public Transform shotSpawn;
  350.     public GameObject bullet;
  351.     public float lastShot;
  352.  
  353.     private void Start()
  354.     {
  355.         agent = GetComponent<NavMeshAgent>();
  356.         agent.destination = player.transform.position;
  357.  
  358.     }
  359.  
  360.     private void Update()
  361.     {
  362.         if (health <= 0)
  363.             Destroy(this.gameObject);
  364.         if (enemySpotted == true && spottedEnemy == null)
  365.             enemySpotted = false;
  366.     }
  367.  
  368.     private void FixedUpdate()
  369.     {
  370.         if (enemySpotted == false)
  371.         {
  372.             agent.destination = player.transform.position;
  373.             agent.isStopped = false;
  374.         }
  375.         else if (enemySpotted == true)
  376.         {
  377.             agent.isStopped = true;
  378.             transform.LookAt(spottedEnemy.transform.position);
  379.             if (Time.realtimeSinceStartup - fireRate > lastShot)
  380.             {
  381.                 Shoot();
  382.                 lastShot = Time.realtimeSinceStartup;
  383.             }
  384.         }
  385.     }
  386.     private void Shoot()
  387.     {
  388.         Instantiate(bullet, shotSpawn.transform.position, shotSpawn.transform.rotation);  
  389.     }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement