godot-start/script/demo07_collision/kinematic_body_2d.gd

31 lines
877 B
GDScript
Raw Normal View History

2021-10-05 05:50:57 +00:00
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
2022-07-02 04:22:49 +00:00
# false这个选项参数看意思是把无限惯性关了这个关了之后就不会向碰撞的刚体施加作用力需要你自己去写
2022-07-02 04:24:28 +00:00
var info: KinematicCollision2D = move_and_collide(velocity * delta, false)
2021-10-05 05:50:57 +00:00
if info != null:
print(info.collider.name)