- Home /
Camera Rotation on Touch. Clamp rotation.
I'm not a programmer and have been searching a lot but I can't find out how to do this. Hope you can help me with this small problem. I have this script which is working great for me. Only thing I want to add to this is to clamp the rotation of the camera to for example 40 degrees max.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Touch Look")]
public class TouchLook : MonoBehaviour
{
public float sensitivityX = 5.0f;
public float sensitivityY = 5.0f;
public bool invertX = false;
public bool invertY = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
Vector2 delta = Input.touches[0].deltaPosition;
float rotationZ = delta.x * sensitivityX * Time.deltaTime;
rotationZ = invertX ? rotationZ : rotationZ * -1;
float rotationX = delta.y * sensitivityY * Time.deltaTime;
rotationX = invertY ? rotationX : rotationX * -1;
transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
}
}
}
}
Answer by Ermiq · Nov 22, 2019 at 01:00 PM
You could check the angle between some pivot transform forward vector and the forward vector that the camera will have when new rotation is applied. If the angle is more than 40 degrees, don't apply the rotation.
But you can't use camera transform as a pivot obviously, because its forward vector is changed by the rotation. So, for example, you'll need to use player character transform as a pivot, but the character should not be rotated by the camera.
By the way, in new Vector3(rotationX, rotationZ, 0)
the parameter rotationZ
should be called rotationY
, because vector parameters are x, y, z
.
// before you apply the rotation, check the angle first
// player.transform is a pivot transform that will represent the initial straight forward vector.
float angle = GetAngle(player.transform, transform, new Vector3(rotationX, rotationY, 0));
if (angle < 40f)
transform.localEulerAngles += new Vector3(rotationX, rotationY, 0);
private float GetAngle(Transform pivot, Transform camera, Vector3 rotationToBeApplied) {
// get the direction vector that the camera will have upon the rotation application
// to get that direction you'll need to get the resulting rotation as a quaternion. We'll take current camera rotation, rotate it (the rotation, not the camera) x degrees around X axis, and y degrees around Y axis.
Quaternion q = camera.rotation * Quaternion.AngleAxis(rotationToBeApplied.x, Vector3.right) * Quaternion.AngleAxis(rotationToBeApplied.y, Vector3.up);
// now we've got the rotation, convert it into a direction vector
Vector3 cameraResultingDirection = q * Vector3.forward;
// get the angle between the pivot and this new camera vector
float angle = Vector3.Angle(cameraResultingDirection, pivot.forward);
return angle;
}
Answer by lgarczyn · Nov 23, 2019 at 04:55 AM
I would advise against reading and storing your current orientation from a quaternion. The constant conversion between quaternion and Euler angle is often bad, even if it shouldn't cause problems here.
My advisor would be storing a rotationY and a rotationX float inside TouchLook, that store the current rotation.
Use inputs to increment them to their max values, and only then convert them to a quaternion using Quaternion.Eulers or two Quaternion.AngleAxis
Your answer
![](https://koobas.hobune.stream/wayback/20220612215144im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting the FPS Camera to a certain position 0 Answers
smooth camera rotation problem 1 Answer
Problems with Gyroscope VR Car Game 0 Answers
Clamp a Vector above minimum angle from another Vector? 0 Answers
View Matrix Camera rotation problem 0 Answers