Hello to everyone
I am creating a dynamic obstacle on a rotating cube in Unity. But the objects I create are sometimes trapezoidal and sometimes flat. How can I make rotation settings?
Hello to everyone
I am creating a dynamic obstacle on a rotating cube in Unity. But the objects I create are sometimes trapezoidal and sometimes flat. How can I make rotation settings?
Can you try to explain in more detail what you’re doing? I don’t understand the problem.
… I think a picture may be worth a thousand words here.
Sorry if that sounds mean (that’s definitely not my intention!) I’m just kinda lost in language soup right now ![]()
I agree. I still can’t make out what you’re trying to do, or what’s going wrong.
But I think maybe what you’re doing could be described this way: you want to instantiate a randomly-chosen prefab, in any of several positions on/around another object. And of course you need to have the right orientation for each position.
Here’s how I would do that.
In your parent object — the cube or whatever you’re instantiating things around — create an empty GameObject child for each spawn position. Set up each one at the correct position and orientation for things to spawn there.
Now in your script, have a public Transform[ ] spawnPositions array. Drag your spawn point GameObjects into this array.
Then when you want to instantiate something, pick a random element of spawnPositions, and copy the position and rotation into your new instance:
Transform where = spawnPositions[Random.Range(0, spawnPositions.Length)];
barrierInstantiate.transform.position = where.position;
barrierInstantiate.transform.rotation = where.rotation;
Ok, this really sounds like a position, rotation, and scale issue to me. Once you parent an object you need to use localPosition, localRotation, and localScale to manipulate the object. You can set the scale in the prefab to avoid having to scale after and even make different prefabs with the correct orientations. Adjusting after shouldn’t be difficult as all faces are 90° from each other for rotations, scaling is most likely a uniform value, and positioning which your going to have to work out on your own.
Lastly, another approach may work for you. Add your objects to the cube in the editor. If you need 5 barriers on a face of the cube add them, orient them, scale them, and align to the cube. Disable all children of the cube. In a script have a reference to each barrier, move a barrier to the location you want and make active. This way all you need is a little 2D positioning to get what you want. Your code shows fixed positions, so why not set them where you want too, and just make them active via script.
Another point to keep in mind: you don’t have to use Instantiate with a prefab in your project. You can use it with an actual GameObject (active or not) in the scene.
I do this all the time… I refer to such things a “prototypes” rather than “prefabs” just to have a clear terminology. A prototype exists in the scene, and (unlike prefabs) it can have references to other objects in the scene, or vice versa. This is often quite convenient.
And in this case, your prototypes could be children of the cube, and so dragged around with it as the cube moves around in the scene. And when you instantiate them, the new instances would already be in the correct position and orientation!