- Home /
How to instantiate an object with a set rotation in one axis, but the other two axes inherited from the parent?
I'm trying to spawn an object with the x axis rotated to 90, but the y and z axes having the same rotation of the spawner. The spawner runs through random items (in this case, it's for decorative flags) and then spawns it.
The reason I need it to be 90 on the x axis is because of how cloths are orientated by default. To have the object up a wall, I need to rotate it. If I instantiate it with transform.rotation, it assumes I also want x, so it gets messy as it can get stuck in walls and the like.
Here's my code; however, it ignores the spawner's y and z and flags always face the same direction!
#pragma strict
var partsPrefab : Transform[];
function Start ()
{
var z = transform.rotation.z;
var y = transform.rotation.y;
var rot = Quaternion.Euler(Vector3(90, y, z));
Instantiate(partsPrefab[Random.Range(0,partsPrefab.Length)], transform.position, rot);
Destroy(gameObject);
}
function Update () {
}
It's my first time working with angles like this, though, so I'm a bit lost. Is there any way I can have x = 90, but y and z be transform.rotation?
Any help would be appreciated!
have you tried Quaternion.identity ins$$anonymous$$d of just "rot" ?
He literally said he wants to set the x axis rotation to 90, obviously Quaternion.identity isn't the answer mate
Did u try to put Scripts on ur Sub-Gameobject, that contain that Information of Rotation, etc. in the start or awake function?
Answer by highpockets · May 25, 2019 at 11:02 AM
You are not getting the euler angles when you are setting your z and y floats. You are getting the z and y values of the quaternion. The z and y of a quaternion relate to the axis of a rotation and have nothing to do with euler angles which are in degrees. To get the euler angles of the z and y, you need to use: var z = transform.rotation.eulerAngles.z;
.
Having said that though, I would think that Quaternion.LookRotation would give you what you need. You just specify the forward direction (and a hinted up direction for more control if needed) and you're set.
Your answer
Follow this Question
Related Questions
Accelerating in Turned Direction 2 Answers
transform.Rotate is rotating all axes, why? 0 Answers
Setting rotation parallel to ground 1 Answer
Have Head Bone Rotate with Camera 2 Answers
Flip over an object (smooth transition) 3 Answers