- Home /
Using Vector3.MoveTowards with a list of waypoints
Hi all, I am trying to make a GameObject follow a certain path of waypoints by a roll dice.
I.E: Dice roll is 3, traverse a list of waypoints until it reaches the last one.
The code I have works but the GameObjects moves from the current waypoint it is in, to the last one, skipping the rest of waypoints in the middle.
void Start()
{
_waypoints = _waypointsController.GetWaypoints();
_currentWaypoint = _waypoints[0].transform;
print("Starting: " +_currentWaypoint.name);
_currentIndex = _waypoints.IndexOf(_currentWaypoint.gameObject);
print("Starting index: " + _currentIndex);
transform.position = _currentWaypoint.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_diceRoll = _diceManager.RollDice();
print("Roll dice was: " + _diceRoll);
_isMoving = true;
}
MovePlayer();
if (Vector3.Distance(transform.position, _currentWaypoint.position) > 0.1f)
{
transform.position = Vector3.MoveTowards(transform.position, _currentWaypoint.position, _moveSpeed * Time.deltaTime);
}
}
private void MovePlayer()
{
if (_isMoving)
{
print("Starting waypoint and index: " + _currentWaypoint.name + " " + _currentIndex);
for (int k = 0; k <= _diceRoll - 1; k++)
{
if (_waypoints.IndexOf(_currentWaypoint.gameObject) < _waypoints.Count - 1)
{
_currentWaypoint = _waypoints[_currentIndex + 1].transform;
print("New waypoint" + _currentWaypoint.name);
}
else
{
_currentWaypoint = _waypoints[0].transform;
}
_currentIndex = _waypoints.IndexOf(_currentWaypoint.gameObject);
}
print("Ending waypoint and index: " + _currentWaypoint.name + " " + _currentIndex);
_diceRoll = 0;
print("Stop moving");
_isMoving = false;
}
}
I have tried putting the MoveTowards inside the MovePlayer method but it doesn't work, game object stutters each space press.
Any insight is appreciated.
My understanding of your code is that MovePlayer() will take you to the last waypoint directly, because your for loop will not allow movement along the waypoint. Update() needs to finish for every position along the way.
I'm not entirely certain, but maybe you can use something from my waypoint movement code in Update().
int iCurWP = 1;
float fWPTime = 0f;
Vector3 vVel;
//move along waypoints
if (iNumWayPoints > 1)
{
fWPTime -= Time.fixedDeltaTime;
if (fWPTime <= 0)
{
//find out velocity to next wp
Vector3 vDist = new Vector3(vWayPoints[iCurWP].x - vPos.x,
vWayPoints[iCurWP].y - vPos.y, 0);
float fDist = vDist.magnitude;
fWPTime = fDist / iSpeed;
vVel = new Vector3(vDist.x / fWPTime, vDist.y / fWPTime, 0);
iCurWP = (iCurWP + 1) % iNumWayPoints;
}
vPos += vVel * Time.fixedDeltaTime; //move along velocity
}
//set object to position vPos here
Your answer

Follow this Question
Related Questions
how to make a block move in one direction,Make a Object such as a Block move in one direction 1 Answer
Something wrong with the movement and the rotation 0 Answers
Translating Player Movement and Clipping Through Walls 2 Answers
How to get Horizontal axis value to 0 instantly after releasing the positive button!!!! 0 Answers