A box is going to follow a simple path (Doesn't work).
So I am trying to make a box follow a path consisting of 5 empty game objects scattered around the map. The 5 empty GameObjects names are Waypoint 1 up to Waypoint 5. I have doubled checked at least 5 times. Whenever I run my code I get no error, but my Box Object just spins around. Debug.log(currentwaypoint) is always zero which is understandable. Even though I can probably find a code on the internet that has zero problems, I want to understand why this doesn't work when it seems 100% logical in my eyes.
using UnityEngine;
using System.Collections;
public class Moveto : MonoBehaviour {
public Transform[] waypoints = new Transform[5];
float speed = 50;
int currentwaypoint = 0;
void Start () {
waypoints[0] = GameObject.Find("Waypoint1").transform;
waypoints[1] = GameObject.Find("Waypoint2").transform;
waypoints[2] = GameObject.Find("Waypoint3").transform;
waypoints[3] = GameObject.Find("Waypoint4").transform;
waypoints[4] = GameObject.Find("Waypoint5").transform;
}
void Update () {
if (currentwaypoint < waypoints.Length) {
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentwaypoint].position, speed * Time.deltaTime);
transform.LookAt(waypoints[currentwaypoint]);
Debug.Log(currentwaypoint);
if (gameObject.transform.position == waypoints[currentwaypoint].position)
{
currentwaypoint++;
}
}
}
}
Code works fine for me. Double check the waypoints in the scene.
Answer by vadithiyan · Mar 18, 2016 at 08:21 PM
The code works fine, tested it just now. Check if your waypoints are named correctly. If it still spins around in the same place, try changing
(gameObject.transform.position == waypoints[currentwaypoint].position)
to something like
if(Vector3.SqrMagnitude(waypoints[currentwaypoint].position - gameObject.transform.position) < 0.0001f)
to account for floating point precision.
I had the empty GameObjects as children to my Box but decided to make them parent less and it worked now. I still don't see why that shouldn't work though, but then again I don't have a lot of experience. It turns in instant though so I will have to work on that.
When you move the parent object, the child objects move along with the parent, so when you make a parent object move towards the child it will never reach the child. The same goes for rotation, any rotation applied to the parent will be translated to the child with the parent as a pivot. So the child(the way point) kept moving as you updated the parent, hence the continuous rotation.
Oh, yea. That's true, thanks. I actually should have thought of that, but I have been sick for 4 days so I'll let it slide for now :P
Your answer
![](https://koobas.hobune.stream/wayback/20220612072219im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How would you move from a array like from [2] to [3] or backwards like [2] to [1] 2 Answers
Problems with jumping in Doodle Jump like game 2 Answers
Press the button play music in the next scene. 1 Answer
Index out of range error 1 Answer
Detecting a continuous set of objects in a tile based game (ie pipes, walkways, walls) 0 Answers