- Home /
Limit camera vertical rotation with keyboard
Hello. I want to rotate the camera around the interacted object with keys. My horizontal rotate works perfectly. But I don't know how to handle vertical rotation. I got this code from this question. I also want to clamp this vertical rotation just like in horizontal. I already tried to paste the code from horizontal to vertical and just change the currentVector and axis on RotateAround but this was working only to target objects in specified positions on scene.
private void RotateHorizontal()
{
_horizontalRotateDegrees = 0;
var value = horizontalRotate.action.ReadValue<float>();
if(horizontalRotate.action.ReadValue<float>() < 0)
{
_horizontalRotateDegrees -= rotateSensitivity.Value * value * Time.deltaTime;
}
if(horizontalRotate.action.ReadValue<float>() > 0)
{
_horizontalRotateDegrees -= rotateSensitivity.Value * value * Time.deltaTime;
}
var currentVector = playerCamera.transform.position - _targetTransformPos;
currentVector.y = 0;
var angleBetween = Vector3.Angle(_initialVector, currentVector) *
(Vector3.Cross(_initialVector, currentVector).y > 0 ? 1 : -1);
var newAngle = Mathf.Clamp(angleBetween + _horizontalRotateDegrees, -angleHorizontal, angleHorizontal);
_horizontalRotateDegrees = newAngle - angleBetween;
playerCamera.transform.RotateAround(_targetTransformPos, Vector3.up, _horizontalRotateDegrees);
}
Answer by Ermiq · Feb 26, 2021 at 10:41 AM
For camera rotation that rotates around player or some other object I prefer to use the following setup for camera that consists of empty GameObjects with the following hierarchy:
1. CamRotationPivotH
- a root game object, and it considered to rotate around it's Y axis only (rotates in World space).
2. CamRotationPivotV
- a child of the CamRotationPivotH
, it rotates around its X axis only (rotates in Local space).
3. Camera
component is attached to a game object that is a child of the CamRotationPivotV
. It's local rotation could be rotated anyhow depending on different effects and states of the player, like recoil, damage hit effect, speed burst shake effect, and it won't affect the rotation from player inputs.
public bool clampVerticalRotation;
public float MinimumX; //min angle for vertical rotation
public float MaximumX; //max angle for vertical rotation
Quaternion TargetRotationH;
Quaternion TargetRotationV;
public void RotateCamera(float horizontal, float vertical)
{
// Get current rotations
TargetRotationV = CamRotationPivotV.localRotation;
TargetRotationH = CamRotationPivotH.rotation;
// Add desired horizontal and vertical angles to the current rotations
TargetRotationV *= Quaternion.Euler(-vertical * Time.deltaTime, 0f, 0f);
TargetRotationH *= Quaternion.Euler(0f, horizontal * Time.deltaTime, 0f);
// Clamp vertical
if (clampVerticalRotation)
TargetRotationV = ClampRotationAroundXAxis(TargetRotationV);
// Apply rotations to the camera objects
CamRotationPivotV.localRotation = TargetRotationV;
CamRotationPivotH.rotation = TargetRotationH;
}
// Here is some Quaternion magic that I copy/pasted from somewhere:
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
angleX = Mathf.Clamp(angleX, MinimumX, MaximumX);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
return q;
}
To make it rotate around your object, set the CameraRotationPivotH.position
to the object's position, and set the Camera.transform.localPosition.z
to something like -5, for example. And send desired rotation angles to the RotateCamera()
method.
Your answer
Follow this Question
Related Questions
Making object Clamp to Look Rotation of Camera 1 Answer
Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers
Make camera move and look at an object over time 0 Answers
How to make the camera follow the player while still being able to be rotated? 1 Answer
How to clamp rotation in degrees in relation to another GameObject 1 Answer