Hi guys! Continuing with my roguelike, I stumbled upon grid movement for the player, using a bit of code from Robertbu, mixed in with some of my own code, which works perfectly. I duplicated the code for the AI, with some minor changes. However, the AI doesn’t move the way I need it to. Rather than moving precise grid units, the AI sometimes stops off centre. I looked through my code and I can’t find an exact reason as to why this isn’t working.
This is the code for the spawner of the monsters.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class basicmob : MonoBehaviour {
public List<GameObject> moblist = new List<GameObject>();
public GameObject mob;
public static int monsters;
public GameObject newspawn;
public static bool cleared;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(ControllerScript.counter>=5 monsters<10 !ControllerScript.moving){
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(overseer.nextfloor){
moblist.Clear ();
}
if(ControllerScript.nextturn moblist.Count>0){
foreach(GameObject monster in moblist){
var script=monster.GetComponent<Mobhelper>();
script.turnProcedure();
}
}
}
}
and the code for the monsters themselves
sing 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 patrol(){
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;
}
var chance=Random.Range (0,5);
if (mobMove (transform.position == mobEnd)){
mobMove = false;
ControllerScript.nextturn=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);
}
}
the weirdest thing about the result is that the AI moves slower than the player, despite that the speed variables are the same.
Is there a reason as to why the above things I mentioned are happening, or is there a better way to go about this.
I wasn’t sure whether or not to put this in unity answers, since it’s technically more than one question.
I’m working in c#
Thanks.