For the game I am making I want to make the enemies move and attack my themselves. Detecting the player is the problem. When I wrote the script it worked well, even after some changes I made to add other stuff (like allowing them to pick up weapons and choose what weapon to go to). Now, all of a sudden it just stopped working. For absolutely no reason. I have no idea what is wrong with it but any help would be appreciated. This is a college project and the deadline is 3 weeks away and I am quite anxious that I might not finish it in time. If I don’t fix it now it’s going to set me back a lot.
Here is the script:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
GameObject Player;
public bool patrol = true, guard = false, clockwise=false;
public bool moving = true;
public bool pursuingPlayer = false, goingToLastLoc = false;
Vector3 target;
Rigidbody2D rid;
public Vector3 playerLastPos;
RaycastHit2D hit;
float speed = 2.0f;
int layerMask = 1<<8;
ObjectManager obj;
GameObject[] weapons;
EnemyWeaponController ewc;
public GameObject weaponToGoTo;
public bool goingToWeapon = false;
public bool hasGun = false;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("Player");
playerLastPos = this.transform.position;
obj = GameObject.FindGameObjectWithTag ("GameController").GetComponent<ObjectManager> ();
ewc = this.GetComponent<EnemyWeaponController> ();
// hit = Physics2D.Raycast(new Vector2(this.transform.position.x,this.transform.position.y), new Vector2(dir.x, dir.y));
rid = this.GetComponent<Rigidbody2D> ();
layerMask = ~layerMask;
}
// Update is called once per frame
void Update () {
if (PlayerHealth.dead == false) {
movement ();
playerDetect ();
canEnemyFindWeapon ();
} else {
this.GetComponent<EnemyAnimate> ().enabled = false;
}
}
void movement()
{
float dist = Vector3.Distance (Player.transform.position, this.transform.position);
Vector3 dir = Player.transform.position - transform.position;
hit = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (dir.x, dir.y), dist, layerMask);
Debug.DrawRay (transform.position, dir, Color.red);
Vector3 fwt = this.transform.TransformDirection (Vector3.right);
RaycastHit2D hit2 = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (fwt.x, fwt.y), 0.8f, layerMask);
Debug.DrawRay (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (fwt.x, fwt.y), Color.cyan);
if(moving == true)
{
transform.Translate (Vector3.right * speed * Time.deltaTime);
}
if(patrol==true)
{
Debug.Log ("Patrolling normally");
speed = 2.0f;
if(hit2.collider != null)
{
//Debug.LogError(hit2.collider.tag);
if (hit2.collider.gameObject.tag == "Wall")
{
//Quaternion rot = this.transform.rotation;
if (clockwise == false) {
transform.Rotate (0, 0, 90);
} else {
transform.Rotate (0, 0, -90);
}
}
}
if (weaponToGoTo != null) {
patrol = false;
goingToWeapon = true;
}
}
if(pursuingPlayer==true)
{
Debug.Log ("Pursuing Player");
speed = 3.0f;
rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2((playerLastPos.y - transform.position.y), (playerLastPos.x - transform.position.x)) * Mathf.Rad2Deg);
if (hit.collider.gameObject.tag == "Player") {
playerLastPos = Player.transform.position;
}
}
if (goingToLastLoc == true) {
Debug.Log ("Checking last known player location");
speed = 2.3f;
rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((playerLastPos.y - transform.position.y), (playerLastPos.x - transform.position.x)) * Mathf.Rad2Deg);
if (Vector3.Distance (this.transform.position, playerLastPos) < 1.5f) {
patrol = true;
goingToLastLoc = false;
}
}
if (goingToWeapon == true) {
speed = 3.2f;
rid.transform.eulerAngles = new Vector3 (0, 0, Mathf.Atan2 ((weaponToGoTo.transform.position.y - transform.position.y), (weaponToGoTo.transform.position.x - transform.position.x)) * Mathf.Rad2Deg);
if (ewc.getCur () != null) {
weaponToGoTo = null;
patrol = true;
goingToWeapon = false;
pursuingPlayer = false;
goingToLastLoc = false;
}
if (weaponToGoTo.active==false || weaponToGoTo==null)
{
weaponToGoTo = null;
patrol = true;
goingToWeapon = false;
pursuingPlayer = false;
goingToLastLoc = false;
}
}
}
void setWeaponToGoTo (GameObject weapon)
{
weaponToGoTo = weapon;
goingToWeapon = true;
patrol = false;
pursuingPlayer = false;
goingToLastLoc = false;
}
void canEnemyFindWeapon()
{
if (ewc.getCur() == null && weaponToGoTo == null && goingToWeapon==false) {
weapons = obj.getWeapons ();
for(int x = 0;x<weapons.Length;x++)
{
float distance = Vector3.Distance (this.transform.position, weapons [x].transform.position);
if (distance < 10) {
Vector3 dir = weapons [x].transform.position - transform.position;
RaycastHit2D wepCheck = Physics2D.Raycast (new Vector2 (this.transform.position.x, this.transform.position.y), new Vector2 (dir.x, dir.y), distance, layerMask);
if(wepCheck.collider.gameObject.tag == "Weapon")
{
setWeaponToGoTo (weapons[x]);
}
}
}
}
}
public void playerDetect()
{
Vector3 pos = this.transform.InverseTransformPoint (Player.transform.position);
if (hit.collider != null)
{
if (hit.collider.gameObject.tag == "Player" && pos.x >1.2f && Vector3.Distance(this.transform.position,Player.transform.position) <9) {
patrol = false;
pursuingPlayer = true;
goingToWeapon = false;
} else {
if(pursuingPlayer==true)
{
goingToLastLoc = true;
pursuingPlayer = false;
goingToWeapon = false;
}
}
}
}
public float getSpeed()
{
return speed;
}
}