Getting rotation relative to the player and not the world

Hi guys, so this question may be a little hard to explain. I’m trying to rotate my player and camera with a finger swipe. So a left/right swipe will rotate about the y axis, and an up/down swipe will make the player look up or down. However I’ve noticed that when I’m facing a certain direction, swiping up or down will change either the x or z axes. For example, facing in one direction, swiping up might rotate the view such that the landscape (which should be horizontal) now slants, whereas if I turn my character 90 degrees, the same swipe would make it look up as it should. So I can’t use transform.Rotate(blah,0,0), because that will only work when I’m facing one particular direction, and same if i use (0,0,blah). Here’s my code:

if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
		var touchDeltaPosition : Vector2 = Input.GetTouch(0).deltaPosition;
		multiplier = 0.2;
		transform.Rotate(touchDeltaPosition.y * multiplier, touchDeltaPosition.x * multiplier, 0);
}

Any ideas how to fix this? Cheers.

Try to put an space reference parameter in Rotate() function, it should be like:
transform.Rotate(touchDeltaPosition.y * multiplier, touchDeltaPosition.x * multiplier, 0, Space.Self);
}

I’d strongly advice to look up quaternions in unitys script reference.

Ok, problem solved. For anyone else wondering how to fix this problem, zero the z rotation OUTSIDE of the if statement controlling the x/y rotation. See code below:

if(transform.localEulerAngles.z > 0 || transform.localEulerAngles.z < 0) {
		transform.localEulerAngles.z = 0;
	}

Also it wouldn’t let me write if(blah !== 0), hence the mess of blah || blah.

Thanks for the responses though guys, still helpful.