- Home /
Problem With 360 Rotation Degrees
hi
i have a player with a camera as child and 2 UI button that rotate the camera up and down . when "up button" pressed , RTU (boolean) variable set to TRUE and in update function it rotate the camera up and when "down button" pressed , RTD (boolean) variable set to TRUE and in update function it rotate the camera down . and when they release , RTU and RTD set to FALSE .
here is my code :
var MyCamera:gameobject; //Camera
var RTD:boolean=false; var RTD:boolean=false;
function Update()
{
if(RTU == true )
MyCamera.transform.Rotate(Time.deltaTime*20,0,0);
if(RTD == true )
MyCamera.transform.Rotate(-Time.deltaTime*20,0,0);
}
now the problem is here : i cant tell the function , rotate the camera just +45 And -45 degree . transform.Rotate is from 0 to 360 and it don't have -45 value . -45 is 315 in transform.Rotate .
<< here is a picture that make my question more clear : >>
like picture , before 0 come 360 and i want to rotate my camera between 315 and 45 degree (+45 to -45) . how can i do that ?
<< Excuse Me For My Bad English , If The Question Is Not Clear , Just Tell Me >>
Answer by juanma_teso · Mar 06, 2015 at 12:22 PM
Set a condition that prevents be under 315 and over 45: if(camera.rotation.eulerAngles.x >315){ rotate the camera; } if(camera.rotation.eulerAngles.x < 45){ rotate the camera; } This will work if transform floats to int (Mathf.Floor)
that's what im looking for :) Thank You Very $$anonymous$$uch :) when i reach 315 degree it will stop rotating (because its bigger then 315 and that's fine) but i also cant rotate it down because its not in range for doing rotation ! its my code :
if($$anonymous$$yCamera.transform.rotation.eulerAngles.x <= 315 && $$anonymous$$yCamera.transform.rotation.eulerAngles.x >= 45)
{
print("45 - 315");
}
else
{
if(RTU == true)
{
print("TU :: "+$$anonymous$$yCamera.transform.rotation.eulerAngles.x);
$$anonymous$$yCamera.transform.Rotate(-Time.deltaTime*20,0,0);
}
if(RTD == true)
{
print("TD :: "+$$anonymous$$yCamera.transform.rotation.eulerAngles.x);
$$anonymous$$yCamera.transform.Rotate(Time.deltaTime*20,0,0);
}
}
how can i solve it :-?
Answer by christophfranke123 · Mar 06, 2015 at 11:41 AM
You might want to use RotateTowards instea. This lets you rotate your camera toward a certain rotation without exceeding it and specify the maximum relative degree between from and to:
camera.transform.rotation = Quaternion.RotateTowards(camera.transform.rotation, targetRotation, degreesPerSecond*Time.deltaTime);
Your answer
Follow this Question
Related Questions
Limit transform.LookAt to 90 degrees based off of object's forward axis 1 Answer
How can I set a limit to my object rotation???(Here's the code) 2 Answers
accelerometer Rotation limiting 1 Answer
How do I limit my car's z rotation when a key is pressed? 0 Answers
Limiting the rotation of an object 1 Answer