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 Soldier : MonoBehaviour
- {
- public bool dead;
- public Animator anim;
- public float speed;
- public Rigidbody2D rb;
- public Vector3 mov;
- public Vector3 dir;
- public bool patrol;
- public List<Transform> wp;
- public int wpI = 0;
- public float dist = 0.2f;
- public float timer = 0.5f;
- public float initTimer = 0.5f;
- public GameObject bullets;
- public Transform firepoint;
- public Bullet b;
- public Vector2 direction;
- public float bSpeed = 10;
- public bool enemySighted;
- public bool flip;
- public Transform bj;
- public Vector3 scale;
- public float dirX;
- public float dirY;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- anim = GetComponent<Animator>();
- transform.position = wp[wpI].transform.position;
- }
- // Update is called once per frame
- void Update()
- {
- dirX = dir.x;
- dirY = dir.y;
- scale = transform.localScale;
- mov = new Vector3(dirX, dirY).normalized;
- if (patrol == true)
- {
- Patrol();
- }
- dir = wp[wpI].position - transform.position;
- float angle = Mathf.Atan2(dirX, dirY) * Mathf.Rad2Deg;
- dir.Normalize();
- mov = dir;
- anim.SetFloat("X", dirX);
- anim.SetFloat("Y", dirY);
- anim.SetBool("Dead", dead);
- firepoint.position = transform.position + mov.normalized;
- b.rb.velocity = this.mov;
- mov = new Vector2(dirX * bSpeed, dirY * bSpeed);
- if (dead == true)
- {
- Dead();
- }
- if(enemySighted == true && timer > initTimer)
- {
- Shoot();
- }
- timer += Time.deltaTime;
- }
- private void FixedUpdate()
- {
- if (enemySighted)
- {
- Vector3 temp = Vector3.MoveTowards(transform.position, bj.position, speed * Time.deltaTime);
- FollowPlayer(temp += transform.position);
- }
- }
- void Patrol()
- {
- rb.velocity = new Vector2(dir.x * speed, dir.y * speed);
- transform.position = Vector3.MoveTowards(transform.position, wp[wpI].transform.position, speed * Time.deltaTime);
- if(Vector3.Distance(transform.position, wp[wpI].transform.position)<dist)
- {
- wpI++;
- if(wpI>=wp.Count)
- {
- wpI = 0;
- }
- }
- }
- void Dead()
- {
- rb.velocity = Vector2.zero;
- /*rb.constraints = RigidbodyConstraints2D.FreezePositionX;
- rb.constraints = RigidbodyConstraints2D.FreezePositionY;
- rb.constraints = RigidbodyConstraints2D.FreezeRotation;*/
- patrol = false;
- transform.gameObject.tag = "DeadSoldier";
- }
- private void Shoot()
- {
- anim.SetTrigger("Attack");
- timer = 0;
- var bullet = Instantiate(bullets, firepoint.position, Quaternion.identity).GetComponent<Bullet>();
- //Instantiate(bullets, firepoint.position, Quaternion.identity);
- bullet.Dir = this.mov;
- }
- public void SetAnimFloat(Vector2 setVector)
- {
- anim.SetFloat("X", setVector.x);
- anim.SetFloat("Y", setVector.y);
- }
- public void FollowPlayer(Vector2 direction)
- {
- //bj = GameObject.FindWithTag("BJ").transform;
- if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
- {
- if (direction.x > 0)
- {
- SetAnimFloat(Vector2.right);
- }
- else if (direction.x < 0)
- {
- SetAnimFloat(Vector2.left);
- }
- }
- else if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y))
- {
- if (direction.y > 0)
- {
- SetAnimFloat(Vector2.up);
- }
- else if (direction.y < 0)
- {
- SetAnimFloat(Vector2.down);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement