Elevator script not working

Hi guys, I’m trying to do a control Point with two teams blue and red. I want my flag to go up & down a post. when the red team is in the circle the flag goes up and when the blue team comes in the circle the flag goes down change color and goes up with the new color on the flag.

Everything is working except when the flag is supposed to go up after changing color. It just magically appear at the top of the post. Can you guys help me?

public class ControlPoint : MonoBehaviour
{

    //Ne pas oublier de changer le tag du joueur

    public string redTeam = "Player";
    public string blueTeam = "Player1";

    [SerializeField] public float captureTime = 5f;
    [SerializeField] public float capRadius = 5f;

    [SerializeField] public ParticleSystem particle;
    bool particleRed = true;
    bool particleBlue = true;


    public float redCapture;
    public float blueCapture;

    public bool redCaptureStatus;
    public bool blueCaptureStatus;

    Collider[] colliders;


    // flag shader that changes color

    //control shader !!!! plusieurs points = plusieurs shader pour que la couleur ne change pas sur tout les points
    public Material shader;

    //control de la transition + scale
    Vector3 startPosition;
    [SerializeField] GameObject target;
    public GameObject flag;
    Vector3 targetPosition;
    Vector3 scalePosition;
    Vector3 scaleTarget;
    float scaleXSize = 1.5f;
    float timeToReachTarget;
    float temps;
    float tempsDescendre;


    void Start()
    {
        redCaptureStatus = false;
        blueCaptureStatus = false;

        redCapture = 0;
        blueCapture = 0;

        //shader + flags
       
       
        //position
        startPosition = flag.transform.position; //todo get le flag position verifier si sa fonctionne
        targetPosition = target.transform.position;
        timeToReachTarget = captureTime;

        //scale
        scalePosition = flag.transform.localScale;
        scaleTarget = new Vector3(scaleXSize, 1, 1);

    }


    void Update()
    {
        colliders = Physics.OverlapSphere(transform.position, capRadius);

        if (colliders.Length>0 && colliders != null)
        {
            for (int i = 0; i < colliders.Length; i++)
            {
                if (TeamCount(blueTeam, colliders) != TeamCount(redTeam, colliders) || TeamCount(redTeam, colliders) != TeamCount(blueTeam, colliders))
                {
                    if (colliders[i].tag == blueTeam)
                    {
                        if (blueCapture < captureTime)
                        {
                            blueCapture += Time.deltaTime;

                            if (blueCapture > -captureTime)
                            {
                                redCapture -= Time.deltaTime;

                                if (redCapture > blueCapture)
                                {
                                    tempsDescendre += Time.deltaTime / timeToReachTarget;
                                    flag.transform.position = Vector3.Lerp(targetPosition, startPosition, tempsDescendre);
                                    flag.transform.localScale = Vector3.Lerp(scaleTarget, scalePosition, tempsDescendre);
                                }

                                if(blueCapture > redCapture)
                                {
                                 
                                    temps += Time.deltaTime / timeToReachTarget;
                                    flag.transform.position = Vector3.Lerp(startPosition, targetPosition, temps);
                                    flag.transform.localScale = Vector3.Lerp(scalePosition, scaleTarget, temps);
                                    shader.SetColor("Color_E1736ED4", Color.blue);
                                }

                            } 
                        }
                        else
                        {
                            blueCaptureStatus = true;
                            redCaptureStatus = false;
                        }

                    }

Update: I just figured out that when I’m using vector3.MoveTowards it’s working but not really well…

Just from looking at the code, I think what’s happening is “tempsDescendre” and “temps” start out small, but as time passes they grow because of adding “Time.deltaTime” to them. This will cause them to slide slowly and accumulate speed as they travel to their destination.

And I can’t see the code changing “timeToReachTarget” other than setting it to “captureTime”'s value at the start, which also doesn’t seem to change. So you’re always dividing Time.deltaTime by 5.

Depending on the flag pole’s length in units, this could be causing the flag to accumulate too much speed too quickly.

Since you’re using Vector3, you could just use Vector3.Slerp to get a smooth effect (starting slow, accumulating speed, and slowing down as it reaches the top/bottom of the pole).

Also when using MoveTowards, make sure you multiply the third parameter by Time.deltaTime. This could also work for Lerp and Slerp. And I’m now thinking the reason the flag is instantly teleported is because you’re not multiplying “tempsDescendre” by Time.deltaTime in the Lerp functions.