pull/2/head
jaysunxiao 2022-07-02 12:22:49 +08:00
parent abe65f5898
commit 33e5718b25
2 changed files with 5 additions and 37 deletions

View File

@ -1,44 +1,12 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://script/demo07_collision/kinematic_body_2d.gd" type="Script" id=2]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 31.5644, 29.6976 )
[sub_resource type="GDScript" id=2]
script/source = "extends KinematicBody2D
# How fast the player will move (pixels/sec).
export var speed = 400
# Size of the game window.
var screen_size
func _ready():
screen_size = get_viewport_rect().size
func _physics_process(delta):
var velocity = Vector2.ZERO # The player's movement vector.
if Input.is_action_pressed(\"move_right\"):
velocity.x += 1
if Input.is_action_pressed(\"move_left\"):
velocity.x -= 1
if Input.is_action_pressed(\"move_down\"):
velocity.y += 1
if Input.is_action_pressed(\"move_up\"):
velocity.y -= 1
if velocity.length() <= 0:
return
velocity = velocity.normalized() * speed
var info: KinematicCollision2D = move_and_collide(velocity * delta)
if info != null:
print(info.collider.name)
"
[sub_resource type="RectangleShape2D" id=3]
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 30.17, 29.3458 )
[node name="Node2D" type="Node2D"]
@ -67,13 +35,13 @@ texture = ExtResource( 1 )
[node name="KinematicBody2D" type="KinematicBody2D" parent="."]
position = Vector2( 549.424, 338.422 )
script = SubResource( 2 )
script = ExtResource( 2 )
__meta__ = {
"_edit_group_": true
}
[node name="CollisionShape2D" type="CollisionShape2D" parent="KinematicBody2D"]
shape = SubResource( 3 )
shape = SubResource( 2 )
[node name="Sprite" type="Sprite" parent="KinematicBody2D"]
texture = ExtResource( 1 )

View File

@ -24,7 +24,7 @@ func _physics_process(delta):
return
velocity = velocity.normalized() * speed
# false这个选项参数看意思是把无限惯性关了这个关了之后就不会向碰撞的刚体施加作用力需要你自己去写
var info: KinematicCollision2D = move_and_collide(velocity * delta)
if info != null:
print(info.collider.name)