#Another pathfinding issue

33 messages · Page 1 of 1 (latest)

shadow wharf
#

so this is the script for my path finding person mosnter thing
extends CharacterBody2D

Speed of the ghost's movement

var speed = 400

Declare the player variable

var player : CharacterBody2D = null

Called when the node is ready

func _ready():
# Get the player (Devin) node
player = get_node("../Devin") # Adjust path if needed

func _physics_process(delta):
if player == null:
print("Player reference is null. Check the path or node structure.")
return

# Get the direction to the player (Devin)
var direction = (player.global_position - global_position).normalized()

# Set velocity for movement (direction * speed)
velocity = direction * speed  # Use the `velocity` property in Godot 4

# Move the Ghost towards the player (Devin)
move_and_slide()

# Check for collision with the player (Devin)
for i in range(get_slide_collision_count()):
    var collision = get_slide_collision(i)
    if collision.collider == player:
        # Restart the game (reload the current scene)
        get_tree().reload_current_scene()
        return

and when the object approaches the player it stops just like in the picture above and stop moving towards the player. How do i make it so that it actually goes to the player and restarts the game when it touches it

acoustic hazel
#

could it be that that's the position of the player for some reason?

shadow wharf
#

could you please exxplain what your asking

acoustic hazel
#

i mean that your code should work, it should move the ghost to the players position. so the only problem i could think of is that your player position, the origin of it's main node (probably also a characterbody) isn't actually on the sprite, but instead somewhere higher and a bit to the left.
Just show a screenshot of your player scene.

shadow wharf
#

okay

#

my godot is laggy so it might take a bit

#

Devin is the maincharacter

acoustic hazel
#

yes, but i meant a screenshot of that scene, the Devin-Scene, and also not just the nodes, but also how they look.

shadow wharf
#

like the inside of devin?

#

so liek this

hearty lotus
#

@shadow wharf can you share Devin's movement script please? Can you also please share the Ghost's inspector info (right panel) when you select their CharacterBody2D in their local scene?

acoustic hazel
# shadow wharf

Yes, but i meant more like a screenshot of the whole editor screen.

shadow wharf
# hearty lotus <@850011611698692096> can you share Devin's movement script please? Can you also...

devins movment
extends CharacterBody2D

Movement speed

var speed = 200

Jump strength

var jump_force = -500

Gravity

var gravity = 1200

Called every frame

func _physics_process(delta):
# Apply gravity
if not is_on_floor():
velocity.y += gravity * delta
else:
velocity.y = 0

# Handle left/right movement
if Input.is_action_pressed("ui_left"):
    velocity.x = -speed
elif Input.is_action_pressed("ui_right"):
    velocity.x = speed
else:
    velocity.x = 0

# Handle jumping
if Input.is_action_just_pressed("ui_up") and is_on_floor():
    velocity.y = jump_force

# Move the character
move_and_slide()

inspector

acoustic hazel
# shadow wharf this?

yes, you should usually not change the position of the root node of your scene. but this alone shouldn't cause an issue. where are your Sprite and Collision shape? i can't see them inside the viewport.

shadow wharf
#

are you looking for the inspector of the sprite and collision shape nodes?

acoustic hazel
#

no, i want to see where they are in this area, although having their positions and offset should also be enough.

shadow wharf
#

oh alr

acoustic hazel
#

the problem now is that i don't know where the positions of the ghost or the character are.

shadow wharf
#

are yo utalking about the littlecrossair thing

#

im new to godot sorry

acoustic hazel
#

yeah that

shadow wharf
#

if your talking about that then devin is the one at the top but to the right

#

while ghost is the orange one

acoustic hazel
#

well now we know the problem: the ghost doesn't care where the collision or sprite of the player is, it just cares about that position, the little cross, it tries to reach that, so of course it will never reach the player.

shadow wharf
#

oh

#

how can i change the position of the gross

#

cross

acoustic hazel
#

you first have to make the sprite and collision align with that cross, thats the main problem.

shadow wharf
#

okayu

#

okay