Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | pogi.dev |
I having issues with destroying my mob when I jump on them in Godot 4.0.1. I am following the tutorial and have my code matching what I see in the docs, but my player doesn’t destroy the mob when it is collided with the top or jumped on.
extends CharacterBody3D
#setting speed of mob character
@export var min_speed = 10
@export var max_speed = 18
signal squashed
@warning_ignore("unused_parameter")
func _physics_process(delta):
move_and_slide()
func initialize(start_position, player_position):
look_at_from_position(start_position, player_position, Vector3.UP)
rotate_y(randf_range(-PI/4, PI/4))
var random_speed = randi_range(min_speed, max_speed)
velocity = Vector3.FORWARD * random_speed
velocity = velocity.rotated(Vector3.UP, rotation.y)
func squash():
squashed.emit()
queue_free()
func _on_visible_on_screen_notifier_3d_screen_exited():
queue_free()
This is the script for my player:
extends CharacterBody3D
# How fast the player moves in meters per second.
@export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75
@export var jump_impulse = 20
@export var bounce_impulse = 16
var target_velocity = Vector3.ZERO
func _physics_process(delta):
var direction = Vector3.ZERO
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_back"):
direction.z += 1
if Input.is_action_pressed("move_forward"):
direction.z -= 1
if direction != Vector3.ZERO:
direction = direction.normalized()
$Pivot.look_at(position + direction, Vector3.UP)
# 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)
# Moving the Character
velocity = target_velocity
move_and_slide()
if is_on_floor() and Input.is_action_pressed("jump"):
target_velocity.y = jump_impulse
for index in range(get_slide_collision_count()):
var collision = get_slide_collision(index)
if (collision.get_collider() == null):
continue
if collision.get_collider().is_in_group("mob"):
var mob = collision.get_collider()
if Vector3.UP.dot(collision.get_normal()) > 0.1:
mob.squash()
target_velocity.y = bounce_impulse
Welcome to the world of debugging. Are you sure your code is being run? To start, add some print
statements to confirm the code is executing. If those print
statements don’t, well, print… perhaps the mobs aren’t in the correct group or the layers are incorrectly configured. Gotta go back and check because the code looks alright.
spaceyjase | 2023-03-30 15:38