Duplicate of this post. Please avoid making duplicate posts as it is against forum rules.
As for your question, are the positions and platforms are both children of the same parent? If not, you may want to use global_position
rather than position
in your comparisons.
Another thing that could be happening is that the if
statements are all happening one right after the other, as the conditions check the position and whether the action is pressed, but they do not account for if the player just moved. I would suggest reformatting your code so there is a single condition for checking each input and then compare the positions. Something like this (untested, only showing right input):
if Input.is_action_just_pressed(“right”):
if position.y == 60:
velocity = position.direction_to(P2) * speed
velocity = move_and_collide(velocity)
elif position.y == 140:
velocity = position.direction_to(P3) * speed
velocity = move_and_collide(velocity)
elif position.y == 220:
velocity = position.direction_to(P4) * speed
velocity = move_and_collide(velocity)
else:
print (“Unknown position, cannot move to platform!”)
That may fix the issue if the issue is that the conditions are all triggering each other in a chain.