Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A simple Free-Look to the Cinemachine Virtual Camera

Discussion in 'Cinemachine' started by Moaty5, Dec 2, 2018.

  1. Moaty5

    Moaty5

    Joined:
    Dec 18, 2014
    Posts:
    6
    CM is a great camera framework !

    you might need though a Virtual camera to look around.
    in case for example of an FPS game, or a shoulder-aim view of a third person game.
    the Free-Look CM preset is great, but its a bit too feature rich that it doesnt fit simple/certain situations.
    it doesnt for example support offsetted follow behaviour (for example: the player would be a little offsetted on the screen which is problematic for shoulder-aim view)

    for that i created a very simple script that allows a virtual camera to look around, with CM still doing its magic normally
    i thought to share it, that someone could be looking for something like this.

    please dont hesitate to leave feedback, who knows how this could inspire the folks working on CM at unity or other fellow developers

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// A simple script that allows the Cinemachine virtual camera to look around. this script must be added to a CM virtual camera
    6. /// </summary>
    7. [RequireComponent(typeof(CinemachineVirtualCameraBase))]
    8. public class CameraLookInput : MonoBehaviour
    9. {
    10.     public float Speed = 5f;
    11.     public float PitchMax = 70f;
    12.     public float PitchMin = 45f;
    13.  
    14.     private CinemachineVirtualCamera cam;
    15.     private Transform lookAt;
    16.  
    17.     private float _currentX, _currentY;
    18.  
    19.     private void Start()
    20.     {
    21.         cam = GetComponent<CinemachineVirtualCamera>();
    22.  
    23.         lookAt = new GameObject("LookAtTarget").transform;
    24.         lookAt.position = transform.position;
    25.         lookAt.position += transform.forward * 10f;
    26.  
    27.         cam.LookAt = lookAt;
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         float x = Input.GetAxis("Mouse X");
    33.         float y = Input.GetAxis("Mouse Y");
    34.  
    35.         _currentX = Mathf.Clamp(_currentX + y * Speed, PitchMin, PitchMax);
    36.         _currentY += x * Speed;
    37.  
    38.         Vector3 dir = Vector3.forward * 10f;
    39.  
    40.         Vector3 input = new Vector3(-_currentX, _currentY);
    41.         Quaternion r = Quaternion.Euler(input);
    42.  
    43.         Vector3 freeLook = r * dir;
    44.         Vector3 pos = transform.position + freeLook;
    45.         lookAt.position = pos;
    46.  
    47.         Debug.DrawLine(lookAt.position, transform.position, Color.green);
    48.     }
    49. }
     
    Last edited: Dec 3, 2018
    Gregoryl likes this.