32 lines
693 B
C#
32 lines
693 B
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class PlayerMovement : Node
|
|
{
|
|
[Export] private CharacterBody2D _body2D;
|
|
|
|
[Export] private float _speed;
|
|
|
|
private Vector2 _direction = new ();
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
ReadInput();
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
float dt = (float)delta;
|
|
|
|
_body2D.SetVelocity(_direction * _speed * dt);
|
|
_body2D.MoveAndSlide();
|
|
}
|
|
|
|
private void ReadInput()
|
|
{
|
|
_direction.X = Input.GetAxis("move_left", "move_right");
|
|
_direction.Y = Input.GetAxis("move_up", "move_down");
|
|
_direction = _direction.Normalized();
|
|
}
|
|
}
|