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

Question How to orbit free look camera from a script?

Discussion in 'Cinemachine' started by numberkruncher, Jul 20, 2021.

  1. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    For mouse and keyboard input I would like to create a script where when the user presses Q or E the orbit of the free look camera is rotated by ether -90 degrees or +90 degrees on the horizontal axis.

    For gamepad input the default input handling works perfectly.

    What is the best way to achieve this?
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I have come up with an approach which seems to work well. I have created a child game object on my player game object which rotates in 90 degree increments and maintains its rotation relative to world space. I have added a virtual camera which follows and aims at this child game object.

    Here is the script that I created if anyone is interested:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. namespace Project
    6. {
    7.     public sealed class BirdsEyeCameraRotator : MonoBehaviour
    8.     {
    9.         [SerializeField]
    10.         private InputActionReference rotateLeftAction = null;
    11.         [SerializeField]
    12.         private InputActionReference rotateRightAction = null;
    13.  
    14.         [Space]
    15.         [SerializeField]
    16.         private float angleIncrements = 90f;
    17.         [SerializeField]
    18.         private float speed = 90f;
    19.  
    20.  
    21.         private Vector3 currentAngles;
    22.         private Vector3 targetAngles;
    23.  
    24.  
    25.         private void OnEnable()
    26.         {
    27.             this.currentAngles = this.transform.eulerAngles;
    28.  
    29.             this.rotateLeftAction.action.performed += this.RotateLeftAction_Performed;
    30.             this.rotateRightAction.action.performed += this.RotateRightAction_Performed;
    31.         }
    32.  
    33.         private void OnDisable()
    34.         {
    35.             this.rotateLeftAction.action.performed -= this.RotateLeftAction_Performed;
    36.             this.rotateRightAction.action.performed -= this.RotateRightAction_Performed;
    37.         }
    38.  
    39.         private void Update()
    40.         {
    41.             this.currentAngles = Vector3.MoveTowards(this.currentAngles, this.targetAngles, this.speed * Time.deltaTime);
    42.             this.transform.eulerAngles = this.currentAngles;
    43.         }
    44.  
    45.  
    46.         private void RotateLeftAction_Performed(InputAction.CallbackContext context)
    47.         {
    48.             this.targetAngles.y -= this.angleIncrements;
    49.         }
    50.  
    51.         private void RotateRightAction_Performed(InputAction.CallbackContext context)
    52.         {
    53.             this.targetAngles.y += this.angleIncrements;
    54.         }
    55.     }
    56. }
    57.  
     
    Gregoryl likes this.