Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TheBot : MonoBehaviour {
- public float speed;
- public int jumpForce;
- public Transform groundCheck;
- public Transform meleeCheck;
- public Transform bulletSpawner;
- public Transform wallSlideBSpawner;
- public LayerMask layerGround;
- public float meleeCoolDown;
- public float meleeDamage;
- private Rigidbody2D body;
- private Animator anim;
- private Dash dashController;
- private Shooter shotController;
- private float unloadWaitingTime = 60 * 3;
- private float idleGunTime = 0;
- private bool facingRight = true;
- private bool onGround = true;
- private bool jumping = false;
- private bool attacking = false;
- private bool dead = false;
- private bool isGunLoaded = false;
- private bool isGunLoading = false;
- private bool isGunUnloading = false;
- private bool takingDamage = false;
- private bool dashing = false;
- private bool isWallSliding = false;
- private bool wallJumping = false;
- void Start () {
- body = GetComponent<Rigidbody2D>();
- anim = GetComponent<Animator>();
- dashController = GetComponent<Dash>();
- shotController = GetComponent<Shooter>();
- }
- void Update () {
- PlayAnimations();
- CheckIfGrounded();
- checkIfWallSliding();
- dashing = dashController.IsDashing();
- if (Input.GetButtonDown("Jump") && onGround && !isGunLoading && !jumping && !takingDamage && !isWallSliding){
- jumping = true;
- }
- if(Input.GetButtonDown("Jump") && !onGround && !isGunLoading && !takingDamage && !wallJumping && isWallSliding){
- wallJumping = true;
- Debug.Log("detecting walljump input");
- }
- if (Input.GetButtonDown("Melee") && !attacking && !isGunLoading){
- Attack();
- }
- if(Input.GetButtonDown("Ranged") && !attacking && !isGunLoading){
- Shoot();
- }
- if(Input.GetButtonDown("Dash") && !attacking && !isGunLoading && onGround){
- dashController.DashTo(facingRight? Dash.RIGHT : Dash.LEFT);
- }
- if(isGunLoaded){
- idleGunTime += Time.deltaTime;
- if (idleGunTime >= unloadWaitingTime){
- UnloadGun();
- }
- }
- }
- void FixedUpdate(){
- if(!takingDamage){
- float move = Input.GetAxis("Horizontal");
- //while charachter is wall sliding, slowly fall
- if (isWallSliding && !wallJumping && body.velocity.y < -0.7f){
- body.velocity = new Vector2(body.velocity.x, -0.7f);
- }
- if(!dashing){
- if(onGround){
- //if not dashing on on ground, walk with normal speed
- body.velocity = new Vector2(move * speed, body.velocity.y);
- } else {
- //if character is not on ground, reduce the speed so he doesn't jump too far away
- float airControlAccelerationLimit = 1.5f; // Higher = more responsive air control
- float airSpeedModifier = 0.7f; // the 0.7f in your code, affects max air speed
- float targetHorizVelocity = move
- * speed
- * airSpeedModifier; // How fast we are trying to move horizontally
- float targetHorizChange = targetHorizVelocity
- - body.velocity.x; // How much we want to change the horizontal velocity
- float horizChange = Mathf.Clamp(
- targetHorizChange ,
- -airControlAccelerationLimit ,
- airControlAccelerationLimit ); // How much we are limiting ourselves
- // to changing the horizontal velocity
- body.velocity = new Vector2(body.velocity.x + horizChange, body.velocity.y);
- }
- }
- if((move < 0 && facingRight) || (move > 0 && !facingRight) ){
- //control direction character is facing
- Flip();
- }
- if (jumping){
- body.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
- if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)){
- //if is moving while jumping, reduce jump height
- body.velocity = new Vector2(body.velocity.x, body.velocity.y*0.8f);
- }
- onGround = false;
- jumping = false;
- }
- else if (wallJumping){
- Debug.Log("attempting walljump");
- float velocityX = body.velocity.x + (0.5f * jumpForce) * (facingRight? -1 : 1);
- body.velocity = new Vector2(velocityX, jumpForce * 0.55f);
- wallJumping=false;
- isWallSliding=false;
- }
- }
- }
- void CheckIfGrounded(){
- onGround = false;
- Collider2D[] collisionResults = new Collider2D[2];
- int objectsBeneath = Physics2D.OverlapBoxNonAlloc(groundCheck.position, new Vector2(0.9f, 0.3f), 0.0f, collisionResults, layerGround);
- for (int i=0; i <objectsBeneath; i++ ){
- if (!GameObject.ReferenceEquals(gameObject, collisionResults[i].gameObject)){
- onGround = true;
- }
- }
- }
- void checkIfWallSliding(){
- //if(isWallSliding && (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow))){
- if (!onGround){
- RaycastHit2D[] ray = new RaycastHit2D[1];
- int totalRayHits = Physics2D.LinecastNonAlloc(bulletSpawner.position, body.position, ray, 1 << LayerMask.NameToLayer("SolidGround"));
- bool wallFound = totalRayHits > 0 && ray[0].collider.gameObject.tag == "SolidGround";
- isWallSliding = wallFound && ( (facingRight && Input.GetKey(KeyCode.RightArrow)) || (!facingRight && Input.GetKey(KeyCode.LeftArrow))) ;
- } else {
- isWallSliding = false;
- }
- }
- void PlayAnimations(){
- if(dead){
- anim.Play("Die");
- }
- else if(dashing){
- anim.Play("Dash");
- }
- else if(takingDamage){
- anim.Play("Damaged");
- }
- else if(isWallSliding){
- if(isGunLoaded){
- anim.Play("Wall Slide Loaded Gun");
- } else {
- anim.Play("Wall Slide");
- }
- }
- else if(isGunLoading){
- anim.Play("Load Gun");
- }
- else if(isGunUnloading && body.velocity.x == 0){
- anim.Play("Unload Gun");
- }
- else if (attacking){
- if(onGround){
- anim.Play("Attack");
- } else {
- anim.Play("Attack Jump");
- }
- }
- else if (onGround && body.velocity.x != 0){
- if(isGunLoaded){
- anim.Play("Walk Loaded Gun");
- } else {
- anim.Play("Run");
- }
- }
- else if(onGround && body.velocity.x == 0){
- if(isGunLoaded){
- anim.Play("Idle Loaded Gun");
- } else {
- anim.Play("The Bot");
- }
- }
- else if (!onGround && body.velocity.y !=0){
- if(isGunLoaded){
- anim.Play("Jump Loaded Gun");
- } else {
- anim.Play("Jump");
- }
- }
- else{
- anim.Play("The Bot");
- }
- }
- void Flip(){
- facingRight = !facingRight;
- transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z);
- }
- void Attack(){
- attacking = true;
- Invoke("StopAttacking", meleeCoolDown);
- Collider2D hitBarrel = Physics2D.OverlapBox(meleeCheck.position, new Vector2(0.6f, 1.4f), 0.0f, layerGround);
- if (hitBarrel && hitBarrel.tag == "ExplosiveBarrel"){
- hitBarrel.BroadcastMessage("TakeDamage",1);
- hitBarrel.gameObject.SetActive(true);
- }
- }
- void Shoot(){
- if (!isGunLoaded){
- isGunLoading = true;
- } else {
- if(isWallSliding){
- shotController.Shoot(!facingRight, wallSlideBSpawner.position);
- //bulletSpawner.transform.position = new Vector2(-1 * bulletSpawner.transform.position.x, bulletSpawner.transform.position.y);
- } else {
- shotController.Shoot(facingRight, bulletSpawner.position);
- }
- idleGunTime = 0;
- }
- }
- void StopAttacking(){
- attacking = false;
- }
- void OnFinishLoadingGun(){
- isGunLoading = false;
- isGunUnloading = false;
- isGunLoaded = true;
- Shoot();
- }
- void OnFinishUnloadingGun(){
- isGunLoaded = false;
- isGunLoading = false;
- isGunUnloading = false;
- }
- void UnloadGun(){
- isGunUnloading = true;
- }
- void Damaged(){
- body.velocity = new Vector2(0, body.velocity.y);
- takingDamage = true;
- }
- void DamageFinished(){
- takingDamage = false;
- }
- public void Die(){
- dead = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement