- Home /
Clamping the camera X axis when not using the mouse axis? C#
I have a project which involves using a 'deadzone'/'free aiming' I'm a novice with C# and finally from going over forums found a way of doing it, it might not be the best way but i was happy it worked. The only problem is that i have been really struggling to clamp the rotation of the camera. I have given it many attempts and got quite close when using euler angles but had the problem of only on side of the clamp working and the other making the value jump to 0. I understand roughly what a quaternion is but have been struggling to make it work in code especially when your not supposed to change the value of just one of the x,y,z,w. Here is the code i have been using
public float noTurn = 0.1f; // Extent of the no-turn zone as a fraction of Screen.height;
public float factor = 150.0f;
private Vector3 center;
public bool YActive;
public bool XActive;
public float maxX;
public float minX;
/*private Vector3 maximumXRotation;
private Vector3 minimumXRotation;
private Vector3 XRotation;*/
// Use this for initialization
void Start ()
{
center = new Vector3(Screen.width / 2, Screen.height / 2, 0);
/*maximumXRotation = new Vector3 (maxX, 0f, 0f);
minimumXRotation = new Vector3 (minX, 0f, 0f);
XRotation = new Vector3 (transform.localEulerAngles.x, 0f, 0f);*/
}
void Update ()
{
Vector3 delta = (Input.mousePosition - center) / Screen.height;
Debug.Log(delta);
if (delta.y > noTurn && YActive == true)
transform.Rotate(-(delta.y - noTurn) * Time.deltaTime * factor, 0f, 0f);
if (delta.y < -noTurn && YActive == true)
transform.Rotate(-(delta.y + noTurn) * Time.deltaTime * factor, 0f, 0f);
if (delta.x > noTurn && XActive == true)
transform.Rotate(0f, (delta.x - noTurn) * Time.deltaTime * factor, 0f);
if (delta.x < -noTurn && XActive == true)
transform.Rotate(0f, (delta.x + noTurn) * Time.deltaTime * factor, 0f);
/*if (YActive == true)
Mathf.Clamp (transform.localEulerAngles, minimumXRotation, maximumXRotation);*/
/*if (YActive == true){
LateUpdate()}*/
}
/*void LateUpdate()
{
transform.localEulerAngles = new Vector3( Mathf.Clamp(transform.rotation.eulerAngles.x, minX, maxX), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z );
}*/
There are a few attempts in there which are all withing the different / /. Was hoping someone could point me in the right direction or maybe a better solution to the free aiming which might make it easier to solve the clamping problem.
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170055im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Clamping the camera X axis when not using the mouse axis? C# 0 Answers
Multiple Cars not working 1 Answer