Character Not Moving Properly NEED HELP
I'm attempting to write an AI for an enemy so that every second he moves to a new available square. I have him placed at a point on a tile map with a script attached to him. Since I don't want him to be trying to pick a new direction every frame I did some research and it appears the best option for this is to do a coroutine so that it's an action that doesn't execute every single frame. I've been trying for hours writing this different ways in the update function, in IEnumerator, having all sorts of checks turned on, but so far my character always seems to either be bouncing around like a pingpong ball or standing still. This is the code I have in the coroutine right now.
IEnumerator move()
{
List<string> available = new List<string>();
available.Add("North");
available.Add("South");
available.Add("East");
available.Add("West");
if (tilemap.GetTile(new Vector3Int(currentX, currentY + 1, 0)) == null || tilemap.GetTile(new Vector3Int(currentX, currentY + 1, 0)).name != floor || myStack.Contains(new int[] { currentX, currentY + 1 }))
{
available.Remove("north");
}
if (tilemap.GetTile(new Vector3Int(currentX, currentY - 1, 0)) == null || tilemap.GetTile(new Vector3Int(currentX, currentY - 1, 0)).name != floor || myStack.Contains(new int[] { currentX, currentY = 1 }))
{
available.Remove("south");
}
if (tilemap.GetTile(new Vector3Int(currentX - 1, currentY, 0)) == null || tilemap.GetTile(new Vector3Int(currentX - 1, currentY, 0)).name != floor || myStack.Contains(new int[] { currentX - 1, currentY }))
{
available.Remove("west");
}
if (tilemap.GetTile(new Vector3Int(currentX + 1, currentY, 0)) == null || tilemap.GetTile(new Vector3Int(currentX + 1, currentY, 0)).name != floor|| myStack.Contains(new int[] { currentX + 1, currentY }))
{
available.Remove("east");
}
yield return new WaitForSeconds(1f);
if (available.Count > 0)
{
int random = Random.Range(0, (available.Count));
string direction = available[random];
switch (direction)
{
case "north":
moveVector.y = 1f;
moveVector.x = 0;
currentY = currentY + 1;
break;
case "south":
moveVector.y = -1f;
moveVector.x = 0;
currentY = currentY - 1;
break;
case "east":
moveVector.y = 0;
moveVector.x = -1f;
currentX = currentX + 1;
break;
case "west":
moveVector.y = 0;
moveVector.x = 1f;
currentX = currentX - 1;
break;
}
transform.position = new Vector3Int(currentX, currentY, 0);
moveState = MoveState.Walk;
anim.SetFloat("moveX", moveVector.x);
anim.SetFloat("moveY", moveVector.y);
anim.SetBool("isMoving", true);
myStack.Push(new int[] { currentX, currentY });
}
else
{
int[] test = myStack.Pop();
currentX = test[0];
currentY = test[1];
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612152051im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I have a colider error 1 Answer
Character Movement bug. 0 Answers
About Character moviment methods 0 Answers
Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer