# Rotation questions (Solved)

Hi, I’m pretty new to Unity, so I have some basic rotation questions.

A) how do I set an object’s rotation in JavaScript? To be clear, I’m not asking how to rotate an object, I’m asking how to SET the rotation (as in, set it to an absolute value rather than a relative one).

B) How can I randomize said rotation?

you set it through blabla.transform.rotation = xxx
this expects a quaternion

if you just want it to set and have euler angles you can use blabla.transform.eulerAngles = xxx

You can get a randomized rotation by using Random.rotation.

–Eric

Here’s the script I’m trying to make work. When a certain object collides with the trigger, it is destroyed and respawned at the center of the screen with a certain rotation.

``````function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
GuiControl.Player2Score += 1;
Instantiate(other.gameObject, Vector3(0, 0, 0), transform.rotation);
}
``````

So are you saying I would change that to:

``````function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
GuiControl.Player2Score += 1;
Instantiate(other.gameObject, Vector3(0, 0, 0), transform.eulerAngles(0,0,0));
}
``````

And set the rotation to what I want (for example, 5 in y?)?

Because that doesn’t seem to work.

the code there isn’t optimal for position as well as rotation.

If you don’t want any non-default one use

Vector3.zero
Quaternion.identity

^^But I DO want to rotate it, that’s sort of the point. Say 13 in y, for example. If I just wanted the default then I wouldn’t need to do anything, since it automatically respawns with 0 rotation.

I want to set its rotate values directly (later I’m going to randomize it, but for now I just want to set it directly).

For random rotation you can use the function provided above by Eric

For real setting you will have to read a bit in the script reference of Quaternion

righty-0. I’ll try out his suggestion, then read up on Quaternion in the script reference.

EDIT: Well, there’s a problem. Random.rotation sets all 3 axis, whereas I need control of which axis is altered (specifically, only the y-axis should rotate).

To rotate around one axis, you could use Quaternion.AngleAxis with a random angle parameter:-

``````var randAngle = Random.value * 360.0;
transform.rotation = Quaternion.AngleAxis(randAngle, Vector3.up);
``````