drag to rotate camera

This commit is contained in:
Cat Flynn 2022-09-04 17:37:35 +01:00
parent dcfd17551b
commit c1dd67d67a
2 changed files with 34 additions and 5 deletions

View File

@ -6,5 +6,5 @@
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="."] [node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.913325, 0.407231, 0, -0.407231, 0.913325, 0, 24.1191, 58.6188 ) transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 )
fov = 25.0 fov = 25.0

View File

@ -4,15 +4,44 @@ using System;
public class OrbitCamera : Spatial public class OrbitCamera : Spatial
{ {
[Export] [Export]
private float _speed; private float _sensitivity = 200f;
public override void _Ready() private Vector2 _rotation;
private bool _canRotate = false;
public override void _Input(InputEvent e)
{ {
if (e is InputEventMouseMotion mouseMotion)
{
HandleMouseMovement(mouseMotion);
}
if (e is InputEventMouseButton mouseButton)
{
HandleMouseButton(mouseButton);
}
} }
public override void _Process(float delta) public override void _Process(float delta)
{ {
Rotate(Vector3.Up, _speed * delta); Rotation = Vector3.Zero;
var sensitivity = -1f / _sensitivity;
Rotate(Vector3.Right, _rotation.y * sensitivity);
Rotate(Vector3.Up, _rotation.x * sensitivity);
}
// left click to drag
private void HandleMouseButton(InputEventMouseButton mouseButton)
{
if (mouseButton.ButtonIndex != (int)ButtonList.Left) return;
_canRotate = mouseButton.Pressed;
}
private void HandleMouseMovement(InputEventMouseMotion mouseMotion)
{
if (!_canRotate) return;
var delta = mouseMotion.Relative;
_rotation += delta;
} }
} }