Browse Source

Merge pull request #3037 from cbscribe/kcc_2dmove_updates

Update "Click and Move" to use Vector2 helpers
Rémi Verschelde 5 years ago
parent
commit
33bb17d846
1 changed files with 27 additions and 27 deletions
  1. 27 27
      tutorials/2d/2d_movement.rst

+ 27 - 27
tutorials/2d/2d_movement.rst

@@ -68,9 +68,9 @@ Add a script to the kinematic body and add the following code:
 
     public class Movement : KinematicBody2D
     {
-        [Export] public int Speed = 200;
+        [Export] public int speed = 200;
 
-        Vector2 velocity = new Vector2();
+        public Vector2 velocity = new Vector2();
 
         public void GetInput()
         {
@@ -88,7 +88,7 @@ Add a script to the kinematic body and add the following code:
             if (Input.IsActionPressed("up"))
                 velocity.y -= 1;
 
-            velocity = velocity.Normalized() * Speed;
+            velocity = velocity.Normalized() * speed;
         }
 
         public override void _PhysicsProcess(float delta)
@@ -153,11 +153,11 @@ while up/down moves it forward or backward in whatever direction it's facing.
 
     public class Movement : KinematicBody2D
     {
-        [Export] public int Speed = 200;
-        [Export] public float RotationSpeed = 1.5f;
+        [Export] public int speed = 200;
+        [Export] public float rotationSpeed = 1.5f;
 
-        Vector2 velocity = new Vector2();
-        int rotationDir = 0;
+        public Vector2 velocity = new Vector2();
+        public int rotationDir = 0;
 
         public void GetInput()
         {
@@ -171,18 +171,18 @@ while up/down moves it forward or backward in whatever direction it's facing.
                 rotationDir -= 1;
 
             if (Input.IsActionPressed("down"))
-                velocity = new Vector2(-Speed, 0).Rotated(Rotation);
+                velocity = new Vector2(-speed, 0).Rotated(Rotation);
 
             if (Input.IsActionPressed("up"))
-                velocity = new Vector2(Speed, 0).Rotated(Rotation);
+                velocity = new Vector2(speed, 0).Rotated(Rotation);
 
-            velocity = velocity.Normalized() * Speed;
+            velocity = velocity.Normalized() * speed;
         }
 
         public override void _PhysicsProcess(float delta)
         {
             GetInput();
-            Rotation += rotationDir * RotationSpeed * delta;
+            Rotation += rotationDir * rotationSpeed * delta;
             velocity = MoveAndSlide(velocity);
         }
     }
@@ -233,9 +233,9 @@ is set by the mouse position instead of the keyboard. The character will always
 
     public class Movement : KinematicBody2D
     {
-        [Export] public int Speed = 200;
+        [Export] public int speed = 200;
 
-        Vector2 velocity = new Vector2();
+        public Vector2 velocity = new Vector2();
 
         public void GetInput()
         {
@@ -243,12 +243,12 @@ is set by the mouse position instead of the keyboard. The character will always
             velocity = new Vector2();
 
             if (Input.IsActionPressed("down"))
-                velocity = new Vector2(-Speed, 0).Rotated(Rotation);
+                velocity = new Vector2(-speed, 0).Rotated(Rotation);
 
             if (Input.IsActionPressed("up"))
-                velocity = new Vector2(Speed, 0).Rotated(Rotation);
+                velocity = new Vector2(speed, 0).Rotated(Rotation);
 
-            velocity = velocity.Normalized() * Speed;
+            velocity = velocity.Normalized() * speed;
         }
 
         public override void _PhysicsProcess(float delta)
@@ -295,9 +295,9 @@ on the screen will cause the player to move to the target location.
             target = get_global_mouse_position()
 
     func _physics_process(delta):
-        velocity = (target - position).normalized() * speed
-        # rotation = velocity.angle()
-        if (target - position).length() > 5:
+        velocity = position.direction_to(target) * speed
+        # look_at(target)
+        if position.distance_to(target) > 5:
             velocity = move_and_slide(velocity)
 
  .. code-tab:: csharp
@@ -307,10 +307,10 @@ on the screen will cause the player to move to the target location.
 
     public class Movement : KinematicBody2D
     {
-        [Export] public int Speed = 200;
+        [Export] public int speed = 200;
 
-        Vector2 target = new Vector2();
-        Vector2 velocity = new Vector2();
+        public Vector2 target = new Vector2();
+        public Vector2 velocity = new Vector2();
 
         public override void _Input(InputEvent @event)
         {
@@ -322,9 +322,9 @@ on the screen will cause the player to move to the target location.
 
         public override void _PhysicsProcess(float delta)
         {
-            velocity = (target - Position).Normalized() * Speed;
-            // Rotation = velocity.Angle();
-            if ((target - Position).Length() > 5)
+            velocity = Position.DirectionTo(target) * speed;
+            // LookAt(target);
+            if (Position.DistanceTo(target) > 5)
             {
                 velocity = MoveAndSlide(velocity);
             }
@@ -332,12 +332,12 @@ on the screen will cause the player to move to the target location.
     }
 
 
-Note the ``length()`` check we make prior to movement. Without this test,
+Note the ``distance_to()`` check we make prior to movement. Without this test,
 the body would "jitter" upon reaching the target position, as it moves
 slightly past the position and tries to move back, only to move too far and
 repeat.
 
-Uncommenting the ``rotation`` line will also turn the body to point in its
+Uncommenting the ``look_at()`` line will also turn the body to point in its
 direction of motion if you prefer.
 
 .. tip:: This technique can also be used as the basis of a "following" character.