I have a virtual joystick to rotate the player and this piece of code works nice and smooth for the rotation of the players view.

public void rotatePlayer(Vector3 newPosition){
  transform.Rotate(new Vector3(-newPosition.y,newPosition.x,0)*Time.deltaTime);

I pass a Vector3 with the position of the joystick in relation of his parrent's(the joystick border) anchor.That gives me the rotation direction.Which means that if I set the rotation of the camera direclty it will just flip to that point and not move progresively to that point.

Even though z is set to 0 in the vector3, it changes the z axis angle of the camera. How can I clamp it to 0 without using many resources?

I tried other methods but they just switch the camera's rotation directly when the joystick is touched like I already said.Flip and stop.

I guess I could just set the z axis rotation to 0 every time but that just seems like double work.

  • \$\begingroup\$ Does this only happen if the camera's rotation is already different from the default or does it always happen? \$\endgroup\$ – Bálint Aug 9 '16 at 14:45
  • \$\begingroup\$ Always.The x and y axis rotation affects the z rotation as much as I understand and see \$\endgroup\$ – Georgi Dzhondzhov Aug 9 '16 at 14:47

Use localEulerAngles instead

float rotationX = transform.localEulerAngles.x + amount to rotate x;
float rotationY = transform.localEulerAngles.y + amount to rotate y;
transform.localEulerAngles = new Vector3(rotationX, rotationY, 0);
  • \$\begingroup\$ When the joystick is moved and the PointerEvenetData changes it just jumps to that point, it doesn't move progresively to that point it just jumps to it.I need a slow rotation towards a point.Changing the eulerangules all the time even with a little bit serously drops the fps for some reason.I asume it could be my fault. \$\endgroup\$ – Georgi Dzhondzhov Aug 9 '16 at 16:25
  • \$\begingroup\$ Updated the example code. I don't think rotating your camera should lower your FPS \$\endgroup\$ – Peter Morris Aug 10 '16 at 7:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.