Untitled
unknown
csharp
a year ago
1.8 kB
8
Indexable
public partial class Camera : Camera3D
{
[Export] private float mouseSpeed = 300f;
[Export] private float moveSpeed = 5f;
[Export] private float acceleration = 25f;
private Vector3 velocity = Vector3.Zero;
private Vector2 lookAngles = Vector2.Zero;
private Ground ground;
private Info info;
public Camera(Ground ground)
{
this.ground = ground; // no used but might be useful to calculate direction correctly, how? idk
}
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
}
public override void _Process(double delta)
{
lookAngles.Y = Mathf.Clamp(lookAngles.Y, Mathf.Pi / -2, Mathf.Pi / 2);
Rotation = new Vector3(lookAngles.Y, lookAngles.X, 0f);
Vector3 direction = getDirection();
if (direction == Vector3.Zero) {
velocity = Vector3.Zero;
}
if (direction.LengthSquared() > 0) {
velocity += direction * acceleration * (float) delta;
}
if (velocity.Length() > moveSpeed) {
velocity = velocity.Normalized() * moveSpeed;
}
Translate(velocity * (float) delta);
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseEvent) {
lookAngles -= mouseEvent.Relative / mouseSpeed;
}
}
private Vector3 getDirection()
{
Vector3 direction = Vector3.Zero;
if (Input.IsKeyPressed(Key.W)) {
direction += Vector3.Forward;
}
if (Input.IsKeyPressed(Key.S)) {
direction += Vector3.Back;
}
if (Input.IsKeyPressed(Key.A)) {
direction += Vector3.Left;
}
if (Input.IsKeyPressed(Key.D)) {
direction += Vector3.Right;
}
if (Input.IsKeyPressed(Key.Space)) {
direction += Vector3.Up;
}
if (Input.IsKeyPressed(Key.Shift)) {
direction += Vector3.Down;
}
return direction.Normalized();
}
}Editor is loading...
Leave a Comment