Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  4. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

Camera for dungeon crawler

Discussion in 'Scripting' started by AticAtac, Sep 12, 2017.

  1. AticAtac

    AticAtac

    Joined:
    Feb 26, 2014
    Posts:
    7
    Hi,
    i need a camera which can be used to look around (with limits x and y) and can be controlled at the same time by another script (e.g. move steps in different directions or change rotation in 90° steps). I managed to import FreeLook-Camera from assets, but i couldn't get it work to change rotation (north, east, south, west) while "free looking".

    Any help welcome
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    2,040
    Not sure exactly what you want, but just by guessing I think you probably want to have at least two levels of camera control, applying each control to a child transform and going down to the actual camera:

    MainControl
    ChildControl
    ActualCamera

    The MainControl object would be driven to north south east west by your character's facing.

    The ChildControl object would normally have its .localRotation set to Quaternion.identity, unless you are looking slightly offset somewhere (perhaps up or down, or slightly askew), but not turning your character to a new compass direction.

    Usually with a rig like this, once you move the character (MainControl), your ChildControl would re-center itself to avoid confusion.
     
  3. AticAtac

    AticAtac

    Joined:
    Feb 26, 2014
    Posts:
    7
    You are right. The character moves with the keys (block move) in 4 directions (just like in Dungeon Master) and by pressing e.g. the left mouse button i want to be able to look around. I wanted to ask here for a "ready" solution / advices before i do it myself (i am new to Unity), because i don't want to "reinvent the wheels" and i would like to concentrate on other game elements (prottyping).
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    6,294
    Perhaps if you do a setup like @Kurt-Dekker said, you could have it so when the mouse button is down your adjust the camera. I'm sure you could find something close to it somewhere, but if that's all you want & need.. you might gain some experience by making it .. :)
    I have used the standard asset cam before and it is probably close to what you want, but even then you may require some modification to get it working, also..
    It's hard to find "that exact thing you want" sometimes.. because everyone is a little different ;) (unique/picky lol)
     
  5. AticAtac

    AticAtac

    Joined:
    Feb 26, 2014
    Posts:
    7
    I finally did it myself :)

    Here is the code:

    Code (csharp):
    1.  
    2. public class CameraControl : MonoBehaviour {
    3.  
    4.     public float sensX = 1f;
    5.     public float sensY = 1f;
    6.     public float minX = -360F;
    7.     public float maxX = 360F;
    8.     public float minY = -60F;
    9.     public float maxY = 60F;
    10.  
    11.     private Quaternion _originalRotation;
    12.     private float _rotX=0F;
    13.     private float _rotY=0F;
    14.     private bool _mouseLook = false;
    15.  
    16.     private float _direction = 0;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         _originalRotation = Camera.main.transform.localRotation;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         if (Input.GetKeyUp ("s")) {            
    26.             Camera.main.transform.position += Camera.main.transform.forward*-1;
    27.         }
    28.         if (Input.GetKeyUp ("w")) {            
    29.             Camera.main.transform.position += Camera.main.transform.forward;
    30.         }
    31.         if (Input.GetKeyUp ("d")) {            
    32.             Camera.main.transform.position += Camera.main.transform.right;
    33.         }
    34.         if (Input.GetKeyUp ("a")) {            
    35.             Camera.main.transform.position += Camera.main.transform.right*-1;
    36.         }
    37.  
    38.  
    39.         if (Input.GetKeyUp("e"))
    40.         {
    41.             _direction += 90f;
    42.             if (_direction > 360f)
    43.                 _direction -= 360f;
    44.             _originalRotation = Quaternion.Euler(0, _direction, 0);
    45.             Camera.main.transform.rotation = _originalRotation;
    46.         }
    47.         if (Input.GetKeyUp("q"))
    48.         {
    49.             _direction -= 90f;
    50.             if (_direction < -360f)
    51.                 _direction += 360f;
    52.             _originalRotation = Quaternion.Euler(0, _direction, 0);
    53.             Camera.main.transform.rotation = _originalRotation;
    54.         }
    55.  
    56.         if (Input.GetMouseButtonDown(1))
    57.         {
    58.             _mouseLook = true;
    59.             _rotX = _rotY = 0;
    60.         }
    61.         else if (Input.GetMouseButtonUp(1))
    62.         {
    63.             _mouseLook = false;
    64.             //Camera.main.transform.localRotation = _originalRotation;
    65.         }
    66.            
    67.         if (_mouseLook)
    68.             UpdateMouseLook();
    69.     }
    70.  
    71.     void UpdateMouseLook()
    72.     {
    73.         _rotX += Input.GetAxis("Mouse X") * sensX;
    74.         _rotY += Input.GetAxis("Mouse Y") * sensY;
    75.         _rotX = ClampAngle(_rotX, minX, maxX);
    76.         _rotY = ClampAngle(_rotY, minY, maxY);
    77.         Quaternion xQuaternion = Quaternion.AngleAxis(_rotX, Vector3.up);
    78.         Quaternion yQuaternion = Quaternion.AngleAxis(_rotY, -Vector3.right);
    79.         Camera.main.transform.localRotation = _originalRotation * xQuaternion * yQuaternion;
    80.     }
    81.  
    82.     public static float ClampAngle(float angle, float min, float max)
    83.     {
    84.         if (angle < -360F)
    85.             angle += 360F;
    86.         if (angle > 360F)
    87.             angle -= 360F;
    88.         return Mathf.Clamp(angle, min, max);
    89.     }
    90. }
    91.  
    92.  
     
    Kurt-Dekker likes this.
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    6,294
    Cool, glad ya got it working :)