瀏覽代碼

Fix camera colliding with the player in 3D platformer demo (#1187)

This was most common at low physics tick rates like 20 TPS.

This also fixes an issue with the RigidBody character demo having
movement speed depending on the physics tick rate.
Hugo Locurcio 4 月之前
父節點
當前提交
90617ba1e8

File diff suppressed because it is too large
+ 1 - 1
3d/kinematic_character/cubelib.tres


+ 5 - 5
3d/kinematic_character/level.tscn

@@ -56,7 +56,7 @@ tracks/0/keys = {
 
 [sub_resource type="AnimationLibrary" id="AnimationLibrary_uxnju"]
 _data = {
-"updown1": SubResource("2")
+&"updown1": SubResource("2")
 }
 
 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_6xp3u"]
@@ -84,8 +84,8 @@ tracks/0/keys = {
 
 [sub_resource type="AnimationLibrary" id="AnimationLibrary_o6d7g"]
 _data = {
-"side": SubResource("3"),
-"updown1": SubResource("2")
+&"side": SubResource("3"),
+&"updown1": SubResource("2")
 }
 
 [sub_resource type="BoxShape3D" id="4"]
@@ -131,7 +131,7 @@ shape = SubResource("BoxShape3D_s7fhl")
 [node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator1"]
 callback_mode_process = 0
 libraries = {
-"": SubResource("AnimationLibrary_uxnju")
+&"": SubResource("AnimationLibrary_uxnju")
 }
 autoplay = "updown1"
 
@@ -148,7 +148,7 @@ shape = SubResource("BoxShape3D_s7fhl")
 [node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator2"]
 callback_mode_process = 0
 libraries = {
-"": SubResource("AnimationLibrary_o6d7g")
+&"": SubResource("AnimationLibrary_o6d7g")
 }
 autoplay = "side"
 

+ 1 - 1
3d/kinematic_character/player/cubio.gd

@@ -12,7 +12,7 @@ const DECELERATION = 4
 func _physics_process(delta: float) -> void:
 	if Input.is_action_just_pressed(&"exit"):
 		get_tree().quit()
-	if Input.is_action_just_pressed(&"reset_position") or global_position.y < - 6:
+	if Input.is_action_just_pressed(&"reset_position") or global_position.y < -6.0:
 		# Pressed the reset key or fell off the ground.
 		position = start_position
 		velocity = Vector3.ZERO

+ 2 - 2
3d/kinematic_character/player/cubio.tscn

@@ -1,8 +1,8 @@
 [gd_scene load_steps=6 format=3 uid="uid://c1j6vfe3s2bq8"]
 
-[ext_resource type="Script" path="res://player/cubio.gd" id="1"]
+[ext_resource type="Script" uid="uid://31pf3ace5gs2" path="res://player/cubio.gd" id="1"]
 [ext_resource type="ArrayMesh" uid="uid://ba7dqpj07mlsy" path="res://models/cube.mesh" id="2"]
-[ext_resource type="Script" path="res://player/follow_camera.gd" id="3"]
+[ext_resource type="Script" uid="uid://dp4h5reedxr3q" path="res://player/follow_camera.gd" id="3"]
 [ext_resource type="Material" uid="uid://betphk8l40sig" path="res://models/white_cube_material.tres" id="4"]
 
 [sub_resource type="BoxShape3D" id="1"]

+ 4 - 2
3d/platformer/player/follow_camera.gd

@@ -13,14 +13,16 @@ var collision_exception: Array[RID] = []
 
 func _ready() -> void:
 	# Find collision exceptions for ray.
+	# This is done to prevent the player and enemies from colliding
+	# with the camera.
 	var node: Node = self
 	while is_instance_valid(node):
-		if node is RigidBody3D:
+		if node is RigidBody3D or node is CharacterBody3D:
 			collision_exception.append(node.get_rid())
 			break
 		else:
 			node = node.get_parent()
-	set_physics_process(true)
+
 	# This detaches the camera transform from the parent spatial node.
 	set_as_top_level(true)
 

File diff suppressed because it is too large
+ 1 - 1
3d/rigidbody_character/cubelib.tres


+ 6 - 6
3d/rigidbody_character/level.tscn

@@ -4,7 +4,7 @@
 [ext_resource type="PackedScene" uid="uid://cbvuesb1ptdh4" path="res://player/cubio.tscn" id="2"]
 [ext_resource type="ArrayMesh" uid="uid://h65pkfq5sgmy" path="res://models/cube.mesh" id="3"]
 [ext_resource type="PackedScene" uid="uid://bonauusmt0ss" path="res://models/mushroom.glb" id="5"]
-[ext_resource type="Script" path="res://level.gd" id="6"]
+[ext_resource type="Script" uid="uid://cwpf43cjk6o52" path="res://level.gd" id="6"]
 [ext_resource type="Texture2D" uid="uid://5wiey7d4yffc" path="res://models/white_wood.png" id="6_slp5m"]
 [ext_resource type="PackedScene" uid="uid://bftavngs5kq7e" path="res://cube_rigidbody.tscn" id="7"]
 
@@ -58,7 +58,7 @@ tracks/0/keys = {
 
 [sub_resource type="AnimationLibrary" id="AnimationLibrary_64cvx"]
 _data = {
-"updown1": SubResource("2")
+&"updown1": SubResource("2")
 }
 
 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_0eb0o"]
@@ -86,8 +86,8 @@ tracks/0/keys = {
 
 [sub_resource type="AnimationLibrary" id="AnimationLibrary_4vwp8"]
 _data = {
-"side": SubResource("3"),
-"updown1": SubResource("2")
+&"side": SubResource("3"),
+&"updown1": SubResource("2")
 }
 
 [sub_resource type="BoxShape3D" id="4"]
@@ -134,7 +134,7 @@ shape = SubResource("BoxShape3D_w6bmu")
 [node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator1"]
 callback_mode_process = 0
 libraries = {
-"": SubResource("AnimationLibrary_64cvx")
+&"": SubResource("AnimationLibrary_64cvx")
 }
 autoplay = "updown1"
 
@@ -151,7 +151,7 @@ shape = SubResource("BoxShape3D_w6bmu")
 [node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator2"]
 callback_mode_process = 0
 libraries = {
-"": SubResource("AnimationLibrary_4vwp8")
+&"": SubResource("AnimationLibrary_4vwp8")
 }
 autoplay = "side"
 

+ 4 - 4
3d/rigidbody_character/player/cubio.gd

@@ -4,10 +4,10 @@ extends RigidBody3D
 @onready var camera: Camera3D = $Target/Camera3D
 @onready var start_position := position
 
-func _physics_process(_delta: float) -> void:
+func _physics_process(delta: float) -> void:
 	if Input.is_action_just_pressed(&"exit"):
 		get_tree().quit()
-	if Input.is_action_just_pressed(&"reset_position") or global_position.y < - 6:
+	if Input.is_action_just_pressed(&"reset_position") or global_position.y < -6.0:
 		# Pressed the reset key or fell off the ground.
 		position = start_position
 		linear_velocity = Vector3.ZERO
@@ -23,11 +23,11 @@ func _physics_process(_delta: float) -> void:
 	dir = cam_basis * dir
 
 	# Air movement.
-	apply_central_impulse(dir.normalized() * 0.04)
+	apply_central_impulse(dir.normalized() * 5.0 * delta)
 
 	if on_ground():
 		# Ground movement (higher acceleration).
-		apply_central_impulse(dir.normalized() * 0.08)
+		apply_central_impulse(dir.normalized() * 10.0 * delta)
 
 		# Jumping code.
 		# It's acceptable to set `linear_velocity` here as it's only set once, rather than continuously.

+ 2 - 2
3d/rigidbody_character/player/cubio.tscn

@@ -1,7 +1,7 @@
 [gd_scene load_steps=8 format=3 uid="uid://cbvuesb1ptdh4"]
 
-[ext_resource type="Script" path="res://player/cubio.gd" id="1"]
-[ext_resource type="Script" path="res://player/follow_camera.gd" id="3"]
+[ext_resource type="Script" uid="uid://8e5vq04gpm2b" path="res://player/cubio.gd" id="1"]
+[ext_resource type="Script" uid="uid://86cixhtmw0n3" path="res://player/follow_camera.gd" id="3"]
 [ext_resource type="Material" uid="uid://6m447vojdbos" path="res://models/white_cube_material.tres" id="4"]
 
 [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_5bo75"]

Some files were not shown because too many files changed in this diff