Hi. So i have tried to implement the worm boss from Terraria into my 2D platformer. Basically it works by the head moves and the tail parts all follow the previous tail section and thus forming a link/chain.
Currently it works like a ‘homing missile’ in that it rotates itself. So the head moves as thus:
This mostly works, however if i increase the movespeed or rotate speed it bugs out. I want the boss to have a high speed but a low rotate speed, so that it makes ‘passes’ at Players in that it tries to hit the Player then swings back by rotating slowly. But my code just flat out bugs out at high speeds because the tails keep getting separated.
I have also tried to just use Vector3.MoveTowards (shown below) for the tail sections movement but the tails also seem to get separated here.
void BodyMovement(){
int i = 0;
foreach (var body in bodies)
{
if (i == 0){
body.transform.position = Vector3.MoveTowards(body.transform.position, transform.position, speed);
}
else{
body.transform.position = Vector3.MoveTowards(body.transform.position, bodies[i-1].transform.position, bodySpeed);
}
i++;
}
}
Also help to smooth this out would be much appreciated.
So did a quick a test & you want something like this:
//Target is the piece infront (As a Vector3)
//body is the current piece (As a GameObject)
//Look at the target (Due to being in 2d, we want to change the up vector not the forward)
body.transform.rotation = Quaternion.LookRotation(Vector3.forward, (target - body.transform.position).normalized);
//Our actual target is how close we are allowed to be behind that piece
target = target - (body.transform.up * minBodyDist);
//Get the current distance from our target
float dist = (target - body.transform.position).magnitude;
//Move towards the target, never allowing us to go farther away than the maxBodyDist
body.transform.position = Vector3.MoveTowards(body.transform.position, target, Mathf.Lerp(0.0f, minBodyDist, dist/maxBodyDist));
//Need to lerp to the distance we are away or we won't catch up
body.transform.position = Vector3.MoveTowards(body.transform.position, target, Mathf.Lerp(0.0f, dist, dist/maxBodyDist));