Trouble Controlling 18 Servos Arduino MEGA

Hello i have been working on a hexapod for my A-level DT project and i am having trouble with my servos at first they all worked fine but as i added more they started to occasionally move to random directions when i connected the arduino to them i have tried connecting more current to them(currently i am using 6 4xAA battery packs in parallel) but this was ineffective i think it is a problem with the cleanliness of the power so i'm going to try connecting capacitors to each servos but i was wondering if you have a had a similar problem and know how to fix it? here is my code but i'm fairly sure its not a problem with that and if you need i will upload pictures of the bot itself
(STS=Side to side)(UAD=up and down)(foot=ankle control)

#include <Servo.h>
Servo STS1;
Servo STS2;
Servo STS3;
Servo STS4;
Servo STS5;
Servo STS6;
Servo FOOT1;
Servo FOOT2;
Servo FOOT3;
Servo FOOT4;
Servo FOOT5;
Servo UAD1;
Servo UAD2;
Servo UAD3;
Servo UAD4;
Servo UAD5;
Servo UAD6;

void setup()
{
STS1.attach(10); // servo on digital pin 10
STS2.attach(3);
STS3.attach(A1);
STS4.attach(8);
STS5.attach(4);
STS6.attach(A3);
FOOT1.attach(11);
FOOT2.attach(9);
FOOT3.attach(A0);
FOOT4.attach(13);
FOOT5.attach(A2);
UAD1.attach(6);
UAD2.attach(2);
UAD3.attach(5);
UAD4.attach(12);
UAD5.attach(7);
UAD6.attach(A4);
}

void loop()
{
STS1.write(30);   
STS2.write(30);
STS3.write(90);
STS4.write(60);
STS5.write(60);
STS6.write(90);
FOOT1.write(110);
FOOT4.write(100);
FOOT5.write(140);
UAD1.write(50);
UAD4.write(85);
UAD5.write(85);
delay(1000);          
UAD1.write(90);
UAD4.write(50);
UAD5.write(50);
FOOT1.write(140);
FOOT4.write(70);
FOOT5.write(70);
delay(1000);
STS1.write(80);
STS4.write(5);
STS5.write(5);
delay(1000);
STS1.write(80);  
STS4.write(5);
STS5.write(5);
FOOT1.write(110);
FOOT4.write(110);
FOOT5.write(130);
UAD1.write(50);
UAD4.write(100);
UAD5.write(100);
delay(1000);
STS1.write(60);
STS4.write(25);
STS5.write(25);
STS2.write(0);
STS6.write(110);
STS3.write(60);
FOOT2.write(150);
FOOT3.write(150);
UAD2.write(50);
UAD3.write(50);
UAD6.write(50);
delay(1000);
UAD2.write(90);
UAD3.write(90);
UAD6.write(90);
FOOT2.write(180);
FOOT3.write(180);
delay(1000);
STS2.write(50);
STS6.write(60  );
STS3.write(110);
delay(1000);
STS2.write(50);
STS3.write(110);
FOOT2.write(150); 
FOOT3.write(150);
UAD2.write(50);
UAD3.write(50);
UAD6.write(50);
delay(1000);
}

You need SLA or LiPo batteries for that sort of peak current load (it could be 20+A). Your power
wiring has to be up to the task too.

Sounds like you have an inadequate power supply You could set the hexapod on something so the legs are not supporting it and see if the servos move more smoothly. Some of the 4xAA battery packs have small wiring. If you are using rechargeable AA batteries, you probably should use 6xAA instead of 4xAA battery packs, as rechargeable AA batteries only output 1.2v when fully charged.

Actually NiMH are generally 1.35V fully charged, about 1.3V nominal. NiMH are not
the same as NiCd (in particular they have a voltage that varies with state of charge
much more and much less memory effect). Cheap/unbranded NiMH are worth avoiding.

I am running it on a base with non rechargeable batteries so the legs are not on the floor, I plan to get a 2S lipo with a turnigy ubec 8A-15A voltage regulator and hopefully that will sort out the problem. Thank you for the help everyone