It is necessary to make it so that when an object is spawned, it appears at a certain angle, which is constantly changing, but not accidental. When creating-destroying, I did

`for (int I = 0; i < 50; i++){ transform.position = new Vector3 (i+10, 0, transform.position.z) Instantiate(obj [Random.Range (0, obj.GetLength (0))], transform.position, transform.rotation );// появляется случайный объект в такой же позиции, как и раньше, под тем же углом, что и раньше (угол меняется в совсем другом месте) }` 

Here obj [] is an array consisting of a square with a rectangle. And when the square rotates, all subsequent squares appear at the same angle, but the rectangles do not (this should be so). But how to do the same, but from the pool? I removed the Instantiate line and wrote this:

 GameObject newObject = theObjectPools[objectSelector].GetPooledObject ();// в этом пуле хранятся квадрат и прямоугольник newObject.transform.position = transform.position; // позиция появления квадрата или прямоугольника newObject.transform.rotation = transform.rotation; // угол их появления 

In the end, I can make all the objects appear spawn at some angle, but two problems: 1) all objects change the angle, but only squares are needed, 2) Quaternion does not accept float (I can’t tell him to have the same angle, like that square over there). How to make so that only the squares appear with the right angle?

  • maybe it makes sense to somehow divide the methods of the square and the rectangle (for example, a different script and pull the right one). Quaternion accepts float, you just need to use either Euler coordinates (there is an option) or immediately work with habrahabr.ru/post/183908 - pavel
  • Made with a crutch. Created somewhere on top of an object that rotates. And when creating a square (with an angle of 0), it changes its angle to the same one that the object has on top (because the square is created behind the field of view, then the user does not see this movement). If everything goes well with performance, then I’ll leave this option. But I don't like him, so I hope for answers. And about the separation - there was an idea to make the type "if the length of the object is equal to two, then do this and that." But then I thought - you need to rotate not the entire pool (this is bad !!!), but only the object in it ... But as it is, it did not come up after all - Krem Soda
  • I never used the bullets myself :) I used all the handles (just arrays of objects, in 1 bullet objects of type 1). - pavel
  • The fact is that I have to create one of random objects at specific points (with the condition above), and calculate whether it can appear there or not for me too) although it is possible - Krem Soda

1 answer 1

If object A is the parent of objects B and C, then if A is rotated at an opr angle, all objects down the hierarchy will rotate with it. To avoid this, all children need to set transform.parent = null, rotate object A, and then back transform.parent = A.

You, it seems, instead of changing the rotation of the object in the scene, change the prefab rotation. The prefab should set a zero turn, and then instantiate it.

 var instantiated = GameObject.Instantiate(prefab) instantiated.rotation = ...