- Home /
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) !!!!!!
Looks like this kind of question requires some real test. Also it seems that no one could know exactly what it is. In the first place, you use *=
with 20*Time.delta
which is very small, the x
could be rounded down to 0
. So it's always 0
. Try using +=
ins$$anonymous$$d. Even with that the camera can go in wrong direction as you said in the last sentence. I doubt that may be related to using transform.rotation
while it should be transform.localRotation
. That's my suggestion, you should give it a try. I hope you'll comment back the result soon.
Answer by lordlycastle · Mar 01, 2015 at 06:58 PM
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
.
camera is a child of player game object ! i use RotateAround() before and it work not fine (camera go to another position) ! i think because of its a child of player .
Answer by sparkzbarca · Mar 01, 2015 at 07:11 PM
hmmm i'm just going to give you some code that works to limit rotation :P
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.
what is RotateAngle += Input.GetAxis("$$anonymous$$ouse Y") ? what it do ?
i have ui button (its for mobile) . what is I*nput.GetAxis ("$$anonymous$$ouse X") in CamTransform.RotateAround* ??
TELL $$anonymous$$E U CAN HELP $$anonymous$$E $$anonymous$$ORE :(( TELL $$anonymous$$E :((