Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dudoser7722 · May 18, 2020 at 08:48 AM · aiwalkingmaze

[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]);
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · May 18, 2020 at 03:48 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Any recommendations for having randomized obstacles for a nav mesh? 1 Answer

grand theft auto People 1 Answer

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

NavMesh Agent Controlled AI Sliding Toward next waypoint 0 Answers

Maze Game Pathfinding AI 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges