# Rotation around Y

Hello there!
I’m having some problems rotating an object only in it’s Y.
if you use the codes

``````var i : int = 0;
transform.rotation = Quaternion.AngleAxis(i, Vector3.up);
i++;
``````

or

``````	var tiltAroundY = Input.GetAxis("Horizontal") + transform.eulerAngles.y;
// Create a rotation
transform.rotation = Quaternion.Euler (transform.eulerAngles.x, tiltAroundY, transform.eulerAngles.z);
``````

The first option keeps the object without any rotation exempt around the Y, so I can’t tilt the object, for example.
And in the second option the object can be rotated in any axis, and get tilted, but if you rotate the object on its Y it rotates in a kind of word coordinate, and not as I rotate an object on the unity3D viewer, on the real Y of the object.

I hope someone understand my problem.
Thanks!

Well, answering my own question… The solution was to take the objects Y direction and rotate around with the RotateAround function. Like this:

``````function Update () {
var rotationSpeed : float = 40;
var rotateAroundYInput : float = Input.GetAxis("Horizontal");
var obejctUp : Vector3 = transform.TransformDirection (Vector3.up);
transform.RotateAround (transform.position, obejctUp, rotateAroundYInput * rotationSpeed * Time.deltaTime);
}
``````

I hope that this become useful for someone else than me.

Yes it became useful for me!!

Good you found a working solution. Note though that you can also just use transform.Rotate(…):

Be careful though! There are several overloaded versions of that method, so be sure to supply the right parameters.

I would choose this one:

I just tried using transform.Rotate but it’s not doing anything. Here is the code:

``````function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 5;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 7;
transform.Translate(0, 0, z);
transform.Rotate(Vector3.right, x);
}
``````

Movement along the z axis works fine. And if I do this:

``````transform.Translate(x, 0, z);
``````

it strafes as you would expect. Does anyone know why it’s not rotating?

Thanks!

With a copy/paste of you code, it works for me. Any code in any attached scripts that are setting rotations?

Hmm, interesting that my code works for you. I’ll give you more details, hopefully that will help. Keep in mind I’m a noob to both Unity and 3D programming. My scene has a simple terrain, camera and a cube. The cube has a rigidbody, a mesh renderer, the script I pasted above and a Box Collider. I tried adding a Character Controller but then it would either slide off the terrain or display other odd behavior. My camera is using a “smooth follow” script, I don’t remember which example I snagged that from.

Does any of that extra detail help?

Thanks!!

Ah, well, your rigidbody is prolly affecting it. Is your rigidbody set to isKinematic?

My rigidbody was not set to isKinematic, I went ahead and tried that both ways with no improvement. I also deleted the rigidbody to see if that would help but it made no difference.

You said you are using the smooth follow script? The script keeps the camera behind its target. I wonder if the object is rotating but since it is always in the same relation to the camera, it appears not to be rotating. At the very least, you should check if the rotation remains the same throughout by check its value in the inspector.

Thank you!!! I deleted that script and a couple other things on the camera and it works now. Phew.