Problem with SmoothDamp when applied to each child of a gameObject

Hello, I’m new to unity and, with friends, we want to make a Slither.io like game.
We made a Player Controller for our snake and we have a problem with the last 2/3 spheres composing our snake wihch don’t follow the trajectory of the rest of the body, whereas the rest of the body works fine.
Our snake is made like follows : an empty GameObject composed of a head (ball in the code below) and the body parts.

public class PlayerControler : MonoBehaviour {
        private Rigidbody rb;
        public float speed;
        Transform ball;
        private Vector3 movementVelocity;
      
        void FixedUpdate () {
          
            ball = transform.GetChild(0).transform;
            if(Input.GetKey(KeyCode.Mouse0)){
                speed = 20;
            } else {
                speed = 10;
            }
          
            if(Input.GetKey(KeyCode.Q)) {
                ball.transform.Rotate(0, Time.deltaTime * (-180), 0);
            }
            if(Input.GetKey(KeyCode.D)) {
                ball.transform.Rotate(0, Time.deltaTime * 180, 0);
            }
          
            Move();
        }
      
        void Move(){
            for (int i = transform.childCount-1; i>=0; i--){
                Transform t=transform.GetChild(i);
              
                if (i == 0){
                    t.position += ball.transform.forward * speed * Time.fixedDeltaTime;
                } else {
                    t.position = Vector3.SmoothDamp(t.position, transform.GetChild(i-1).transform.position, ref movementVelocity, 1.9f*Time.fixedDeltaTime, Mathf.Infinity, Time.fixedDeltaTime);
                }
            }
        }
    }

We think it’s related to the SmoothDamp but we can’t figure out why.
Thanks for your help.

It looks like you’re passing the same ‘MovementVelocity’ for each call to SmoothDamp - you’d need a different one for each sphere. (as it needs that data for each subsequent call to SmoothDamp).

You could set up MovementVelocity as an array (with the number of spheres as the size) and then just pass it as MovementVelocity[i]

1 Like

It worked !
Thank you very much !
I admit that i didn’t took time to look at what this variable does.
Anyway !
Thanks again !