Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Implement a limit in Camera Movement

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

  1. getmyisland_dev

    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.

    If you need more information please ask me.

    Thanks in advance,
    Max
     
    Last edited: Apr 27, 2021
  2. Kurt-Dekker

    Kurt-Dekker

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

    The reason is gimbal lock.

    Instead track your own float variable that is the heading, adjust and clamp that variable, then "drive" the camera's rotation each frame.
     
    Joe-Censored likes this.
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    First thanks for your fast answer, but I don't really understood everythings you said in your answer.

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

    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. {
    3.    heading += AdjustmentWhateverYouWant;
    4. }
    3. clamp it

    Code (csharp):
    1. heading = Mathf.Clamp( heading, minimum, maximum);
    4. drive the rotation to the camera transform:

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

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Thank you for your reply.

    I tested this out, but I still don't know how I can apply this into my edgescrolling script. (line 22-34)

    It says that I can't apply that when I try it.

    To be clear I already have a thing that defines the heading (line 12 and 20) and in now want to apply this.

    Thank you for trying to help me.