S
S
Svet_Ars2021-03-18 15:05:03
Unity
Svet_Ars, 2021-03-18 15:05:03

Can you help me solve object generation in Unity?

Hello, I have a Unity 2D project and 2D platforms are endlessly generated in it in a certain area (Area is a square 200 x 200), but when the platform goes beyond the square, the object is recreated in a random place of this square and so on until the player moves and does not will lose. (the square is a child object of the Persian and it is constantly moving behind the Persian and therefore the platform is constantly coming out of it and being created in a new random place inside the square). But the thing is, I don't want the player to see how objects are created, that is, so that objects are not created in the player's scope, how can I do this? Is there a ready-made option or logic or is it better for me to show the code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CHIDWI, 2021-03-19
@CHIDWI

You can try using transform.LookAt(target); Or via raycast. First, we check whether the specified coordinates are in the field of view, if not, we place the object in this way, if yes, then we generate new coordinates.

S
Svet_Ars, 2021-03-19
@Svet_Ars

Most likely this is the dumbest solution to this problem, BUT it works. I put a dot in the upper left corner of the camera (an empty object) and in the lower right. And I checked if the object goes beyond their coordinates, then create new coordinates. Below I have given a method that checks all this. If I helped someone, I will be glad. I created 125 objects in the scene that are constantly looking for a new position in real time and the game is consistently giving 300 FPS so I'm fine
with private void ExtraPos()
{
NameLaber1:
float x = Random.Range(ChunkesGenerate.instance.start.transform.position. x, ChunkesGenerate.instance.End.transform.position.x);
float y = Random.Range(ChunkesGenerate.instance.start.transform.position.y, ChunkesGenerate.instance.End.transform.position.y);
var p = new Vector3(x, y, 1f);
if (x >= ChunkesGenerate.instance.cntrl_1.transform.position.x &&
y <= ChunkesGenerate.instance.cntrl_1.transform.position.y &&
x <= ChunkesGenerate.instance.cntrl_2.transform.position.x &&
y >= ChunkesGenerate.instance.cntrl_2.transform.position.y)
{
goto NameLaber1;
}
else
{
transform.position = p;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question