How to make enemies bob up and down on moving

Hello! I am currently trying to make a ghostly 2D enemy that bobs up and down as it moves along, in a 2.5D shooter. I wrote a simple movement script which works fine, but when I tried to add a bobbing effect it doesn’t seem to work. Honestly the maths of bobbing is slightly above my head, so I just used the same script that I used for camera bobbing. Any advice on how to adapt it to move the enemy model would be greatly appreciated! :slight_smile:

void Update()
{

target = player.position;
target.y = transform.position.y;
    
       if(Vector3.Distance(transform.position, target) > stoppingDistance && Vector3.Distance(transform.position, target) < chasingDistance || (provoked))
       {
        agent.destination = target;
       }
       else if (Vector3.Distance(transform.position, target) < stoppingDistance && Vector3.Distance(transform.position, target) > retreatDistance)
       {
        agent.destination = transform.position; 
       }
       else if (Vector3.Distance(transform.position, target) < retreatDistance)
       {
        agent.destination = -target;   
       }

//Below is the script for the bobbing effect

       if (agent.destination != transform.position) //try to make bob when moving
      {
       timer += bobSpeed * Time.deltaTime;
       Vector3 newPosition = new Vector3(Mathf.Cos(timer) * bobAmount, restPosition.y + Mathf.Abs((Mathf.Sin(timer) * bobAmount)), restPosition.z); 
       enemyPosition = newPosition;
      } 
       else
     {
      timer = Mathf.PI / 2; 
      Vector3 newPosition = new Vector3(Mathf.Lerp(enemyPosition.x, restPosition.x, transitionSpeed * Time.deltaTime), Mathf.Lerp(enemyPosition.y, restPosition.y, transitionSpeed * Time.deltaTime), Mathf.Lerp(enemyPosition.z, restPosition.z, transitionSpeed * Time.deltaTime)); 
      enemyPosition = newPosition;
     }
      if (timer > Mathf.PI * 2) 
            {
             timer = 0;
            }

}

You can use animations.