Unable to make 2D enemy zigzag : Top Down View

Hello All,

I’m currently working on some enemy AI scripts. I’m working on an enemy that dashes toward the player in a zigzag pattern when the player reaches it’s collider, Unfortunately the zigzag movement turned out to be a harder task than imagined. The enemy is constantly following the player but only zigzags when triggered to. Think of a geometry wars style game.


  • I’ve looked into questions over zigzag movement, but none of them helped in anyway.
  • I’ve taken a look into the Mathf.PingPong and I think it’s something that will be very useful for this script.
  • I’ve also tried the sine movement but had less luck then with the previous method.

I’ll post my code down below. Any help would be really appreciated - i’ve been stuck on this single script for toooo long


Note: Most the script below is disabled. I’ve just been trying to get the enemy to zigzag towards the player by all means. My current thoughts are that I could somehow make it pingpong on the Y axis since the AI moves toward the player using it’s X axis, but the enemy will not chase the player - it only pings and pongs.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyZigzag : MonoBehaviour
{
    // Start is called before the first frame update
    Vector3 zigEnd; //(player.position - transform.position) * 2
    Transform player;
    float dist;
    public float enemySpd = 4f;
    public float zigSpeed = 10f;
    bool follow = true;
    public Collider2D col;
    public Rigidbody2D rb;
    Vector2 vecX = new Vector2(1, 0);


    void Start()
    {
        player = GameObject.FindWithTag("Player").transform;


    }

    // Update is called once per frame
    void Update()
    {
        // Set the x position to loop between 0 and 3
        
        Vector2 zigzag = new Vector2(Mathf.PingPong(Time.time * zigSpeed, 3) , transform.position.y);
        zigzag = transform.TransformPoint(transform.position);
        transform.position = zigzag;

        //FollowPlayer();
    }

    void FollowPlayer()
    {
        
        dist = Vector2.Distance(player.position, transform.position);

        if (follow == true)
        {
            transform.right = player.position - transform.position;
            rb.velocity = transform.TransformDirection(vecX * enemySpd);
        }

    }

   /* private void OnTriggerEnter2D(Collider2D collision)
    {
        
        if (collision.CompareTag("Player"))
        {
            
            follow = false;
            
            bool zigDir = false;
            float zigSeg = dist / zigSteps;

            for(int i = 0; i < zigSteps; i++)
            {
                zigDir = !zigDir;
                if (zigDir == true)
                
                zigEnd = transform.position.normalized * zigSeg;
                while (transform.position != (transform.position).normalized + zigEnd)
                {
                
                    transform.right = player.position - transform.position;
               }
           }

        }
        
    }*/

}

this code should work if you want the object to go to a constant position (the position of the target when it started the movement) if youwant to follow the playe position you remove the targetposition and acces the target current position of the transform but this wont have a linear speed, also i am using the X axis as pingpong axis, and try setting the speed to 0.1 in inspector for testing

Vector3 targetPosition;
public Transform target;
Vector3 startPosition;
float percentage;
public float speed = 0.1f;
bool moving;

void Start() { InitMovement(); }

void Update()
{
    if (moving)
    {
        transform.position = Vector3.Lerp(startPosition, targetPosition, percentage);
        transform.position += new Vector3(Mathf.PingPong(Time.time, 2), 0, 0);

        percentage += speed * Time.deltaTime;
    }
}

void InitMovement()
{
    percentage = 0;
    startPosition = transform.position;
    targetPosition = target.position;
    moving = true;
}