mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-03 05:48:42 +03:00
Add C# to 2D Movement
This commit is contained in:
@@ -36,7 +36,8 @@ fact that the player can move diagonally by pressing two keys at the same time.
|
||||
|
||||
Add a script to the kinematic body and add the following code:
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
extends KinematicBody2D
|
||||
|
||||
@@ -60,6 +61,46 @@ Add a script to the kinematic body and add the following code:
|
||||
get_input()
|
||||
move_and_slide(velocity)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Movement : KinematicBody2D
|
||||
{
|
||||
[Export] public int Speed = 200;
|
||||
|
||||
Vector2 velocity = new Vector2();
|
||||
|
||||
public void GetInput()
|
||||
{
|
||||
velocity = new Vector2();
|
||||
if (Input.IsActionPressed("right"))
|
||||
{
|
||||
velocity.x += 1;
|
||||
}
|
||||
if (Input.IsActionPressed("left"))
|
||||
{
|
||||
velocity.x -= 1;
|
||||
}
|
||||
if (Input.IsActionPressed("down"))
|
||||
{
|
||||
velocity.y += 1;
|
||||
}
|
||||
if (Input.IsActionPressed("up"))
|
||||
{
|
||||
velocity.y -= 1;
|
||||
}
|
||||
velocity = velocity.Normalized() * Speed;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
GetInput();
|
||||
MoveAndSlide(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
In the ``get_input()`` function we check for the four key events and sum them
|
||||
up to get the velocity vector. This has the benefit of making two opposite keys
|
||||
cancel each other out, but will also result in diagonal movement being faster
|
||||
@@ -84,7 +125,8 @@ while up/down moves it forward or backward in whatever direction it's facing.
|
||||
|
||||
.. image:: img/movement_rotate1.gif
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
extends KinematicBody2D
|
||||
|
||||
@@ -111,6 +153,50 @@ while up/down moves it forward or backward in whatever direction it's facing.
|
||||
rotation += rotation_dir * rotation_speed * delta
|
||||
move_and_slide(velocity)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Movement : KinematicBody2D
|
||||
{
|
||||
[Export] public int Speed = 200;
|
||||
[Export] public float RotationSpeed = 1.5f;
|
||||
|
||||
Vector2 velocity = new Vector2();
|
||||
int rotationDir = 0;
|
||||
|
||||
public void GetInput()
|
||||
{
|
||||
rotationDir = 0;
|
||||
velocity = new Vector2();
|
||||
if (Input.IsActionPressed("right"))
|
||||
{
|
||||
rotationDir += 1;
|
||||
}
|
||||
if (Input.IsActionPressed("left"))
|
||||
{
|
||||
rotationDir -= 1;
|
||||
}
|
||||
if (Input.IsActionPressed("down"))
|
||||
{
|
||||
velocity = new Vector2(-Speed, 0).Rotated(Rotation);
|
||||
}
|
||||
if (Input.IsActionPressed("up"))
|
||||
{
|
||||
velocity = new Vector2(Speed, 0).Rotated(Rotation);
|
||||
}
|
||||
velocity = velocity.Normalized() * Speed;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
GetInput();
|
||||
Rotation += rotationDir * RotationSpeed * delta;
|
||||
MoveAndSlide(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
Here we've added two new variables to track our rotation direction and speed.
|
||||
Again, pressing both keys at once will cancel out and result in no rotation.
|
||||
The rotation is applied directly to the body's ``rotation`` property.
|
||||
@@ -129,7 +215,8 @@ is set by the mouse position instead of the keyboard. The character will always
|
||||
|
||||
.. image:: img/movement_rotate2.gif
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
extends KinematicBody2D
|
||||
|
||||
@@ -149,14 +236,52 @@ is set by the mouse position instead of the keyboard. The character will always
|
||||
get_input()
|
||||
move_and_slide(velocity)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Movement : KinematicBody2D
|
||||
{
|
||||
[Export] public int Speed = 200;
|
||||
|
||||
Vector2 velocity = new Vector2();
|
||||
|
||||
public void GetInput()
|
||||
{
|
||||
LookAt(GetGlobalMousePosition());
|
||||
velocity = new Vector2();
|
||||
if (Input.IsActionPressed("down"))
|
||||
{
|
||||
velocity = new Vector2(-Speed, 0).Rotated(Rotation);
|
||||
}
|
||||
if (Input.IsActionPressed("up"))
|
||||
{
|
||||
velocity = new Vector2(Speed, 0).Rotated(Rotation);
|
||||
}
|
||||
velocity = velocity.Normalized() * Speed;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
GetInput();
|
||||
MoveAndSlide(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
Here we're using the :ref:`Node2D <class_Node2D>` ``look_at()`` method to
|
||||
point the player towards a given position. Without this function, you
|
||||
could get the same effect by setting the angle like this:
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
rotation = get_global_mouse_position().angle_to_point(position)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
var rotation = GetGlobalMousePosition().AngleToPoint(Position);
|
||||
|
||||
|
||||
Click-and-Move
|
||||
--------------
|
||||
@@ -166,7 +291,8 @@ on the screen will cause the player to move to the target location.
|
||||
|
||||
.. image:: img/movement_click.gif
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
extends KinematicBody2D
|
||||
|
||||
@@ -185,6 +311,37 @@ on the screen will cause the player to move to the target location.
|
||||
if (target - position).length() > 5:
|
||||
move_and_slide(velocity)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class Movement : KinematicBody2D
|
||||
{
|
||||
[Export] public int Speed = 200;
|
||||
|
||||
Vector2 target = new Vector2();
|
||||
Vector2 velocity = new Vector2();
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event.IsActionPressed("click"))
|
||||
{
|
||||
target = GetGlobalMousePosition();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
velocity = (target - Position).Normalized() * Speed;
|
||||
// Rotation = velocity.Angle();
|
||||
if ((target - Position).Length() > 5)
|
||||
{
|
||||
MoveAndSlide(velocity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Note the ``length()`` check we make prior to movement. Without this test,
|
||||
the body would "jitter" upon reaching the target position, as it moves
|
||||
|
||||
Reference in New Issue
Block a user