added collision

This commit is contained in:
2022-11-12 12:59:30 +01:00
parent 906738514b
commit b8e6fe9426
6 changed files with 57 additions and 22 deletions

View File

@@ -1,32 +1,39 @@
using Godot;
using System;
public class Player : Area2D
public class Player : KinematicBody2D
{
private Vector2 velocity;
[Export]
public int speed = 400;
public override void _Ready()
{
}
public override void _Process(float delta)
{
public override void _PhysicsProcess(float delta)
{
GetInput();
MoveAndCollide(velocity * delta);
}
public void GetInput()
{
velocity = new Vector2();
if (Input.IsActionPressed("move_left"))
{
Position -= new Vector2(speed, 0) * delta;
velocity.x -= speed;
}
if (Input.IsActionPressed("move_right"))
{
Position += new Vector2(speed, 0) * delta;
velocity.x += speed;
}
if (Input.IsActionPressed("move_up"))
{
Position -= new Vector2(0, speed) * delta;
velocity.y -= speed;
}
if (Input.IsActionPressed("move_down"))
{
Position += new Vector2(0, speed) * delta;
velocity.y += speed;
}
}
}