Last active
February 4, 2023 16:56
-
-
Save EgorBron/1f7986acdef3e1716f874f111ce8ebf9 to your computer and use it in GitHub Desktop.
Simple Freecam (or "Noclip") camera for Godot C# API. Простая свободная (или "ноуклип") камера для Godot C# API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
/// <summary> | |
/// Freecam (or "Noclip") camera / Свободная (или "ноуклип") камера | |
/// </summary> | |
public class Freecam : Camera { | |
[Export] | |
public float speed = 1; // Cam movement speed / Скорость движения | |
[Export] | |
public float sensivity = .01f; // Mouse sensivity / Сенса (чувствительность) мыши | |
[Export] | |
public float upLookLock = 90f; // Maximum (upper) look angle / Максимальный угол обзора (когда смотришь вверх) | |
[Export] | |
public float downLookLock = -90f; // Minimum (down) look angle / Минимальный угол (когда смотришь вниз) | |
/// <summary> | |
/// When "Camera" Node is ready / Когда нода "Camera" подгрузилась | |
/// </summary> | |
public override void _Ready() { | |
Input.SetMouseMode(Input.MouseMode.Captured); // Hides cursor and keeps it in center / Скрывает курсор и держит его в центре | |
} | |
/// <summary> | |
/// just for convenience / чисто для удобства | |
/// </summary> | |
/// <param name="key">Key to chek it is pressed / Кнопка, нажатие которой надо проверить</param> | |
/// <returns>Is provided key pressed / Нажата ли кнопка</returns> | |
bool Press(KeyList key) { | |
return Input.IsKeyPressed((int)key); | |
} | |
/// <summary> | |
/// Calls every frame / Вызывается каждый кадр | |
/// </summary> | |
/// <param name="delta">Time passed after previous frame / Время, прошедшее с прошлого кадра</param> | |
public override void _Process(float delta) { | |
var vel = Vector3.Zero; | |
if (Press(KeyList.W)) vel = Vector3.Forward * speed; | |
if (Press(KeyList.A)) vel = Vector3.Left * speed; | |
if (Press(KeyList.S)) vel = Vector3.Back * speed; | |
if (Press(KeyList.D)) vel = Vector3.Right * speed; | |
if (Press(KeyList.Space)) vel = Vector3.Up * speed; | |
if (Press(KeyList.Shift)) vel = Vector3.Down * speed; | |
TranslateObjectLocal(vel); // Move the camera / Переместить камеру | |
} | |
/// <summary> | |
/// Calls when any input event happends / Вызывается при любом событии, связанном с вводом | |
/// </summary> | |
/// <param name="_event">what`s happend / что произошло</param> | |
public override void _Input(InputEvent _event) { | |
// Check what this event is triggered by mouse movement / Проверка на то, что этот ивент произошёл из-за движения мышки | |
if (_event is InputEventMouseMotion msev) { | |
var mouseD = msev.Relative; | |
if (mouseD != Vector2.Zero) { // if mouse movement happend / если движение мышкой было | |
// Idk why I can`t assign values to vec3.RotationDegrees properties directly | |
// Хз почему не получается присвоить значения полям vec3.RotationDegrees сразу | |
var rot = RotationDegrees; | |
// Set rotation for up/down look and clamp angle / Установить вращение вверх/вниз и зафиксировать угол | |
rot.x = Mathf.Clamp(rot.x - mouseD.y * sensivity, downLookLock, upLookLock); | |
// Set rotation for right/left look / Установить вращение вправо/влево | |
rot.y -= mouseD.x * sensivity; | |
// Apply the rotation / Применить вращение | |
RotationDegrees = rot; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment