衝突判定

基本

一定の周期で呼ばれる関数_physics_process()の中で衝突判定を行う

KinematicBodyでの判定

extends KinematicBody

KinematicBodyから衝突判定を行う場合、body_enteredbody_exitedが使用できます body_enteredが衝突開始に呼ばれる関数で、body_exitedが衝突終了時に呼ばれる関数です

判定するにはCollisionShapeを子ノードに設定する必要があります

collisionshape.webp

body_entered

func body_entered(node: Node):
	if(not node.name != "Player"):
		event() #追加の関数

body_exited

func body_exited(node: Node):
	if(not node.name != "Player"):
		event() #追加の関数

StaticBodyでの判定

extends StaticBody

判定する関数がないので、AreaというNodeを使用して判定が行えます
子ノードに加えると判定することができます

staticbody-area-collision.webp

AreaにはKinematicBodyと同様にbody_enteredbody_exitedがあります

extends StaticBody

onready var area = $Area

func _ready():
	area.connect("body_entered", self, "body_entered")
	pass
	
func body_entered(node: Node):
	print(node.name)

上記のコードはAreabody_enteredをStaticBodyの中に新しく作成したbody_enteredに接続して使っています

銃弾やNPCなどPlayerから影響を受けてイベントを起こす必要のあるものに使えると思います

おまけ

get_property_list()get_method_list()get_meta_list()などを使用して使えるプロパティや関数を表示することができる

for key in get_method_list():
		print(key.name)

参考

how do I detect collision between a kinematicbody 2d(player) node and a rigidbody2d node(mob) in Godot
Standard Material 3D
Godot Docs
Collision Object
Kinematic Body
Godot Github Modules
物理の紹介
Area