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 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 bool dead;
- 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;
- public float dirZ;
- public bool lookY;
- public bool lookX;
- public float ang;
- public Vector2 bjV;
- public Vector2 soldierV;
- public bool attack;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- transform.position = wp[wpI].transform.position;
- ang = 0.0f;
- }
- // Update is called once per frame
- void Update()
- {
- bj = GameObject.FindWithTag("BJ").transform;
- dirX = dir.x;
- dirY = dir.y;
- dirZ = dir.z;
- scale = transform.localScale;
- mov = new Vector3(dirX, dirY, dirZ).normalized;
- if (patrol == true)
- {
- Patrol();
- }
- dir = wp[wpI].position - transform.position;
- float angle = Mathf.Atan2(dirX, dirY) * Mathf.Rad2Deg;
- dir.Normalize();
- mov = dir;
- //b.rb.velocity = this.mov;
- mov = new Vector2(dirX * bSpeed, dirY * bSpeed);
- if (dead == true)
- {
- Dead();
- }
- if (enemySighted == true && timer > initTimer )
- {
- attack = true;
- //firepoint.position = transform.position + mov.normalized;
- Shoot();
- }
- rb.freezeRotation = true;
- /*Vector2 tDir = bj.position - this.transform.position;
- Vector2 forward = this.transform.position;
- float ang = Vector2.SignedAngle(tDir, forward);
- if(ang <= 90)
- {
- anim.SetFloat("Y", -dirY);
- }
- if(ang > 90)
- {
- anim.SetFloat("Y", dirY);
- }
- if (ang <= 90)
- {
- anim.SetFloat("X", -dirX);
- }
- if (ang > 90)
- {
- anim.SetFloat("X", dirX);
- }*/
- /*if (mov.x == 1)
- {
- //firepoint.transform.position += transform.right * speed * Time.deltaTime;
- firepoint.transform.position = new Vector3(1.0f, 0f, 0f);
- }
- else if (mov.x == -1)
- {
- //firepoint.transform.position = -transform.right * speed * Time.deltaTime;
- firepoint.transform.position = new Vector3(-1.0f, 0f, 0f);
- }
- if (mov.y == 1)
- {
- //firepoint.transform.position += transform.up * speed * Time.deltaTime;
- firepoint.transform.position = new Vector3(0f, 1.0f, 0f);
- }
- else if (mov.y == -1)
- {
- //firepoint.transform.position = -transform.up * speed * Time.deltaTime;
- firepoint.transform.position = new Vector3(0f, -1.0f, 0f);
- }*/
- timer += Time.deltaTime;
- }
- private void FixedUpdate()
- {
- if (enemySighted)
- {
- Vector3 temp = Vector3.MoveTowards(transform.position, bj.position, speed * Time.deltaTime);
- firepoint.position = transform.position + mov.normalized;
- //FollowPlayer(temp += transform.position);
- }
- //firepoint.position = transform.position + mov.normalized;
- }
- 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);
- mov = new Vector2(dirX * bSpeed, dirY * bSpeed);
- 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";
- }
- public void Shoot()
- {
- //firepoint.position = transform.position + mov.normalized;
- //anim.SetTrigger("Attack");
- timer = 0;
- var bullet = Instantiate(bullets, firepoint.position, firepoint.rotation).GetComponent<Bullet>();
- //Instantiate(bullets, firepoint.position, Quaternion.identity);
- bullet.Dir = 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;
- float angleX = Vector2.SignedAngle(this.gameObject.transform.position, bj.transform.position);
- if (angleX > 0)
- {
- anim.SetFloat("X", dirX);
- }
- if (angleX < 0)
- {
- anim.SetFloat("X", -dirX);
- }
- 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