Search Unity

I am making a gravity controlled camera, but i need it to only turn only 90 degrees on a condition.

Discussion in 'Scripting' started by penthemaker, Jul 27, 2020.

  1. penthemaker

    penthemaker

    Joined:
    Jul 27, 2020
    Posts:
    2
    I`m a literally a NEW game maker and I wanted to make a ("drone") that you could drive walls and see where your player is. I would like to know how to rotate another object to the same side as my mouse goes when controlling the camera. BTW i need the object to rotate only 90 degrees. (the object is just the drone, I actually don't know how to control it too, but if it rotates only 90 degrees it will not be hard). Plus i wanted to ask: does anyone know how to limit the x axis rotation(adopt the y axis limitation to work on x too.)

    I don't know am i asking to much, but i am literally stuck.

    THE CODE OF cameraRotatingScript:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraMovement : MonoBehaviour
    7. {
    8.     private void Start()
    9.     {
    10.         //gravity = Physics.gravity;
    11.         Cursor.lockState = CursorLockMode.Locked;
    12.     }
    13.  
    14.     public enum RotationAxis
    15.     {
    16.         MouseX = 1,
    17.         MouseY = 2
    18.     }
    19.  
    20.     public RotationAxis axes = RotationAxis.MouseX;
    21.  
    22.     public float minimumVert = -45.0f;
    23.     public float maximumVert = 45.0f;
    24.  
    25.     public float sensHorizontal = 10.0f;
    26.     public float sensVertical = 10.0f;
    27.  
    28.     public float _rotationX = 0;
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (axes == RotationAxis.MouseX)
    34.         {
    35.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensHorizontal, 0);
    36.         }
    37.         else if (axes == RotationAxis.MouseY)
    38.         {
    39.             _rotationX -= Input.GetAxis("Mouse Y") * sensVertical;
    40.             _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert); //Clamps the vertical angle within the min and max limits (45 degrees)
    41.  
    42.             float rotationY = transform.localEulerAngles.y;
    43.  
    44.             transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
    45.         }
    46.  
    47.  
    48.         if (Input.GetKeyDown("escape"))
    49.         {
    50.             Cursor.lockState = CursorLockMode.None;
    51.         }
    52.      
    53.     }
    54. }
    55.  
    I hope someone helps cause if not idk what I will do ):
     
    Last edited: Jul 28, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Joe-Censored likes this.
  3. penthemaker

    penthemaker

    Joined:
    Jul 27, 2020
    Posts:
    2
    I am sorry, that I even asked, because after sleeping I think i have a solution. So now i feel shame for asking and wasting people time, thanks Kurt-Dekker for your patience and helping me understand mistakes that i made :)
     
    Kurt-Dekker likes this.