Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

cam move up and down 80 degree ?

Discussion in 'Scripting' started by Quast, Feb 24, 2016.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I need from my camera to move up and down 80 degree only not 90. what can i do ?
     
  2. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    hey bud im not a heavy coder but i know with values on anything in code you normally use

    Clamp

     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ooo pitfalls ahoy... clamp acts funny with rotations since 0, 360, 720 are all the "same". You'll likely want to use clamp in conjunction with the "angle from forward" or some such.
     
    Custardcs likes this.
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    OK. what about ME o_O
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have a look at Mathf.Clamp in conjunction with Vector3.Angle... given that you've given nothing about your project or what you have implemented, where you expecting more than general pointers?
     
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I would go for using clamp with the x euler angle of the camera rotation.
     
  7. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I got one but there is error can find it !

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6. public float lookSensitivity = 5f;
    7. public float yRotation;
    8. public float xRotation;
    9. public float currentYRotation;
    10. public float currentXRotation;
    11. public float yRotationV;
    12. public float xRotationV;
    13. public float lookSmoothDamp = 0.1f;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
    23.         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
    24.  
    25.         xRotation = Mathf.Clamp(xRotation, -90, 90);
    26.         yRotation = Mathf.Clamp(yRotation, -80, 80);
    27.  
    28.         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
    29.         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
    30.  
    31.         transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    32.     }
    33. }
    34.  
     
  8. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    what type of cam are you looking for? rts? fps?
     
  9. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    This is my cam script for my RTS game..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. using Dream;
    5.  
    6. namespace Dream{
    7.  
    8. public class CameraControl : MonoBehaviour {
    9.     //Global Variables
    10.     public int CamSpeed = 1;
    11.     public int GUISize = 25;
    12.    
    13.     public float ZoomMaxY = 0;
    14.     public float ZoomMinY = 0;
    15.     public float ZoomSpeed = 0.05f;
    16.    
    17.     public float RotateAmount = 10;
    18.     public float RotateSpeed = 100;
    19.    
    20.     public GameObject camRotate = null;
    21.     public Vector3 CamAngle;
    22.     public float CamX;
    23.     public float CamY;
    24.    
    25.     //Main Method
    26.     void LateUpdate ()
    27.     {
    28.         CameraScreenHit ();
    29.         ZoomCamera ();
    30.         RotateCamera ();
    31.         KeyPan ();
    32.        
    33.        
    34.     }
    35.     //Method for Zooming in and out in the world
    36.    
    37.     private void KeyPan ()
    38.         {
    39.             if (Input.GetKey ("left")) {
    40.                 //transform.position = new Vector3(transform.position.x - CamSpeed,transform.position.y,transform.position.z);
    41.                 camRotate.transform.Translate( - CamSpeed,0, 0, Space.Self);
    42.             }
    43.             if (Input.GetKey ("right")) {
    44.                 //transform.position = new Vector3(transform.position.x + CamSpeed,transform.position.y,transform.position.z);
    45.                 camRotate.transform.Translate(CamSpeed,0, 0, Space.Self);
    46.             }
    47.             if (Input.GetKey ("up")) {
    48.                 //transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z+CamSpeed);
    49.                 camRotate.transform.Translate(0, 0, CamSpeed, Space.Self);
    50.             }
    51.             if (Input.GetKey ("down")) {
    52.                 //transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z-CamSpeed);
    53.                 camRotate.transform.Translate(0, 0, - CamSpeed, Space.Self);
    54.             }
    55.  
    56.         }
    57.    
    58.     private void ZoomCamera()
    59.     {
    60.         float moveY = Input.GetAxis ("Mouse ScrollWheel");
    61.        
    62.         transform.position += new Vector3 (0, moveY * -ZoomSpeed, 0);
    63.        
    64.         if (transform.position.y > ZoomMaxY)
    65.             transform.position = new Vector3 (transform.position.x, ZoomMaxY, transform.position.z);
    66.         if (transform.position.y < ZoomMinY)
    67.             transform.position = new Vector3 (transform.position.x, ZoomMinY, transform.position.z);
    68.             //transform.rotation = Quaternion. (transform.position.y-15, 0, 0);
    69.             //transform.eulerAngles = new Vector3 (transform.position.y-15, 0, 0); effect
    70.             //CamX = transform.position.y - 15;
    71.     }
    72.    
    73.     //Method for moving around the world
    74.     private void CameraScreenHit()
    75.     {
    76.         var recdown = new Rect (0, 0, Screen.width, GUISize);
    77.        
    78.         var recup = new Rect (0, Screen.height - GUISize, Screen.width, GUISize);
    79.        
    80.         var recleft = new Rect (0, 0, GUISize, Screen.height);
    81.        
    82.         var recright = new Rect (Screen.width - GUISize, 0, GUISize, Screen.height);
    83.        
    84.         if (recdown.Contains (Input.mousePosition))
    85.         {
    86.             camRotate.transform.Translate(0, 0, - CamSpeed, Space.Self);
    87.            
    88.         }
    89.        
    90.         if (recup.Contains (Input.mousePosition))
    91.         {
    92.             camRotate.transform.Translate(0, 0, CamSpeed, Space.Self);
    93.         }
    94.        
    95.         if (recleft.Contains (Input.mousePosition))
    96.         {
    97.             camRotate.transform.Translate( - CamSpeed,0, 0, Space.Self);
    98.         }
    99.        
    100.         if (recright.Contains (Input.mousePosition))
    101.         {
    102.             camRotate.transform.Translate(CamSpeed,0, 0, Space.Self);
    103.         }
    104.     }
    105.    
    106.     //Method for rotating around the center player
    107.     public void RotateCamera()
    108.     {
    109.         Vector3 origin = camRotate.transform.eulerAngles;
    110.         Vector3 destination = origin;
    111.        
    112.         if (Input.GetMouseButton (2))
    113.         {
    114.             destination.y += Input.GetAxis ("Mouse X") * RotateAmount;
    115.         }
    116.        
    117.         if (destination != origin)
    118.         {
    119.             camRotate.transform.eulerAngles = Vector3.MoveTowards (origin, destination, Time.deltaTime * RotateSpeed);
    120.         }
    121.        
    122.     }
    123.    
    124. }
    125. }