Godot Version
Godot_v4.2.2
Question
I have been learning godot through godot docs and one of the projects you get to create there is “Squash the creeps 3D” and I am kind of stuck.
for a bit of context there is a ground node with it’s collision shape and also player node. This is the full script that belongs to the player:
extends CharacterBody3D
signal hit
#how fast the player moves in meters per second
@export var speed = 15
#the downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75
#vercial impulse applied to the character upon jumping in meters per second
@export var jump_impulse = 20
#Vertical impulse applied to the character upon bouncing over a mob in
#meters per second
@export var bounce_impulse = 16
#Vector3.ZERO same as Vector3(0,0,0)
var target_velocity = Vector3.ZERO
func _physics_process(delta):
#we create a local variable to store input direction
var direction = Vector3.ZERO
#we check for each move input and update direction accordingly.
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_forward"):
#We are working with vector's x and z axes.
#In 3D, XZ plane is the ground plane
direction.z -= 1
if Input.is_action_pressed("move_back"):
direction.z += 1
if direction != Vector3.ZERO:
direction = direction.normalized() #we have to normalize it in case
#multiple actions were pressed
# Setting the basis property will affect the rotation of the node.
$Pivot.basis = Basis.looking_at(direction)
#ground velocity
target_velocity.x = direction.x * speed
target_velocity.z = direction.z * speed
#Vertical velocity
if not is_on_floor(): #If in the air , fall towards the floor. Literally gravity
target_velocity.y = target_velocity.y - (fall_acceleration * delta)
if is_on_floor() and Input.is_action_pressed("jump"):
target_velocity.y = jump_impulse
# Iterate through all collisions that occurred this frame
for index in range(get_slide_collision_count()):
# We get one of the collisions with the player
var collision = get_slide_collision(index)
# If the collision is with ground
if collision.get_collider() == null:
print("NULL")
continue
# If the collider is with a mob
if collision.get_collider().is_in_group("mob"):
var mob = collision.get_collider()
# we check that we are hitting it from above.
if Vector3.UP.dot(collision.get_normal()) > 0.1:
# If so, we squash it and bounce.
mob.squash()
target_velocity.y = bounce_impulse
# Prevent further duplicate calls.
break
#moving the character
velocity = target_velocity
move_and_slide()
func die():
hit.emit()
queue_free()
func _on_mob_detector_body_entered(body):
die()
The part that I am confused about is this:
Iterate through all collisions that occurred this frame
for index in range(get_slide_collision_count()):
# We get one of the collisions with the player
var collision = get_slide_collision(index)
# If the collision is with ground
if collision.get_collider() == null:
print("NULL")
continue
so here is my question: If there appeared a collision, how can a collider be null? and how can this if statement check wether the collision appeared with the ground or not. btw Ground does not have a mask for player, but player does have a mask for the ground. I am not sure if there was an error with comment (comment above the if statement )because how that block of code can check if the collision appeared with the ground does not make any sense to me.