# Question Implement a limit in Camera Movement

Discussion in 'Scripting' started by getmyisland_dev, Apr 27, 2021.

1. ### getmyisland_dev

Joined:
Dec 22, 2020
Posts:
100
Hello everyone,

I need help with a camera script I'm currently working with.

Here is the script:
Code (CSharp):
1. using System.Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4.
5. public class SecurityCameraControl : MonoBehaviour
6. {
7.     //public float speedH = 20.0f;            Script for a First Person Camera
8.
9.     public float rightSpeed = 20.0f;
10.     public float leftSpeed = -20.0f;
11.
12.     //public float rotationY = 0.0f;        Script for a First Person Camera
13.
14.     void Update()
15.     {
16.         //rotationY += speedH * Input.GetAxis("Mouse X");            Script for a First Person Camera
17.
18.         //transform.eulerAngles = new Vector3(0, rotationY, 0);        Script for a First Person Camera
19.
20.         //rotationY = Mathf.Clamp(rotationY, 50f, 130f);            Script for a First Person Camera
21.
22.         //EdgeScrolling
23.         float edgeSize = 40f;
24.         if (Input.mousePosition.x > Screen.width - edgeSize)
25.         {
26.             //EdgeRight
27.             transform.RotateAround(this.transform.position, Vector3.up, rightSpeed * Time.deltaTime);
28.         }
29.
30.         if (Input.mousePosition.x < edgeSize)
31.         {
32.             //EdgeRight
33.             transform.RotateAround(this.transform.position, Vector3.up, leftSpeed * Time.deltaTime);
34.         }
35.     }
36. }
37.
This is the script and I want to implement a limit to the camera so that I can't rotate 360°.
I already tried to do this in line 20 and it worked, but now I want to transfer that into my real script.

Max

Last edited: Apr 27, 2021
2. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
39,429
Generally never read / write to / from eulerAngles.

The reason is gimbal lock.

Joe-Censored likes this.
3. ### getmyisland_dev

Joined:
Dec 22, 2020
Posts:
100

I know what "clamp" means, but how can I track my own float variable, adjust and "drive" that?

4. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
39,429
1. declare a variable
``float heading;``

2. adjust it when conditions warrant:

Code (csharp):
1. if (conditionToTurn)
2. {
4. }
3. clamp it

Code (csharp):
4. drive the rotation to the camera transform:

Code (csharp):
1. cameraTransform.localRotation = Quaterion.Euler( 0, heading, 0);

getmyisland_dev likes this.

Joined:
Dec 22, 2020
Posts:
100