why does test 1 run smoothly but test 2 appears to not run at all? this is just basic enemy movement system patrolling the area by walking left to right. My guess is that the for loops are ran on 1 frame while the if statements checks each frame and decrements or increments the x value by 1 each frame.
Test 1:
public class test : MonoBehaviour
{
bool patrollingLeft = false;
float x;
Vector3 pos;
void Start()
{
pos = transform.position;
}
void Update()
{
if (patrollingLeft)
{
x--;
if (x == 0) patrollingLeft = false;
}
else
{
x++;
if (x == 100) patrollingLeft = true;
}
pos.x = x;
transform.position = pos;
}
}
test2:
public class test2 : MonoBehaviour {
Vector3 pos;
float x;
void Start () {
pos = transform.position;
}
// Update is called once per frame
void Update () {
transform.position = pos;
for (float x = 0; x < 100; x++)
{
pos.x = x;
}
// Patrol left.
for (float x = 100; x > 0; x--)
{
pos.x = x;
}
}
}
I think its that your changing the value in test 1 but in test 2 your just telling for every x = 100 blah blah blah, equal the position of X. By the way for loops can run every frame and once I believe. Im not an expert or veteran in coding but that should be the case.
Your for loop in test2 does not effect the scene during the loop iteration (ie, the entire loop completes before anything is rendered or seen by unity).
Doing pos.x = x; in terms of what happens in the scene has no effect at all. Unity does not ‘see’ what you’re doing there, it only acts on the state of the transform at the END of the Update() call, and does not have knowledge of what’s happening while IN the update loop (if that makes sense, it’s hard to explain).
As far as Unity is concerned these two are exactly the same:
public class test2 : MonoBehaviour {
Vector3 pos;
float x;
void Start () {
pos = transform.position;
}
// Update is called once per frame
void Update () {
transform.position = pos;
for (float x = 0; x < 100; x++)
{
pos.x = x;
}
// Patrol left.
for (float x = 100; x > 0; x--)
{
pos.x = x; // the last assignment here is the only thing Unity will see but even that won't be realised
// until the next update call since you're only setting the value up the top of Update().
}
}
}
and
public class test2 : MonoBehaviour {
Vector3 pos;
float x;
void Start () {
pos = transform.position;
}
// Update is called once per frame
void Update () {
// on first call to update sets the transform position to pos which was set in Start()
// on all subsequent calls to update will set the transform's x position to 1
transform.position = pos;
// set the position vector's x to 1 but this has no effect until transform.position = pos; is called
// ie, on the next call to Update()
pos.x = 1;
}
}
So it’s important to understand that only the RESULT from what happens in Update() is seen by Unity, all that work that is going on in your loops is totally ignored and only the END RESULT from it is seen.
Note, this is not a Unity thing, that’s how code works. All your loop does is assign a value to a variable 100 times and has no other effect.