- Home /
Problem is not reproducible or outdated
How enemy AI follows player without diagonal movement? (2D)
Hello,
I'm trying to make a game like pac-man and i want my enemy to follow my player if my player goes into its range. I used way points to move my enemies and want them to follow my player until my player leaves the range. After that it should go back to patrolling. However, in my game if the player and enemies are at the same horizontal line(same y axis), enemy moves unit by unit (very slowly). If the player and enemies are at the same vertical line (same x axis), enemy waits until my character stops then rushes into the character.
I need my enemy to move vertically or horizontally according to my player.
void FixedUpdate() {
Vector3 direction = player.position - transform.position;
if (transform.position != waypoints[cur].position) {
if (Distance() < 4f) {
if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y) && Mathf.Abs(direction.x) > Mathf.Epsilon) {
direction.y = 0;
}
else if(Mathf.Abs(direction.y) < Mathf.Abs(direction.x) && Mathf.Abs(direction.y) > Mathf.Epsilon) {
direction.x = 0;
}
transform.position = Vector2.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
}
else {
Vector2 movePoint = transform.position = Vector2.MoveTowards(transform.position, waypoints[cur].position, speed * Time.deltaTime);
GetComponent<Rigidbody2D>().MovePosition(movePoint);
}
}
else {
cur = (cur + 1) % waypoints.Length;
}
}
I actually manage to solve that with grid based movement, it is more useful for my game.
Follow this Question
Related Questions
How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers
How to make slippery/smooth movement in Unity 2D? 3 Answers
Snake Movement Problem 0 Answers
Enemy Is not moving 1 Answer
How do I stop moving when attacking? 1 Answer