# Simultaneously translating and rotating sprite in Unity

I have a 2D object (a PNG file I dragged onto the screen) which is a circle I would like to move left and right when the correct side of the screen is touched and have it rotate in the respective direction while doing so. Naturally, I thought using transform.Translate and transform.Rotate would be the proper way to do this, as such:

void Update() {

if (Input.touchCount > 0)
{
var touch = Input.GetTouch (0);
if (touch.position.x < Screen.width/2)
{
transform.Translate (Vector3.left * Time.deltaTime);
transform.Rotate (Vector3.forward * Time.deltaTime);
}
else if (touch.position.x > Screen.width/2)
{
transform.Translate (Vector3.right * Time.deltaTime);
transform.Rotate (Vector3.back * Time.deltaTime);
}
}
}


For whatever reason, rotating left and right didn't work so I tried forward and back which at least rotated it. The problem is that the circle moves in an arc, I'm assuming because as it rotates, the direction of "right" changes. Also, the circle gets squashed into an ellipse after a bit, I assume because I'm rotating on the wrong axis. I've literally just started using Unity yesterday; what is the most concise and best way to accomplish what I'd like?

Essentially your main problem is that you're not rotating correctly. Which is leading to other issues.

When rotating I find it's easier to use the version of Rotate that takes an axis to rotate around, and the number of degrees to rotate. So:

float degreesPerSecond = 45;
if (touch.position.x < Screen.width/2)
{
transform.Translate (Vector3.left * Time.deltaTime);
transform.Rotate (Vector3.up, degreesPerSecond * Time.deltaTime);
}
else if (touch.position.x > Screen.width/2)
{
transform.Translate (Vector3.right * Time.deltaTime);
transform.Rotate (Vector3.up, -degreesPerSecond * Time.deltaTime);
}


The reason it appears to get squished is because even though you're using a 2D object, it's being rotated in 3D. This makes it appear to be squished when it rotates away from the plane you're viewing it on. The version of Rotate you're using applies the rotation as direct euler angles:

Applies a rotation of eulerAngles.z degrees around the z axis, eulerAngles.x degrees around the x axis, and eulerAngles.y degrees around the y axis (in that order).

Also note that the Translate and Rotate methods both accept a Space parameter, this defaults to the local or Self coordinates. If you always want to rotate around the world axis you'll have to pass in the World variable when translating or rotating.

• I'm not so sure about those axis in the suggested edit. It should be rotating around the same axis, just in opposite directions. Forward vs Up just depends on how you've oriented your scene. – MichaelHouse Aug 7 '14 at 23:52