Simple Problem With Rotation !!!

hi , i have a player game object that have a camera as child . and i have a camera controller script like this :

if(MyCamera.transform.rotation.x < 0.25)
{
	if(RTD == true )
	MyCamera.transform.rotation *= Quaternion.Euler(20*Time.deltaTime,0,0);
}
if(MyCamera.transform.rotation.x > -0.25)
{
	if(RTU == true )
	MyCamera.transform.rotation *= Quaternion.Euler(-20*Time.deltaTime,0,0);
}

MyCamera is the camera and RTD/RTU (boolean variables) is checking the rotation button is hold or not .

problem is i cant limit rotation to up and down between a range (if statement is useless) , because Quaternion.Euler reset the X rotation to 0 every time i hold the rotation button . i use this code before :

MyCamera.transform.Rotate(-Time.deltaTime*20,0,0);

it also not work and i don’t know why ! i also use this code :

if(MyCamera.transform.rotation.x > -0.25)
{
	if(RTU == true )
	MyCamera.transform.rotation.x -= 0.01;
}

it works fine but when i rotate the player ( camera is child of it ) to left/right , camera rotating in wrong way (not rotate in x but rotate in x and y or x and z) !!!

I think it’s because you are limiting the rotation between -0.25, and 0.25 degrees which is very small. An average Time.deltaTime is about 0.015 s; and 20 * 0.015 = 0.3, so it rotates just once and you cannot notice it.

Try using RotateAround() instead of just setting the rotation manually. Set the axis argument equal to the y-axis (or according) to your character.

What are RTU, and RTD variables, and when do they change. A few things, use else if{} if it’s two mutually exclusive statements. Also you can combine the nested if statements like _myCam.Transform.Position > -0.25 && RTU == true.

hmmm i’m just going to give you some code that works to limit rotation :stuck_out_tongue:

	if (Input.GetButton ("Rotate Camera")) {
		RotateAngle += Input.GetAxis("Mouse Y");
		RotateAngle = Mathf.Clamp(RotateAngle, MinX,MaxX);
		CamTransform.RotateAround (CamTransform.position, Vector3.up, Input.GetAxis ("Mouse X"));
		CamTransform.rotation = Quaternion.Euler( new Vector3(-RotateAngle ,CamTransform.rotation.eulerAngles.y,CamTransform.rotation.eulerAngles.z));
	}

define these variables

RotateAngle, should be a vector3

MinX, should be minimum angle, -60 works well

MaxX, should be max angle, 60 works well

if you need help post whats wrong.

i asked it differently in THIS link . and problem Solved :slight_smile: