- Home /
Generate cubes in 5 areas
i have a plane surface with 5 vector3 for a position on the surface for a cube.When the game starts the cube move towards me with a randomly generated position of the 5 vector 3's. i want that when the cube's position is between 2 points on the x surface , i want the cube to Instanciate and again pick one of the 5 positions and again start moving towards me and this should continue in a cycle.
This is the code i am using to pick one of the 5 positions.
public Vector3 Gen(Vector3 sam)
{
int posStart = Random.Range(0, 5);
if (posStart == 0)
{
sam = one;
}
else if (posStart == 1)
{
sam = two;
}
else if (posStart == 2)
{
sam = three;
}
else if (posStart == 3)
{
sam = four;
}
else if (posStart == 4)
{
sam = five;
}
return sam;
}
i can move the cube, no problem there
This is the code for instanciation
if (enemy.transform.position.x >= 1.49 && enemy.transform.position.x <= 1.5) { Instantiate(enemy);
}
This works some times and fails the other times. Some times it does this process 2-4 times and then it just stops generating
Any help would definitely be appreciated. Thanks!!
Answer by Darkforge317 · Dec 01, 2017 at 06:11 AM
Your cube's speed may be too fast for a range of 0.1 units. You're checking if it's position is between 1.49 and 1.50. It's a very small range.
If your cube moves too fast then it's position may go from 1.48 to 1.51. It all depends on the speed.
Other than that, your code looks completely fine to me, but although we're only seeing a small portion of it so there may be a problem elsewhere.
see if the cube is moving too fast then how come some times it IS generating the cubes and sometimes it is not Anyway thanks for the reply,but see if you can help me Thanks
Since you're calling this method from the update method (I'm assu$$anonymous$$g you are) then each frame is completed either faster or slower than the previous one. No guarantees on anything when it comes to frame-rate.
Sorry if you don't like my answer... But that's honestly what I think the problem is, because I can't see your scene and I've only seen a small portion of your code.
Yeah actually that might be the problem See basically i want the cube to be instanciated only once in any range between 1.45-1.5 See if you can help me further Thanks for any reply
Your answer
Follow this Question
Related Questions
How to instantiate a custom sized object? 1 Answer
Problem with repeating objects in array when already in the array 0 Answers
Place cubes? problem with my script :S 1 Answer
Block ground generation 2 Answers
Unity not Instantiating Prefabs Properly 0 Answers