I have a GameObject that works as an object generator and they are generated along the y-axis, that is, straight, according to this function:
public void Generar() {
Instantiate (obj [Random.Range (0,obj.Length)], transform.position, Quaternion.identity);
InvokeRepeating ("Generar", 3, 180);
}
How can I make them parallel to the x axis? I have already put in the generator the objects that have to be generated in the correct way, parallel to the x axis, but they come out as before, why? i tried to put in above function transform.position.x
but i get error.
Well this is kind of confusing -> How to rotate an object parallel to the y-axis so that it is parallel to the x-axis? , and the code that shows, "but" maybe it's me, but looking at your code and this other part -> How can I make them parallel to the x axis? I will leave you some code so that you can determine or understand what they do, I hope it is what you are asking.
If I understand your question correctly, you want to instantiate it on the x-axis, but you are passing it as a position vector, the position of the object that the script has, in this way ->
transform.position
, I imagine that because of the name of the class, maybe it is a dummy object that you will have for the scene, even though it does not have any associated "mesh" model, it has a transform, hence you can do -> transform.position, well this works but it is taking the position in space where you have the object that the script has, so it will be instantiated at that position:Now try to use
Vector3.right
what will instantiate it in positionx + 1
relative to space, as I imagine that the movement for this object will be controlled by the script of the object in question that is the one you are instantiating or from somewhere else.Still if the instantiation doesn't have the rotation you expect based on the original model, you can use what Arie CwHat says about using
Quaternion.LookRotation(Vector3.up)
try that in addition to the aboveVector3.right
.You say ... I have already put in the generator the objects that have to be generated in the correct way, parallel to the x-axis... - where you have put it.
Suppose you have something like this:
So we use it like this:
Well, as it was said before
transform.position
, you are passing the position of the object that the script has, but if you only want to take the position of the object that the script has, but only inx
this one, you can try something like this:Note the change of
transform,position
for othersVector3
.On the other hand if what you want is to control the rotation with which the object is instantiated you can try something like this:
In the above we use
Vector3 rotar_y
to assign the rotation based on Euler 1But it
Instantiate
requires aQuaternion
for rotation so we can useQuaternion.Euler
it like this,to get a
Quaternion
which is what we use in the methodInstantiate
seen in the code above:1