drag to rotate camera

This commit is contained in:
Cat Flynn 2022-09-04 17:37:35 +01:00
parent 0cf3bb3620
commit cd800be3eb
3 changed files with 35 additions and 6 deletions

View File

@ -12,7 +12,6 @@
transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 ) transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 )
[node name="OrbitCamera" parent="." instance=ExtResource( 3 )] [node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
_speed = 1.0
[node name="Railway" parent="." instance=ExtResource( 2 )] [node name="Railway" parent="." instance=ExtResource( 2 )]
script = null script = null

View File

@ -6,4 +6,5 @@
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="."] [node name="Camera" type="Camera" parent="."]
transform = Transform( 0.636065, -0.249781, 0.73009, 0, 0.946159, 0.323703, -0.771636, -0.205896, 0.601818, 8.49012, 5.30069, 6.71091 ) transform = Transform( 1, 0, 0, 0, 0.964146, 0.265371, 0, -0.265371, 0.964146, 0, 2.21115, 9.48434 )
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;
} }
} }