Hello, I’m trying to rotate my camera with a target object by the mouse. but there is some problem with camera’s Y localRotation. when I try to rotate camera to X axis over 90 or under -90, camera’s localRotation Y changes to 180 or -180 forcely. then filping camera’s view. how do I solve this problem?
here is my rotation code.
PS. English is not my first language. sorry for broken english
void SetCamVector()
{
_rotateProp.Distance = Vector3.Distance(Target.transform.position, Camera.main.transform.position);
_rotateProp.Forward = (Target.transform.position - Camera.main.transform.position).normalized;
_rotateProp.Right = Vector3.Cross(_rotateProp.Forward, Vector3.up).normalized;
_rotateProp.Up = Vector3.Cross(_rotateProp.Forward, _rotateProp.Right).normalized;
}
void CameraRotate()
{
if (_rotateProp.RecentPos != Input.mousePosition)
{
SetCamVector();
Vector2 touchVector = Input.mousePosition - _rotateProp.RecentPos;
const float sensivity = 20.0f;
Quaternion quat = Quaternion.identity;
if (touchVector.x != 0)
{
float angle = touchVector.x * sensivity * Time.deltaTime;
quat *= Quaternion.Euler(0, angle, 0);
}
if (touchVector.y != 0)
{
float angle = touchVector.y * sensivity * Time.deltaTime;
quat *= Quaternion.Euler(angle, 0, 0);
}
Camera.main.transform.rotation = quat;
Camera.main.transform.position = Target.position;
Camera.main.transform.position -= quat * _rotateProp.Forward * _rotateProp.Distance;
Camera.main.transform.LookAt(Target.transform.position);
_rotateProp.RecentPos = Input.mousePosition;
}
}