I have a spaceship that is moving straight on the z axis. I have a variable that records its position on the z axis every frame. I want to instantiate objects (as enemies) in front of the ship when its position on the z axis goes up.
I tried for example to instantiate a shpere 10 meters distance in front of the ship, when the ship crosses 3 meters. Here is the script:
var speed = 1.0;
var posZ = 0.0;
var sphere : Transform;
function Update () {
//move forward according to given "speed"
transform.Translate ( 0, 0, speed * Time.deltaTime);
//the position of the ship on z axis
posZ = transform.position.z;
//instantiate enemy when the ship has moved forward 3 meters
if (posZ == 3.0) {
Instantiate (sphere, Vector3(0, 0, posZ+10), Quaternion.identity);
}
}
but it does not instantiate the sphere. I tried to change the the if statement into if (posZ >3.0), but then instantiates a sphere everyframe (it was expected though :!: ). Does anyone has a clue what I’m doing wrong??
Yes, I forgot to mention this. Comparing floating point values should almost never be done using ==, because as StarManta pointed out, it’s usually incredibly unlikely that two floats will have the same value (unless you’ve set them both explicitly).
Basically, you don’t want to be checking whether floats are equal in this case, even using Mathf.Approximately(). That function is still only useful when numbers are nearly the same. In your case, the numbers will almost never be the same or even close because the frame rate varies and moves you a different amount every frame.
Think of it this way: using == or Mathf.Approximately() is like waiting for someone to step on a specific crack in the sidewalk. Most of the time, people will step right over it. What you actually care about is whether they’ve passed that point, so you should use an inequality.
By that I assume you mean that you get no script errors, it’s just that Instantiate is never called? I’m not sure how close two numbers have to be to get a true result returned from Approximately and my immediate guess is that your posz goes from being below the threshold to above it, never being quite approximately equal to each other (as the Approximately function sees things). Perhaps you can use some Debug.Log() statements to output the posz values to see what they look like?
Alternatively, why not go with posz > someNumber, and have someNumber increase as the game progresses:
var speed = 1.0;
var posZ = 0.0;
var triggerZ = 3.0;
var sphere : Transform;
function Update () {
//move forward according to given "speed"
transform.Translate ( 0, 0, speed * Time.deltaTime);
//the position of the ship on z axis
posZ = transform.position.z;
//instantiate enemy when the ship has moved forward 3 meters
if (posZ > triggerZ) {
Instantiate (sphere, Vector3(0, 0, posZ+10), Quaternion.identity);
triggerZ += triggerZ;
}
}
That way you don’t Instantiate every frame after passing z = 3.0, instead it will call Instantiate when you pass 3.0, then again at 6.0, then 9.0, and so on.
Sorry, that was more of a general comment about == and not intended to apply here. In this case, since you’re dealing with Time.deltaTime and who knows what the framerate could be, it’s likely not even Mathf.Approximately equal.
I see what is going on. Thanx everyone for the ==, Mathf.Aproximately lesson!!
HiggyB, thanx for the alternative script, though I think it does the same with Muriac’s script. But it’s always good to see the same idea scripted in a different way.
I was thinking that maybe I should place invisible walls across the level and when the ship triggers them instantiate the enemies. Do you think that it would be a good idea? If yes, how can I be sure that those walls won’t be triggered by anything else (missiles, enemies) but the ship???
Doh, I’ll have to learn to read better before posting. FWIW, I like Muriac’s solution better anyway. Heh.
As to your new question about triggers, you’d just have to check what body is intersecting the trigger and only respond if it’s the player’s ship (or whatever other bodies you want to trigger the instantiation - if there are any).
Alright then!! I’m going to try that path then. Maybe it will take more time than the first idea but I think that the scripts and the enemies will be more organized if I want to track and change something later in the development.