Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Trouble with enemy AI

$
0
0
I've been working on enemy AI for my game. Movement is constrained to a grid, and the idea is that when the player moves one space, the enemies move one space, like the classic dungeon crawler. I came up with a code for it, but the enemies won't move. this is the code: Monster manager: using UnityEngine; using System.Collections; using System.Collections.Generic; public class basicmob : MonoBehaviour { public List moblist = new List(); public GameObject mob; public static int monsters; public GameObject newspawn; public Mobhelper script; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(ControllerScript.counter>5 && monsters<10){ GameObject[] mobPosition; mobPosition= GameObject.FindGameObjectsWithTag("room"); var mobPlace=Random.Range(0,mobPosition.Length); Transform target= mobPosition[mobPlace].transform; newspawn=(GameObject)Instantiate(mob, target.position+(Vector3.up/2), target.rotation) ; moblist.Add(newspawn); ControllerScript.counter=0; monsters++; } if(ControllerScript.nextturn==true){ foreach(GameObject mob in moblist){ script.turnProcedure(); ControllerScript.nextturn=false; } } } } and the enemies themselves have this code: using UnityEngine; using System.Collections; public class Mobhelper : MonoBehaviour { private float speed=1.0f; public GameObject player; private Vector3 mobEnd; private bool mobMove; void Start(){ } void Update(){ } public void turnProcedure(){ patrol(); } void attack(){ } void chase(){ } void patrol(){ var chance=Random.Range (1,4); bool hittingForward=false; if(Physics.Raycast(transform.position, Vector3.forward, 1)){ hittingForward=true; } bool hittingRight=false; if(Physics.Raycast(transform.position, Vector3.right, 1)){ hittingRight=true; } bool hittingLeft=false; if(Physics.Raycast(transform.position, Vector3.left, 1)){ hittingLeft=true; } bool hittingBackwards=false; if(Physics.Raycast(transform.position, -Vector3.forward, 1)){ hittingBackwards=true; } if (mobMove && (transform.position == mobEnd)){ mobMove = false; } if(!mobMove && chance==1 && hittingForward==false){ mobMove = true; mobEnd = transform.position + Vector3.forward; } if(!mobMove && chance==2 && hittingRight==false){ mobMove = true; mobEnd = transform.position + Vector3.right; } if(!mobMove && chance==3 && hittingLeft==false){ mobMove = true; mobEnd = transform.position + Vector3.left; } if(!mobMove && chance==4 && hittingBackwards==false){ mobMove = true; mobEnd = transform.position - Vector3.forward; } transform.position = Vector3.MoveTowards(transform.position, mobEnd, Time.deltaTime * speed); } } I've done some debugging work, and I don't think the issue is with the first code, but I could be wrong. Any help would be appreciated.

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>