Enemy AI patrol mode question.
I have a simple AI script here that chases a hero whenever it detects a collision on its LEFT or RIGHT Raycast. What doesn't seem to be working ATM is the XOR "^" function in FixedUpdate(). It only Logs when the player is being chased which, is the opposite of what its supposed to do. Am I using XOR correctly, or am I missing something bigger?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyAi : MonoBehaviour {
public float enemyMoveSpeed = 1f;
private bool heroDetectedLeft = false;
private bool heroDetectedRight = false;
void OnTriggerEnter2D(Collider2D other){
if (other.gameObject.CompareTag ("Players")) {
Debug.Log ("Collision detected");
other.gameObject.transform.position = Vector2.up;
// TODO: Hero loses 1 HP.
// TODO: Hero drops X Coins.
}
}
void PatrolMode(){
// TODO: Enemy patrol between platform edges.
// TODO: Setup GroundCheck detection.
}
void GroundCheck(){
// RaycastHit2D floor = Physics2D.Raycast (transform.position, Vector2.down, 1f);
}
void LookLeft(){
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.left,4f);
if (hit.collider != null) {
Debug.Log("Ray hit: "+hit.collider.gameObject.name);
if (hit.collider.tag=="Players") {
Debug.Log ("KILL KILL");
heroDetectedLeft = true;
}
}
else{
heroDetectedLeft = false;
}
}
void LookRight(){
RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right,4f);
if (hit.collider != null) {
Debug.Log ("Ray hit: " + hit.collider.gameObject.name);
if (hit.collider.tag == "Players") {
Debug.Log ("KILL KILL");
heroDetectedRight = true;
}
} else {
heroDetectedRight = false;
}
}
void FixedUpdate()
{
LookLeft ();
LookRight ();
if (heroDetectedLeft ^ heroDetectedRight) {
Debug.Log("Resuming = PatrolMode");
}
if (heroDetectedLeft == true) {
transform.Translate (Vector2.left * enemyMoveSpeed * Time.deltaTime);
}
if (heroDetectedRight == true) {
transform.Translate (Vector2.right * enemyMoveSpeed * Time.deltaTime);
}else {
this.transform.Translate (Vector2.zero);
}
}
}
Answer by Zynek · Nov 07, 2016 at 09:31 AM
If you have doubts about XOR, you can use OR just as well. I dont see any kind of time out on detection, where you would set both heroDetected booleans to false, this way your AI could kinda runaway from your game.
Thats about what i can figure out of what you provided. Maybe post some pictures ? Or specify the problem in more details.
Thanks for the input Zynek, I'm thinking of just adding a Coroutine that checks if both left and right bools are false then trigger a "Patrol" function. For some reason I can't get the XOR bitwise to do what I'm asking.
Oooooh but XOR is exclusive OR - meaning it triggers on odd amount of true. So if i get it right, you´re looking for:
if (heroDetectedLeft && heroDetectedRight) {
// Patrol $$anonymous$$ode
}else if (heroDetectedLeft == true) {
//move Left
} else if (heroDetectedRight == true) {
//move Right
}
Your answer
Follow this Question
Related Questions
If statement not working in script. 3 Answers
Will this work? 0 Answers
how to make an enemy patrol and chase me 0 Answers
Enemy AI. How to make the AI chase the player by using the exact same moves as the player. (2D) 1 Answer
Trying to add a tracker to enemy AI either prefab or with reference 0 Answers