How do I add different speeds for different points?

I have the following script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movementPoints : MonoBehaviour {

    public GameObject[] movpoints;
    int current = 0;
    float rotSpeed;
    public float speed;
    float WPradius = 1;

    void Update()
    {
        if (Vector3.Distance(movpoints[current].transform.position, transform.position) < WPradius)
        {
            current++;
            if (current >= movpoints.Length)
            {
                current = 0;
            }
        }
        transform.position = Vector3.MoveTowards(transform.position, movpoints[current].transform.position, Time.deltaTime * speed);

    }
}

This allows me to pick an amount of waypoints where an object would move, it also includes a speed value.

But my problem is, this speed value is constant, I would like to be able to change the speed from waypoint to waypoint, like going from A to B with speed 5, but from B to C with speed 10, how can I do this?

Make a small struct with a waypoint and a speed value:

[Serializable]
public struct Waypoint {
  public float Speed;
  public GameObject Position;
}

Then have an array of those structs instead of plain gameobjects:

public Waypoint[] movpoints;

Then you can access the speed and position as such:

float speed = movpoints[index].Speed;
GameObject position = movepoints[index].Position;
1 Like