# What does SetPositionAndRotation method means?

Hi guys!

In my project, I have a virtual hand in which I move in horizontal and vertical of the screen. Initially, all fingers of my hand’s model are pointed vertically. So I must rotate it to have a horizontal hand.

Then with a joystick I want to move my hand in vertical and horizontal way:

``````float handHoriz = Input.GetAxis("HandHorizontal") * Time.fixedDeltaTime * m_handhorizspeed;
float handVertical = Input.GetAxis("HandVertical") * Time.fixedDeltaTime * m_handverticalspeed;
transform.Rotate(0, 0, 0);
transform.Translate(handHoriz, -handVertical, 0);
transform.Rotate(0, -90, -70);
``````

The code above behaves very strangly, my hand is moving but I can really see each rotation step which I don’t want.

Then I try another code:

``````float handHoriz = Input.GetAxis("HandHorizontal") * Time.fixedDeltaTime * m_handhorizspeed;
float handVertical = Input.GetAxis("HandVertical") * Time.fixedDeltaTime * m_handverticalspeed;
transform.SetPositionAndRotation(transform.position, Quaternion.Euler(0, 0, 0));
Vector3 pos = transform.position;
Vector3 trans = new Vector3(handHoriz, -handVertical, 0);
pos += trans;
transform.SetPositionAndRotation(pos, Quaternion.Euler(0, -90f, -60f));
``````

The last code works perfectly, so my question is why using SetPositionAndRotation differs from my first code?

1 Like

In the first code, .Rotate(0,0,0) does not reset the rotation; it does nothing (i.e, rotates by 0 degrees, relative to current).

If you want to zero rotation, use something like ‘transform.rotation = Quaternion.Identity’.

Your second code shouldn’t need the first SetPositionAndRotation call. (it writes the position it’s already at, and the rotation is overridden by the second call)

``````transform.SetPositionAndRotation(pos, rot)
``````

is basically a shorter/more performant way of writing

``````transform.position = pos;
transform.rotation = rot;
``````
2 Likes