Advertisement
GoodNoodle

Zombie.cs

Sep 19th, 2021 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6.  
  7. public class Zombie : EnemyController
  8. {
  9.    
  10.     public Rigidbody2D theRB;
  11.     public Transform target;
  12.     public Transform homePos;
  13.     public float chaseRadius, attackRadius, grabRadius;
  14.     public float grabTimer;
  15.     public Animator anim;
  16.  
  17.     // Start is called before the first frame update
  18.     void Start()
  19.     {
  20.         currentState = EnemyStates.idle;
  21.         anim = GetComponent<Animator>();
  22.         theRB = GetComponent<Rigidbody2D>();
  23.         target = GameObject.FindWithTag("Player").transform;
  24.     }
  25.  
  26.     // Update is called once per frame
  27.     void FixedUpdate()
  28.     {
  29.         CheckDistance();
  30.     }
  31.  
  32.     public virtual void CheckDistance()
  33.     {
  34.         if (Vector3.Distance(target.position, transform.position) <= chaseRadius
  35.          && Vector3.Distance(target.position, transform.position) > attackRadius)
  36.         {
  37.             // keep for monster attack
  38.             // transform.position = Vector3.MoveTowards(transform.position, target.position, attackRadius);
  39.             if (currentState == EnemyStates.idle || currentState == EnemyStates.walk && currentState != EnemyStates.stagger)
  40.             {
  41.                 Vector3 temp = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
  42.                 ChangeAnim(temp - transform.position);
  43.                 theRB.MovePosition(temp);
  44.                 ChangeState(EnemyStates.walk);
  45.             }
  46.         }  else if (Vector3.Distance(target.position, transform.position) <= chaseRadius
  47.        && Vector3.Distance(target.position, transform.position) <= grabRadius)
  48.         {
  49.             if (currentState == EnemyStates.walk && currentState != EnemyStates.stagger)
  50.             {
  51.                 StartCoroutine(GrabCo());
  52.             }
  53.          }
  54.         }
  55.  
  56.     public void ChangeState(EnemyStates newState)
  57.     {
  58.         if(currentState != newState)
  59.         {
  60.             currentState = newState;
  61.         }
  62.     }
  63.     private void setAnimFloat(Vector2 setvec)
  64.     {
  65.         anim.SetFloat("MoveX", setvec.x);
  66.         anim.SetFloat("MoveY", setvec.y);
  67.     }
  68.  
  69.     public void ChangeAnim(Vector2 dir)
  70.     {
  71.         if(Mathf.Abs(dir.x) > Mathf.Abs(dir.y))
  72.         {
  73.             if(dir.x > 0)
  74.             {
  75.                 setAnimFloat(Vector2.right);
  76.             } else if (dir.x < 0)
  77.             {
  78.                 setAnimFloat(Vector2.left);
  79.  
  80.             }
  81.         } else if (Mathf.Abs(dir.x) < Mathf.Abs(dir.y))
  82.         {
  83.             if (dir.y > 0)
  84.             {
  85.                 setAnimFloat(Vector2.up);
  86.             }
  87.             else if (dir.y < 0)
  88.             {
  89.                 setAnimFloat(Vector2.down);
  90.             }
  91.         }
  92.     }
  93.  
  94.  public IEnumerator GrabCo()
  95.     {
  96.         PlayerController.instance.theSR.enabled = false;
  97.         PlayerController.instance.moveSpeed = 0;
  98.        
  99.         anim.SetBool("Grab", true);
  100.        
  101.             if (Input.GetKeyUp(KeyCode.Space))
  102.         {
  103.             isPressed++;
  104.             if(isPressed > 0)
  105.             {
  106.                 theRB.AddForce(transform.position * grabForce * 5);
  107.                 anim.SetBool("Grab", false);
  108.                 currentState = EnemyStates.walk;
  109.                 PlayerController.instance.theSR.enabled = true;
  110.                 PlayerController.instance.moveSpeed = 7;
  111.                 yield return new WaitForSeconds(1f);
  112.                
  113.              
  114.                
  115.              
  116.              
  117.  
  118.             } else
  119.             {
  120.                 isPressed--;
  121.             }
  122.         }    
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement