Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TheBot : MonoBehaviour {
  6.  
  7.     public float speed;
  8.     public int jumpForce;
  9.     public Transform groundCheck;
  10.     public Transform meleeCheck;
  11.     public Transform bulletSpawner;
  12.     public Transform wallSlideBSpawner;
  13.     public LayerMask layerGround;
  14.     public float meleeCoolDown;
  15.     public float meleeDamage;
  16.  
  17.     private Rigidbody2D body;
  18.     private Animator anim;
  19.     private Dash dashController;
  20.     private Shooter shotController;
  21.     private float unloadWaitingTime = 60 * 3;
  22.     private float idleGunTime = 0;
  23.  
  24.     private bool facingRight = true;
  25.     private bool onGround = true;
  26.     private bool jumping = false;
  27.     private bool attacking = false;
  28.     private bool dead = false;
  29.     private bool isGunLoaded = false;
  30.     private bool isGunLoading = false;
  31.     private bool isGunUnloading = false;
  32.     private bool takingDamage = false;
  33.     private bool dashing = false;
  34.     private bool isWallSliding = false;
  35.     private bool wallJumping = false;
  36.  
  37.  
  38.     void Start () {
  39.         body = GetComponent<Rigidbody2D>();
  40.         anim = GetComponent<Animator>();
  41.         dashController = GetComponent<Dash>();
  42.         shotController = GetComponent<Shooter>();
  43.     }
  44.    
  45.  
  46.     void Update () {
  47.         PlayAnimations();
  48.         CheckIfGrounded();
  49.         checkIfWallSliding();
  50.         dashing = dashController.IsDashing();
  51.  
  52.         if (Input.GetButtonDown("Jump") && onGround  && !isGunLoading && !jumping && !takingDamage  && !isWallSliding){
  53.             jumping = true;
  54.         }
  55.  
  56.         if(Input.GetButtonDown("Jump") && !onGround  && !isGunLoading && !takingDamage && !wallJumping && isWallSliding){
  57.             wallJumping = true;
  58.             Debug.Log("detecting walljump input");
  59.         }
  60.         if (Input.GetButtonDown("Melee") && !attacking && !isGunLoading){
  61.             Attack();
  62.         }
  63.         if(Input.GetButtonDown("Ranged") && !attacking  && !isGunLoading){
  64.             Shoot();
  65.         }
  66.  
  67.         if(Input.GetButtonDown("Dash") && !attacking && !isGunLoading && onGround){
  68.             dashController.DashTo(facingRight? Dash.RIGHT : Dash.LEFT);
  69.         }
  70.  
  71.  
  72.         if(isGunLoaded){
  73.             idleGunTime += Time.deltaTime;
  74.             if (idleGunTime >= unloadWaitingTime){
  75.                 UnloadGun();
  76.             }
  77.         }
  78.  
  79.     }
  80.  
  81.     void FixedUpdate(){
  82.         if(!takingDamage){
  83.  
  84.             float move = Input.GetAxis("Horizontal");
  85.  
  86.             //while charachter is wall sliding, slowly fall
  87.             if (isWallSliding && !wallJumping && body.velocity.y < -0.7f){
  88.                 body.velocity = new Vector2(body.velocity.x, -0.7f);
  89.             }
  90.  
  91.             if(!dashing){
  92.                 if(onGround){
  93.                     //if not dashing on on ground, walk with normal speed
  94.                     body.velocity = new Vector2(move * speed, body.velocity.y);
  95.                 } else {
  96.                     //if character is not on ground, reduce the speed so he doesn't jump too far away
  97.  
  98.                     float airControlAccelerationLimit = 1.5f;  // Higher = more responsive air control
  99.                     float airSpeedModifier = 0.7f; // the 0.7f in your code, affects max air speed
  100.                     float targetHorizVelocity = move
  101.                             * speed
  102.                             * airSpeedModifier;  // How fast we are trying to move horizontally
  103.                     float targetHorizChange = targetHorizVelocity
  104.                             - body.velocity.x; // How much we want to change the horizontal velocity
  105.                     float horizChange = Mathf.Clamp(
  106.                             targetHorizChange ,
  107.                             -airControlAccelerationLimit ,
  108.                             airControlAccelerationLimit ); // How much we are limiting ourselves
  109.                                                            // to changing the horizontal velocity
  110.                     body.velocity = new Vector2(body.velocity.x + horizChange, body.velocity.y);
  111.                 }
  112.             }
  113.  
  114.             if((move < 0 && facingRight) || (move > 0 && !facingRight) ){
  115.                 //control direction character is facing
  116.                 Flip();
  117.             }
  118.  
  119.             if (jumping){
  120.  
  121.                 body.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
  122.  
  123.  
  124.                 if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)){
  125.                     //if is moving while jumping, reduce jump height
  126.                     body.velocity = new Vector2(body.velocity.x, body.velocity.y*0.8f);
  127.                 }
  128.                 onGround = false;
  129.                 jumping = false;
  130.             }
  131.             else if (wallJumping){
  132.                 Debug.Log("attempting walljump");
  133.                 float velocityX = body.velocity.x + (0.5f * jumpForce) * (facingRight? -1 : 1);
  134.                 body.velocity = new Vector2(velocityX, jumpForce * 0.55f);
  135.                 wallJumping=false;
  136.                 isWallSliding=false;
  137.             }
  138.         }
  139.     }
  140.  
  141.  
  142.     void CheckIfGrounded(){
  143.         onGround = false;
  144.         Collider2D[] collisionResults = new Collider2D[2];
  145.         int objectsBeneath = Physics2D.OverlapBoxNonAlloc(groundCheck.position, new Vector2(0.9f, 0.3f), 0.0f, collisionResults, layerGround);
  146.         for (int i=0; i <objectsBeneath; i++ ){
  147.             if (!GameObject.ReferenceEquals(gameObject, collisionResults[i].gameObject)){
  148.                 onGround = true;
  149.             }
  150.         }
  151.     }
  152.  
  153.     void checkIfWallSliding(){
  154.         //if(isWallSliding && (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow))){
  155.         if (!onGround){
  156.             RaycastHit2D[] ray = new RaycastHit2D[1];
  157.             int totalRayHits = Physics2D.LinecastNonAlloc(bulletSpawner.position, body.position, ray, 1 << LayerMask.NameToLayer("SolidGround"));
  158.             bool wallFound = totalRayHits > 0 && ray[0].collider.gameObject.tag == "SolidGround";
  159.  
  160.             isWallSliding = wallFound && ( (facingRight && Input.GetKey(KeyCode.RightArrow)) ||  (!facingRight && Input.GetKey(KeyCode.LeftArrow))) ;
  161.         } else {
  162.             isWallSliding = false;
  163.         }
  164.  
  165.     }
  166.  
  167.     void PlayAnimations(){
  168.         if(dead){
  169.             anim.Play("Die");
  170.         }
  171.         else if(dashing){
  172.             anim.Play("Dash");
  173.         }
  174.         else if(takingDamage){
  175.             anim.Play("Damaged");
  176.         }
  177.         else if(isWallSliding){
  178.             if(isGunLoaded){
  179.                 anim.Play("Wall Slide Loaded Gun");
  180.             } else {
  181.                 anim.Play("Wall Slide");
  182.             }
  183.         }
  184.         else if(isGunLoading){
  185.             anim.Play("Load Gun");
  186.         }
  187.         else if(isGunUnloading && body.velocity.x == 0){
  188.             anim.Play("Unload Gun");
  189.         }
  190.         else if (attacking){
  191.             if(onGround){
  192.                 anim.Play("Attack");
  193.             } else {
  194.                 anim.Play("Attack Jump");
  195.             }
  196.         }
  197.         else if (onGround && body.velocity.x != 0){
  198.             if(isGunLoaded){
  199.                 anim.Play("Walk Loaded Gun");
  200.             } else {
  201.                 anim.Play("Run");
  202.             }
  203.         }
  204.        
  205.         else if(onGround && body.velocity.x == 0){
  206.             if(isGunLoaded){
  207.                 anim.Play("Idle Loaded Gun");
  208.             } else {
  209.                 anim.Play("The Bot");
  210.             }
  211.         }
  212.        
  213.         else if (!onGround && body.velocity.y !=0){
  214.             if(isGunLoaded){
  215.                 anim.Play("Jump Loaded Gun");
  216.             } else {
  217.                 anim.Play("Jump");
  218.             }
  219.         }
  220.         else{
  221.             anim.Play("The Bot");
  222.         }
  223.     }
  224.  
  225.     void Flip(){
  226.         facingRight = !facingRight;
  227.         transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z);
  228.     }
  229.  
  230.     void Attack(){
  231.         attacking = true;
  232.         Invoke("StopAttacking", meleeCoolDown);
  233.         Collider2D hitBarrel = Physics2D.OverlapBox(meleeCheck.position, new Vector2(0.6f, 1.4f), 0.0f, layerGround);
  234.         if (hitBarrel && hitBarrel.tag == "ExplosiveBarrel"){
  235.             hitBarrel.BroadcastMessage("TakeDamage",1);
  236.             hitBarrel.gameObject.SetActive(true);
  237.         }
  238.  
  239.     }
  240.  
  241.     void Shoot(){
  242.         if (!isGunLoaded){
  243.             isGunLoading = true;
  244.         } else {
  245.             if(isWallSliding){
  246.                 shotController.Shoot(!facingRight, wallSlideBSpawner.position);
  247.                 //bulletSpawner.transform.position = new Vector2(-1 * bulletSpawner.transform.position.x, bulletSpawner.transform.position.y);
  248.             } else {
  249.                 shotController.Shoot(facingRight, bulletSpawner.position);
  250.             }
  251.             idleGunTime = 0;
  252.         }
  253.     }
  254.  
  255.     void StopAttacking(){
  256.         attacking = false;
  257.     }
  258.  
  259.     void OnFinishLoadingGun(){
  260.         isGunLoading = false;
  261.         isGunUnloading = false;
  262.         isGunLoaded = true;
  263.         Shoot();
  264.     }
  265.  
  266.     void OnFinishUnloadingGun(){
  267.         isGunLoaded = false;
  268.         isGunLoading = false;
  269.         isGunUnloading = false;
  270.     }
  271.  
  272.     void UnloadGun(){
  273.         isGunUnloading = true;
  274.     }
  275.  
  276.     void Damaged(){
  277.         body.velocity = new Vector2(0, body.velocity.y);
  278.         takingDamage = true;
  279.     }
  280.  
  281.     void DamageFinished(){
  282.         takingDamage = false;
  283.     }
  284.  
  285.     public void Die(){
  286.         dead = true;
  287.     }
  288.  
  289.  
  290.  
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement