Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Zombie : EnemyController
- {
- public Rigidbody2D theRB;
- public Transform target;
- public Transform homePos;
- public float chaseRadius, attackRadius, grabRadius;
- public float grabTimer;
- public Animator anim;
- // Start is called before the first frame update
- void Start()
- {
- currentState = EnemyStates.idle;
- anim = GetComponent<Animator>();
- theRB = GetComponent<Rigidbody2D>();
- target = GameObject.FindWithTag("Player").transform;
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- CheckDistance();
- }
- public virtual void CheckDistance()
- {
- if (Vector3.Distance(target.position, transform.position) <= chaseRadius
- && Vector3.Distance(target.position, transform.position) > attackRadius)
- {
- // keep for monster attack
- // transform.position = Vector3.MoveTowards(transform.position, target.position, attackRadius);
- if (currentState == EnemyStates.idle || currentState == EnemyStates.walk && currentState != EnemyStates.stagger)
- {
- Vector3 temp = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
- ChangeAnim(temp - transform.position);
- theRB.MovePosition(temp);
- ChangeState(EnemyStates.walk);
- }
- } else if (Vector3.Distance(target.position, transform.position) <= chaseRadius
- && Vector3.Distance(target.position, transform.position) <= grabRadius)
- {
- if (currentState == EnemyStates.walk && currentState != EnemyStates.stagger)
- {
- StartCoroutine(GrabCo());
- }
- }
- }
- public void ChangeState(EnemyStates newState)
- {
- if(currentState != newState)
- {
- currentState = newState;
- }
- }
- private void setAnimFloat(Vector2 setvec)
- {
- anim.SetFloat("MoveX", setvec.x);
- anim.SetFloat("MoveY", setvec.y);
- }
- public void ChangeAnim(Vector2 dir)
- {
- if(Mathf.Abs(dir.x) > Mathf.Abs(dir.y))
- {
- if(dir.x > 0)
- {
- setAnimFloat(Vector2.right);
- } else if (dir.x < 0)
- {
- setAnimFloat(Vector2.left);
- }
- } else if (Mathf.Abs(dir.x) < Mathf.Abs(dir.y))
- {
- if (dir.y > 0)
- {
- setAnimFloat(Vector2.up);
- }
- else if (dir.y < 0)
- {
- setAnimFloat(Vector2.down);
- }
- }
- }
- public IEnumerator GrabCo()
- {
- PlayerController.instance.theSR.enabled = false;
- PlayerController.instance.moveSpeed = 0;
- anim.SetBool("Grab", true);
- if (Input.GetKeyUp(KeyCode.Space))
- {
- isPressed++;
- if(isPressed > 0)
- {
- theRB.AddForce(transform.position * grabForce * 5);
- anim.SetBool("Grab", false);
- currentState = EnemyStates.walk;
- PlayerController.instance.theSR.enabled = true;
- PlayerController.instance.moveSpeed = 7;
- yield return new WaitForSeconds(1f);
- } else
- {
- isPressed--;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement