SImple, but how do I check a game object's position in an If statement?
In my game I have a number of cubes that are invisible placed around the map, I can access their public transforms and I use them to move another placeholder cube to their locations. I want to check, using an if statement, when I reach "Waypoint 01", for example, and if the game object is at "Waypoint 01"'s transform move to "Waypoint 02". Below I shall post the code I have made so far to make the visible placeholder cube move to a single waypoint and my attempt to type out an if statement to make the placeholder cube move to the next waypoint.
Any assistance is much appreciated.
{
public Transform enemyStart;
public Transform waypoint01;
public Transform waypoint01plus;
public Transform waypoint02;
public Transform waypoint03;
public Transform waypoint04;
public Transform waypoint05;
public float speed;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, waypoint01.position, step);
if (transform.position = waypoint01)
{
transform.position = Vector3.MoveTowards(transform.position, waypoint01plus.position, step);
}
}
}
Answer by UnrealSoftware · Jul 21, 2016 at 02:00 PM
There are some problems with your code:
Equality is checked with == in most programming languages, including C#. A single = is used for assignments only.
You compare a Vector3 (transform.position) with a Transform (waypoint01) but you actually want to compare the positions of both
Moreover that check would only kick in when the position is EXACTLY equal to waypoint01's position. This is probably a bad idea due to floating point precision. You should check the distance instead and continue when a certain threshold is reached: if (Vector3.Distance(transform.position, waypoint01.position) < 0.1) { ... }
The stuff in the if condition would only be executed once (or very few times when using my distance change) because the transform would quickly move outside the position so the condition is not fulfilled anymore.
The stuff above the if condition would still be executed always which is also not what you want to happen. You don't want to move towards waypoint01's position if you already have been there.
You should re-think your general approach because your solution doesn't scale. You would have to write a lot of code if you have many waypoints which is a bad thing. Are you familiar with arrays/lists? It would be more clever to have an array or list of waypoints. You would then save the index to the next waypoint you want to reach and always move towards its position. As soon as it is reached you increase the index which makes you move towards the next waypoint. And of course you have to stop (or do something else) when the last waypoint has been reached.
If someone were to use an "if" statement to detect if one object's position is the same as anothers, is that any more or less complicated