mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-14 21:43:47 +00:00
3D Aiming
This commit is contained in:
parent
341f76d885
commit
595bdcc501
3 changed files with 24 additions and 4 deletions
|
|
@ -123,6 +123,7 @@ public partial class KeyboardInputProvider : InputProvider
|
|||
|
||||
private Vector2 GetMouseAimInput()
|
||||
{
|
||||
|
||||
return _mouseAImProvider?.GetMouseAimInput() ?? Vector2.Zero;
|
||||
// //Camera2D camera = GetViewport().GetCamera2D();
|
||||
// //if (camera == null) return Vector2.Zero; // Ensure there's a valid camera
|
||||
|
|
|
|||
|
|
@ -5,13 +5,32 @@ namespace Cirno.Scripts.Components.Actors;
|
|||
|
||||
public partial class MouseAimProvider3D : Node3D, IMouseAimProvider
|
||||
{
|
||||
public Vector2 GetMouseAimInput()
|
||||
private Viewport _viewport;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Vector2 mouseWorldPos = DisplayServer.MouseGetPosition();
|
||||
|
||||
var screenPosition = CameraController3D.Instance.UnprojectPosition(this.GlobalPosition);
|
||||
|
||||
return mouseWorldPos - screenPosition;
|
||||
_viewport = GetViewport();
|
||||
}
|
||||
|
||||
public Vector2 GetMouseAimInput()
|
||||
{
|
||||
var mouseWorldPos = GetMouseWorldPosition(this.GlobalPosition.Y);
|
||||
|
||||
var direction = (mouseWorldPos - this.GlobalPosition).Normalized();
|
||||
|
||||
return new Vector2(direction.X, direction.Z);
|
||||
}
|
||||
|
||||
public Vector3 GetMouseWorldPosition(float planeHeight = 0)
|
||||
{
|
||||
var mousePos = _viewport.GetMousePosition();
|
||||
var rayOrigin = CameraController3D.Instance.ProjectRayOrigin(mousePos);
|
||||
var rayDirection = CameraController3D.Instance.ProjectRayNormal(mousePos);
|
||||
|
||||
var plane = new Plane(Vector3.Up, planeHeight);
|
||||
|
||||
var intersection = plane.IntersectsRay(rayOrigin, rayDirection);
|
||||
|
||||
return intersection ?? Vector3.Zero;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue