- Home /
AI script works, when we add a blocked function it stops working HELP
Hey, me and my friend are making a 2D platformer. My friend works on the AI and I work on the player mechanics. My friend ran into this problem that we can't solve, we are beginners to unity so our script may be sloppy. Our problem is that we made an AI that shoots the player when detected, this works but he would shoot at the player even when he was behind objects. We tried to solve this by making a raycast that tries to detect if something is in the way of the enemy and the player, However, our function that we added completely breaks the enemy's movement and it doesn't even work. I'll post the full script and then the area that breaks the script. Even when we remove the faulty function, the movement of the enemy is still broken. Please help!
public Transform target;
public GameObject bound01;
public GameObject bound02;
public float speed;
public float distance;
public float vision;
public float bulletSpeed;
public float reloadTime;
private float reloadDefault;
public float disengage;
public bool alert;
public float timer;
public bool big;
public float veiwRadius;
public bool justAlert;
public LayerMask doHit;
public LayerMask player;
public LayerMask hl;
public GameObject bullet;
public float enemyHealth = 100f;
private float playerBulletDamage = 25f;
private bool blocked;
//FUNCTIONS
void Start ()
{
alert = false;
reloadDefault = reloadTime;
bound01.SetActive (false);
bound02.SetActive (false);
justAlert = false;
blocked = false;
}
void Update ()
{
if (enemyHealth <= 0) {
Destroy (gameObject);
Debug.Log ("enemy died");
}
reloadTime -= Time.deltaTime;
big = target.gameObject.GetComponent <PlayerScript> ().BIG;
gameObject.GetComponent <Rigidbody2D> ().velocity = new Vector2 (speed, 0);
RaycastHit2D hit = Physics2D.Raycast (gameObject.transform.position, new Vector2 (speed, -1), distance, doHit);
Debug.DrawRay (transform.position, new Vector2 (speed, -1), Color.blue);
if (!hit && !alert) {
speed = -speed;
} else if (!hit && alert) {
if (big) {
speed = Mathf.Sign (target.transform.position.x - transform.position.x);
justAlert = false;
} else {
speed = 0;
justAlert = true;
}
} else if (hit && alert && !justAlert) {
speed = Mathf.Sign (target.transform.position.x - transform.position.x);
} else if (hit && alert && big) {
justAlert = false;
}
if (alert == true) {
RaycastHit2D rey = Physics2D.Raycast (transform.position, target.position - transform.position, vision, hl);
if (rey) {
if (rey.collider.gameObject.name == "Ground") {
blocked = true;
} else {
blocked = false;
}
} else {
blocked = false;
}
RaycastHit2D playerHit = Physics2D.CircleCast (transform.position, veiwRadius, target.position - transform.position, vision, player);
Debug.DrawRay (transform.position, target.position - transform.position, Color.green);
if (playerHit.distance > disengage) {
alert = false;
}
if (playerHit && reloadTime < 0) {
GameObject bulletClone = Instantiate (bullet);
Vector2 bulletDirection = Vector2.zero;
bulletDirection = Vector2.ClampMagnitude (transform.TransformDirection (target.transform.position - transform.position), 1);
bulletClone.transform.position = transform.position;
bulletClone.GetComponent<Rigidbody2D> ().velocity = bulletDirection * bulletSpeed;
reloadTime = reloadDefault;
alert = true;
}
} else {
RaycastHit2D playerHit = Physics2D.CircleCast (transform.position, veiwRadius, new Vector2 (speed, 0), vision, player);
Debug.DrawRay (transform.position, new Vector2 (speed, 0), Color.green);
if (playerHit.distance > disengage) {
alert = false;
}
if (playerHit && reloadTime < 0) {
GameObject bulletClone = Instantiate (bullet);
Vector2 bulletDirection = Vector2.zero;
bulletDirection = Vector2.ClampMagnitude (transform.TransformDirection (target.transform.position - transform.position), 1);
bulletClone.transform.position = transform.position;
bulletClone.GetComponent<Rigidbody2D> ().velocity = bulletDirection * bulletSpeed;
reloadTime = reloadDefault;
alert = true;
}
}
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.name == "Enemy_Boundary_01") {
bound01.SetActive (true);
}
if (other.gameObject.name == "Enemy_Boundary_02") {
bound02.SetActive (true);
}
}
void OnTriggerExit2D (Collider2D other)
{
if (other.gameObject.name == "Enemy_Boundary_01") {
bound01.SetActive (false);
}
if (other.gameObject.name == "Enemy_Boundary_02") {
bound02.SetActive (false);
}
}
public void playerBullet ()
{
enemyHealth = enemyHealth - playerBulletDamage;
}
}
Here is the part that breaks the script:
RaycastHit2D rey = Physics2D.Raycast (transform.position, target.position - transform.position, vision, hl);
if (rey) {
if (rey.collider.gameObject.name == "Ground") {
blocked = true;
} else {
blocked = false;
}
} else {
blocked = false;
}
Your answer
Follow this Question
Related Questions
Send an enemy back to its spawn point using waypoints 2 Answers
Find number of objects between two points 1 Answer
Enemy/Partner AI without rotation 1 Answer
Get Component in RaycastHit game object 1 Answer
Enemy raycast detection 0 Answers