Search Unity

Question Setting up fly camera movement + cursor point&drag

Discussion in 'Input System' started by zntxrr, May 20, 2022.

  1. zntxrr

    zntxrr

    Joined:
    Jan 18, 2022
    Posts:
    4
    Hi,

    I am very much a noob. Sorry for asking super basic stuff but my brain has frozen.

    I need to a) move around the camera in free-fly mode with WASD and b) be able to point, click and drag with the mouse.

    The cursor movement should not affect the camera direction so I have to replace the "look" panning with keys.

    I have been able to sort of solve this with two scripts that I retained from tutorials online but now I want to use the new input system to be able to, once it is working, use other types of controllers in different combinations. Gamepad, joysticks etc.


    Code (CSharp):
    1. //===========================================================================//
    2. //                       FreeFlyCamera (Version 1.2)                         //
    3. //                        (c) 2019 Sergey Stafeyev                           //
    4. //===========================================================================//
    5.  
    6. using UnityEngine;
    7.  
    8. [RequireComponent(typeof(Camera))]
    9. public class FreeFlyCamera : MonoBehaviour
    10. {
    11.     #region UI
    12.  
    13.     [Space]
    14.  
    15.     [SerializeField]
    16.     [Tooltip("The script is currently active")]
    17.     private bool _active = true;
    18.  
    19.     [Space]
    20.  
    21.     [SerializeField]
    22.     [Tooltip("Camera rotation by mouse movement is active")]
    23.     private bool _enableRotation = true;
    24.  
    25.     [SerializeField]
    26.     [Tooltip("Sensitivity of mouse rotation")]
    27.     private float _mouseSense = 1.8f;
    28.  
    29.     [Space]
    30.  
    31.     [SerializeField]
    32.     [Tooltip("Camera zooming in/out by 'Mouse Scroll Wheel' is active")]
    33.     private bool _enableTranslation = true;
    34.  
    35.     [SerializeField]
    36.     [Tooltip("Velocity of camera zooming in/out")]
    37.     private float _translationSpeed = 55f;
    38.  
    39.     [Space]
    40.  
    41.     [SerializeField]
    42.     [Tooltip("Camera movement by 'W','A','S','D','Q','E' keys is active")]
    43.     private bool _enableMovement = true;
    44.  
    45.     [SerializeField]
    46.     [Tooltip("Camera movement speed")]
    47.     private float _movementSpeed = 10f;
    48.  
    49.     [SerializeField]
    50.     [Tooltip("Speed of the quick camera movement when holding the 'Left Shift' key")]
    51.     private float _boostedSpeed = 50f;
    52.  
    53.     [SerializeField]
    54.     [Tooltip("Boost speed")]
    55.     private KeyCode _boostSpeed = KeyCode.LeftShift;
    56.  
    57.     [SerializeField]
    58.     [Tooltip("Move up")]
    59.     private KeyCode _moveUp = KeyCode.E;
    60.  
    61.     [SerializeField]
    62.     [Tooltip("Move down")]
    63.     private KeyCode _moveDown = KeyCode.Q;
    64.  
    65.     [Space]
    66.  
    67.     [SerializeField]
    68.     [Tooltip("Acceleration at camera movement is active")]
    69.     private bool _enableSpeedAcceleration = true;
    70.  
    71.     [SerializeField]
    72.     [Tooltip("Rate which is applied during camera movement")]
    73.     private float _speedAccelerationFactor = 1.5f;
    74.  
    75.     [Space]
    76.  
    77.     [SerializeField]
    78.     [Tooltip("This keypress will move the camera to initialization position")]
    79.     private KeyCode _initPositonButton = KeyCode.R;
    80.  
    81.     #endregion UI
    82.  
    83.     private CursorLockMode _wantedMode;
    84.  
    85.     private float _currentIncrease = 1;
    86.     private float _currentIncreaseMem = 0;
    87.  
    88.     private Vector3 _initPosition;
    89.     private Vector3 _initRotation;
    90.  
    91. #if UNITY_EDITOR
    92.     private void OnValidate()
    93.     {
    94.         if (_boostedSpeed < _movementSpeed)
    95.             _boostedSpeed = _movementSpeed;
    96.     }
    97. #endif
    98.  
    99.  
    100.     private void Start()
    101.     {
    102.         _initPosition = transform.position;
    103.         _initRotation = transform.eulerAngles;
    104.     }
    105.  
    106.     private void OnEnable()
    107.     {
    108.         if (_active)
    109.             _wantedMode = CursorLockMode.Locked;
    110.     }
    111.  
    112.     // Apply requested cursor state
    113.     private void SetCursorState()
    114.     {
    115.         if (Input.GetKeyDown(KeyCode.Escape))
    116.         {
    117.             Cursor.lockState = _wantedMode = CursorLockMode.None;
    118.         }
    119.  
    120.         //if (Input.GetMouseButtonDown(0))
    121.         if (Input.GetKeyDown(KeyCode.F))
    122.         {
    123.             _wantedMode = CursorLockMode.Locked;
    124.         }
    125.  
    126.         // Apply cursor state
    127.         Cursor.lockState = _wantedMode;
    128.         // Hide cursor when locking
    129.         Cursor.visible = (CursorLockMode.Locked != _wantedMode);
    130.     }
    131.  
    132.     private void CalculateCurrentIncrease(bool moving)
    133.     {
    134.         _currentIncrease = Time.deltaTime;
    135.  
    136.         if (!_enableSpeedAcceleration || _enableSpeedAcceleration && !moving)
    137.         {
    138.             _currentIncreaseMem = 0;
    139.             return;
    140.         }
    141.  
    142.         _currentIncreaseMem += Time.deltaTime * (_speedAccelerationFactor - 1);
    143.         _currentIncrease = Time.deltaTime + Mathf.Pow(_currentIncreaseMem, 3) * Time.deltaTime;
    144.     }
    145.  
    146.     private void Update()
    147.     {
    148.         if (!_active)
    149.             return;
    150.  
    151.         SetCursorState();
    152.  
    153.         if (Cursor.visible)
    154.             return;
    155.  
    156.         // Translation
    157.         if (_enableTranslation)
    158.         {
    159.             transform.Translate(Vector3.forward * Input.mouseScrollDelta.y * Time.deltaTime * _translationSpeed);
    160.         }
    161.  
    162.         // Movement
    163.         if (_enableMovement)
    164.         {
    165.             Vector3 deltaPosition = Vector3.zero;
    166.             float currentSpeed = _movementSpeed;
    167.  
    168.             if (Input.GetKey(_boostSpeed))
    169.                 currentSpeed = _boostedSpeed;
    170.  
    171.             if (Input.GetKey(KeyCode.W))
    172.                 deltaPosition += transform.forward;
    173.  
    174.             if (Input.GetKey(KeyCode.S))
    175.                 deltaPosition -= transform.forward;
    176.  
    177.             if (Input.GetKey(KeyCode.A))
    178.                 deltaPosition -= transform.right;
    179.  
    180.             if (Input.GetKey(KeyCode.D))
    181.                 deltaPosition += transform.right;
    182.  
    183.             if (Input.GetKey(_moveUp))
    184.                 deltaPosition += transform.up;
    185.  
    186.             if (Input.GetKey(_moveDown))
    187.                 deltaPosition -= transform.up;
    188.  
    189.             // Calc acceleration
    190.             CalculateCurrentIncrease(deltaPosition != Vector3.zero);
    191.  
    192.             transform.position += deltaPosition * currentSpeed * _currentIncrease;
    193.         }
    194.  
    195.         // Rotation
    196.         if (_enableRotation)
    197.         {
    198.             // Pitch
    199.             transform.rotation *= Quaternion.AngleAxis(
    200.                 -Input.GetAxis("Mouse Y") * _mouseSense,
    201.                 Vector3.right
    202.             );
    203.  
    204.             // Paw
    205.             transform.rotation = Quaternion.Euler(
    206.                 transform.eulerAngles.x,
    207.                 transform.eulerAngles.y + Input.GetAxis("Mouse X") * _mouseSense,
    208.                 transform.eulerAngles.z
    209.             );
    210.         }
    211.  
    212.         // Return to init position
    213.         if (Input.GetKeyDown(_initPositonButton))
    214.         {
    215.             transform.position = _initPosition;
    216.             transform.eulerAngles = _initRotation;
    217.         }
    218.     }
    219. }
    220.  
    The drag script:

    Code (CSharp):
    1. using System;
    2.  
    3. using System.Collections;
    4.  
    5. using System.Collections.Generic;
    6.  
    7. using UnityEngine;
    8.  
    9.  
    10.  
    11. public class DragObject : MonoBehaviour
    12.  
    13. {
    14.  
    15.     private Vector3 mOffset;
    16.  
    17.  
    18.  
    19.     private float mZCoord;
    20.  
    21.  
    22.  
    23.     void OnMouseDown()
    24.  
    25.     {
    26.  
    27.         mZCoord = Camera.main.WorldToScreenPoint(
    28.  
    29.             gameObject.transform.position).z;
    30.  
    31.  
    32.  
    33.         // Store offset = gameobject world pos - mouse world pos
    34.  
    35.         mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    36.  
    37.     }
    38.  
    39.  
    40.  
    41.     private Vector3 GetMouseAsWorldPoint()
    42.  
    43.     {
    44.  
    45.         // Pixel coordinates of mouse (x,y)
    46.  
    47.         Vector3 mousePoint = Input.mousePosition;
    48.  
    49.  
    50.  
    51.         // z coordinate of game object on screen
    52.  
    53.         mousePoint.z = mZCoord;
    54.  
    55.  
    56.  
    57.         // Convert it to world points
    58.  
    59.         return Camera.main.ScreenToWorldPoint(mousePoint);
    60.  
    61.     }
    62.  
    63.  
    64.  
    65.     void OnMouseDrag()
    66.  
    67.     {
    68.  
    69.         transform.position = GetMouseAsWorldPoint() + mOffset;
    70.  
    71.  
    72.     }
    73.  
    74. }
    Then I have been following this tutorial from Unity learn:

    Taking Advantage of the Input System Scripting API

    and my intention was to just modify this tutorial to include "look" controlled by keyboard, and then eventually replace the drag script too. But to keep it as bare-bone as ever possible. After that I want to experiment with using other controllers.

    Can I get some help please?

    Cheers