# Problem rotating object around particular axis

I am trying to write a script which will rotate my ‘grabber’ object around its y-axis by 180 degrees when it collides with another object. The other objects are smaller objects moving along a conveyor belt. The grabber should grab onto an item, rotate around its y axis 180 degrees, and release the object.

My script successfully rotates by 180 degrees on the y axis every time it collides with a conveyor object, but at the same time changes the x and z axis rotations, which I do not want. The resulting rotation is this -

As you can see, the grabber is no longer tilted. How would I get the object to rotate ONLY on its y axis? So that no matter how it is oriented, it would always rotate on its local y axis by 180 degrees when it collides?

The most relevant part of the code below is -

`````` //Specify the desired rotation
deltaRot = Quaternion.AngleAxis(transform.rotation.eulerAngles.y + 180f, transform.up);
``````

My script-

``````using UnityEngine;
using System.Collections;

public class MagnetScript : MonoBehaviour {

//The grab positions
private Transform[] grabPositions = new Transform[2];
private Transform grabPosition;

//The obj to grab and it's rb
private GameObject grabObj;
private Rigidbody rb;

//Our Finite State machine
private enum State {idle, grabbing, rotating, releasing};
private State state = State.idle;

//Our grabber turn speed
public float turnSpeed;

//Orignal grabber rotation and desired rotation
private Quaternion originalRotation, desiredRotation, deltaRot;

float buffer = 0.01f;

void Start()
{
//Define the two grab positions
grabPositions[0] = transform.Find("GrabPosition1").transform;
grabPositions[1] = transform.Find("GrabPosition2").transform;

//Define the layer mask (conveyor grabObjects)
}
void FixedUpdate()
{
if (state == State.grabbing) {

if (Vector3.Distance (grabObj.transform.position, grabPositions [0].position) > Vector3.Distance (grabObj.transform.position, grabPositions [1].position)) {
grabPosition = grabPositions [1];
} else
grabPosition = grabPositions [0];

//Hold the grabObject in the grab position
rb.MovePosition (grabPosition.position);

//Specify the desired rotation
deltaRot = Quaternion.AngleAxis(transform.rotation.eulerAngles.y + 180f, transform.up);

//Set State to rotating
state = State.rotating;
}

if(state == State.rotating)
{

//Hold the grabObject in the grab position
rb.MovePosition (grabPosition.position);

//Slerp the rotation
transform.rotation = Quaternion.Slerp( transform.rotation, deltaRot , Time.deltaTime * turnSpeed);

//Calculate the relative position to the og position
Quaternion relative = Quaternion.Inverse(deltaRot) * transform.rotation;

//if the relativePos is less than the threshold, then release the grabObject
if ( relative.eulerAngles.y < buffer ) {
state = State.releasing;
}
}

//Release the grabObject, turn gravity back on
if (state == State.releasing){
rb.useGravity = true;
rb.isKinematic = false;
state = State.idle;
grabObj = null;
grabPosition = null;
}
}

void OnTriggerEnter(Collider col)
{
//Grab grabObject if we are not already grabbing something
if (state == State.idle) {

//Only grab the conveyor grabObjects

state = State.grabbing;

//Get a reference to the grabObject being grabbed
grabObj = col.gameObject;

//Get grabObjects rigidbody and turn off use gravity while we grab
rb = col.gameObject.GetComponent<Rigidbody> ();
rb.useGravity = false;
rb.isKinematic = true;
}
}
}

}
``````

Great, thanks , got it working using

``````//Calculate the relative position to the og position
Quaternion relative = Quaternion.Inverse(originalRotation) * transform.rotation;

//if the relativePos is less than the threshold, then release the grabObject

transform.Rotate(0, Time.deltaTime * turnSpeed, 0, Space.Self);

if (relative.eulerAngles.y >= 180f)
{
state = State.releasing;
}`
``````