drag to rotate camera
This commit is contained in:
parent
83e429633e
commit
ce823c06b6
|
@ -6,5 +6,5 @@
|
|||
script = ExtResource( 1 )
|
||||
|
||||
[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
|
||||
|
|
|
@ -4,15 +4,44 @@ using System;
|
|||
public class OrbitCamera : Spatial
|
||||
{
|
||||
[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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue