- Home /
Limiting the rotation of x-axis
Hello to all, I.m here to ask something about limiting the rotation of the child camera from x axis. i tried adding the clamp inside the Rotate() but it wont work. I tried the google search but still i cant figure out why it wont work. I am replicating the resident evil 5's camera control. I have a character that needed to limit the rotation of child camera's x from 0 to 30 only. How can i do that? Please help me. I'll show my code:
void Start()
{
Screen.lockCursor = true;
////Get the name of Player and Main Camera tag
cameraGO = GameObject.FindGameObjectWithTag("MainCamera");
cameraTargetGO = GameObject.FindGameObjectWithTag("Camera Target");
localPlayerGO = GameObject.FindGameObjectWithTag("Local Player");
}
void LateUpdate()
{
xRotation = Input.GetAxis("Mouse X") * Sensitivity;
yRotation = -Input.GetAxis("Mouse Y") * Sensitivity;
//Rotate the character along y-axis
localPlayerGO.transform.Rotate(0, xRotation, 0);
//Rotate the Camera along x-axis
cameraTargetGO.transform.Rotate(yRotation, 0, 0);
}
Answer by whydoidoit · Oct 10, 2012 at 04:27 PM
Try this:
cameraTargetGO.transform.Rotate(yRotation, 0, 0);
var current = cameraTargetGO.transform.rotation.eulerAngles;
cameraTargetGO.transform.rotation = Quaternion.Euler(Mathf.Clamp(current.x, 0, 30), current.y, current.z);
when the camera riches 30 value of clamp its ok but when it rich the 0 value it reset to 30. i dont know why
It's because < 0 is actually an angle that goes around the 360 degree mark. Hang on let me fix that
cameraTargetGO.transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(current.x > 180 ? current.x - 360 : current.x, 0, 30), current.y, current.z);
what do you call about the code with question mark? it doesnt work but thank you for trying to help me.
That's called the ternary operator (http://en.wikipedia.org/wiki/%3F:). It's really not working for you? You can always replace it with conditional code, like this:
cameraTargetGO.transform.Rotate(yRotation, 0, 0);
var current = cameraTargetGO.transform.rotation.eulerAngles;
float xAngle = current.x;
if (xAngle > 180)
{
xAngle -= 180;
}
cameraTargetGO.transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(xAngle, 0, 30), current.y, current.z);
$$anonymous$$ike's answer is a lot prettier, though. What exactly isn't working?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Limiting player movement (Partially done) 1 Answer
Potential 4.3.1f1 bug Instantiate c# 2 Answers
Drawing flat arrows 1 Answer