- Home /
[GT]How to make WORKING artificial intelligence of a wandering minotaur?
Hello everybody I want to make an adversary who wanders through the maze (he should also theoretically move exactly in the middle of the walls between him), and when he comes across a player, he starts chasing after him, but this has not yet been realized. I wrote this script a lot of time, and it did not give me anything - AI constantly turns into a wall. Can you help me with ideas on how to make such a wandering AI without using NavMeshAgent and invisible points scattered on the map, to the coordinates of which the AI will go. P.S. For those who still decided to recreate the situation - the distance between the walls is 25, the passage width is also 25, the whole map is 250 by 250, the height of the walls is 10 and the AI has a size of 5-5-5.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
public class EnemyScripts : MonoBehaviour
{
public GameObject player;
// public Camera camera;
public float speedmove = 10f;
public float speedrot = 30f;
public float seerangeF = 15f;
public float seerangeT = 15f;
public float attackrange = 2f;
public float radiusF = 2f;
public float radiusT = 2f;
//public Animator animator;
private float turn = 1;
private bool roturn = true;
private float rot;
public float rotime = 1f;
public LayerMask layer;
public BoxCollider collider;
public string state;
public Vector3[] sizefix;
private float posX;
private float posZ;
public float postime=0.2f;
private int lowflex=0;
void Start()
{
speedmove *= Time.deltaTime;
speedrot *= Time.deltaTime;
// transform.position = new Vector3(56.25f,5f,247.5f);
}
void FixedUpdate()
{
if (transform.position.x >= 62f || transform.position.x <= -62f || transform.position.z >= 362f || transform.position.z <= 238f)
transform.position = new Vector3(56.25f, 5f, 247.5f);
RaycastHit hitF;
RaycastHit hitRight;
RaycastHit hitLeft;
RaycastHit hitVision;
// Debug.Log(transform.localPosition);
Physics.BoxCast(transform.position, transform.localScale+sizefix[0], transform.TransformDirection(new Vector3(0,0,1)), out hitF);
Physics.BoxCast(transform.position, transform.localScale + sizefix[1], transform.TransformDirection(Vector3.left), out hitLeft);
Physics.BoxCast(transform.position, transform.localScale + sizefix[1], transform.TransformDirection(Vector3.right), out hitRight);
// Debug.Log(hitLeft.collider.gameObject.name + " " + hitLeft.distance);
//Debug.Log(hitF.collider.gameObject.name + " " + hitF.distance);
Physics.SphereCast(transform.position, radiusT, transform.TransformDirection(Vector3.forward), out hitVision);
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hitF.distance, Color.red);
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left)*hitLeft.distance, Color.green);
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right)*hitRight.distance, Color.blue);
// Debug.Log("F " + hitF.collider.gameObject.name + " " + hitF.distance);
Debug.Log("L " + hitLeft.distance+" and "+hitRight.distance);
//Debug.Log("R " + hitRight.collider.gameObject.name + " " + hitRight.distance);
// Debug.Log(hitRight.collider.gameObject.transform.position.x+" and "+hitLeft.collider.gameObject.transform.position.x);
if (hitVision.collider.gameObject.tag == "Player")
state = "chase";
else state = "walk";
// Debug.Log(state);
StartCoroutine(PosFix());
// else transform.position = new Vector3(posXZ[0], transform.position.y, transform.position.z);
if (state == "walk")
{
if (hitF.distance >= seerangeF && hitLeft.distance >= seerangeT && hitRight.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(1));
transform.Translate(0, 0, speedmove);
}
else if (hitF.distance >= seerangeF && hitLeft.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(2));
transform.Translate(0, 0, speedmove);
}
else if (hitF.distance >= seerangeF && hitRight.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(3));
transform.Translate(0, 0, speedmove);
}
else if (hitLeft.distance >= seerangeT && hitRight.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(4));
transform.Translate(0, 0, speedmove);
}
else if (hitLeft.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(90));
transform.Translate(0, 0, speedmove);
}
else if (hitRight.distance >= seerangeT)
{
if (roturn)
StartCoroutine(Rotate(-90));
transform.Translate(0, 0, speedmove);
}
else if (hitF.distance >= seerangeF)
{
transform.Translate(0, 0, speedmove);
}
if(hitF.distance<=seerangeF){
Debug.Log("distanse<2f");
transform.eulerAngles += new Vector3(0, 180f, 0);
StartCoroutine(LowFlex());
}
}
else if (state == "chase")
{
RaycastHit hitP;
Physics.Raycast(transform.position,player.transform.position,out hitP);
Vector3 dir = (player.transform.position - transform.position);
float angle = Vector3.Angle(dir, transform.forward);
transform.position += (hitP.point - transform.position) * speedmove*0.1f;
Debug.Log(transform.rotation);
}
IEnumerator Rotate(int type)
{
roturn = false;
switch (type)
{
case 1:
Debug.Log("1");
if (Random.Range(1, 3) == 1)
rot = -90f;
else if (Random.Range(1, 3) == 2) rot = 90f;
else rot = 0f; break;
case 2:
Debug.Log("2");
if (Random.Range(1, 2) == 1)
rot = 90f;
else rot = 0f; break;
case 3:
Debug.Log("3");
if (Random.Range(1, 2) == 1)
rot = -90f;
else rot = 0f; break;
case 4:
Debug.Log("4");
if (Random.Range(1, 2) == 1)
rot = 90f;
else rot = -90f; break;
case 90: Debug.Log("5"); rot = -90f; break;
case -90: Debug.Log("6"); rot = 90f; break;
}
transform.eulerAngles += new Vector3(0, rot, 0);
yield return new WaitForSecondsRealtime(rotime);
roturn = true;
}
IEnumerator PosFix()
{
yield return new WaitForSecondsRealtime(postime);
if (transform.rotation.y == 0f && (hitRight.distance < hitLeft.distance)&&(hitRight.distance<2f&&hitLeft.distance<2f))
transform.Translate(-0.1f, 0, 0);
else if (transform.rotation.y == 0f && (hitRight.distance > hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(0.1f, 0, 0);
else if (transform.rotation.y == 180f && (hitRight.distance > hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(-0.1f, 0, 0);
else if (transform.rotation.y == 180f && (hitRight.distance < hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(0.1f, 0, 0);
if ((transform.rotation.y > 10f || transform.rotation.y < 170f) && (hitRight.distance < hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(-0.1f, 0, 0);
else if ((transform.rotation.y > 10f || transform.rotation.y < 170f) && (hitRight.distance > hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(0.1f, 0, 0);
else if ((transform.rotation.y > -10f || transform.rotation.y < -170f) && (hitRight.distance > hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(0.1f, 0, 0);
else if ((transform.rotation.y > -10f || transform.rotation.y < -170f) && (hitRight.distance < hitLeft.distance) && (hitRight.distance < 2f && hitLeft.distance < 2f))
transform.Translate(-0.1f, 0, 0);
}
}
IEnumerator LowFlex()
{
lowflex++;
yield return new WaitForSecondsRealtime(4f);
if (lowflex > 1)
{
seerangeT = 1f;
sizefix[1].z = 0.1f;
// rotime=1f;
}
lowflex = 0;
StartCoroutine(LowFlexCont());
}
IEnumerator LowFlexCont()
{
yield return new WaitForSeconds(0.5f);
sizefix[1].z = 0.9f;
seerangeT = 10f;
// rotime = 0.2f;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
RaycastHit hitF;
RaycastHit hitLeft;
Physics.SphereCast(transform.position, radiusF, transform.TransformDirection(Vector3.forward), out hitF);
Physics.SphereCast(transform.position, radiusT, transform.TransformDirection(Vector3.left), out hitLeft);
Gizmos.DrawWireCube(hitF.point,transform.localScale+sizefix[0]);
Gizmos.DrawWireCube(hitLeft.point, transform.localScale + sizefix[1]);
}
}
make sure your rays are not hitting your $$anonymous$$otaur, cause then they will always be true.. not sure what is wrong with invisible points.. You could generate them on the fly and use them as targets for your Ai.. Or you can use flowfeild vectors, I have an asset for that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612234553im_/https://answers.unity.com/themes/thub/images/avi.jpg)