Snake like movement in 2D space
I am attemtping to archive snake movement with a list of circles using Lerp.
The head uses a script for moving it forward and left and right, while the other body parts lerp to the previous body part using a for loop head:
void MoveHead()
{
if (ButtonMove.instance == null) return;
if (isAlive)
{
if (ButtonMove.instance.isPressed)
{
if (MaxTurnSpeed < turnSpeed)
{
turnSpeed = MaxTurnSpeed;
}
playerBody[0].GetComponent<Rigidbody2D>().velocity = new Vector2(turnSpeed, forwardSpeed);
turnSpeed += turnValue;
startedPressing = true;
}
else if (!startedPressing)
{
playerBody[0].GetComponent<Rigidbody2D>().velocity = new Vector2(0, forwardSpeed);
}
else
{
if (-MaxTurnSpeed > turnSpeed)
{
turnSpeed = -MaxTurnSpeed;
}
playerBody[0].GetComponent<Rigidbody2D>().velocity = new Vector2(turnSpeed, forwardSpeed);
turnSpeed -= turnValue;
}
}
}
and body:
void MoveBody()
{
for (int i = 1; i < playerBody.Count; i++)
{
Vector3 temp = playerBody[i - 1].transform.position;
temp.y -= spawnOffset;
playerBody[i].transform.position = Vector3.Lerp(playerBody[i].transform.position, temp, lerpSpeed);
}
}
This movement is snake-like, but I wish to archive, that every body part crosse the same path as the head. I tried that with a for loop in Update, where I set the positin to every bodypart to the transform of the previous body part, but then they would be stuck together.
Any idea or suggestion is welcome
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612134035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Lerping on Y axis? 0 Answers
DOTween sequence snapping 0 Answers
sprite bug: animator doesnt work and it gets bigger 0 Answers
Object movement algorithm 0 Answers
Can't make non-smooth movement script 0 Answers