Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | JayH |
Hi,
I’m having trouble with some logic, it should be simple, but I just can’t seem to figure it out!
I’m making a 2D Platformer with ladders.
Collisions/Signals all working
Description
My player should be able to run around his level with the wind in his hair, running past ladders without a care in the world.
It is only when the UP key is pressed and when he is overlapping a ladder that he can no longer freely move left and right or jump.
He is then able to use a new set of controls, which centre him on the ladder and then allow him to move up and down the ladder.
Note: I haven’t got to the bit where I remove the Player from the ladder at the bottom or top, this current step is important before obviously.
So to keep things simple, I put in a conditional statement for the main left/right/jump/gravity/on_ground mechanics:
if !player_on_ladder && !Input.is_action_just_pressed("ui_up")
#Let the player move freely
Then in the moving the Player up and down the ladder I have a conditional statement:
if player_on_Ladder && Input.is_action_just_pressed("ui_up")
#Let the player move up and down the ladder
This all seems ‘logical’ as they appear to be opposites to each other, however I’m missing something because it doesn’t work. One thing appears to be cancelling out the other.
Can you spot a flaw in my logic? Or what logic would you suggest?
What is the thing that happens instead? It seems to me that the second check will only be true
for one frame because is_action_just_pressed
only fires once.
SanderVanhove | 2022-07-15 15:00
We probably need some more code context here (just more code in general). It’s important to understand where the bits of code you showed live in your script and how they relate to each other.
jgodfrey | 2022-07-15 13:52
Also, something of note… The is_action_just_pressed()
will only fire in the single frame where the specified input is pressed. It does not fire continually while the input (button) is held down…
jgodfrey | 2022-07-15 13:54
I agree. It may be your handling of the player_on_ladder
flag and the 1-frame input instead of holding the input (e.g., are you using a ray cast, using the player’s position, collision signals, etc. to set that flag?)
godot_dev_ | 2022-07-15 15:07
One more comment. Not sure if you typed the code into the forum or cut/pasted it from your actual project but, you show 2 variations of this variable:
player_on_ladder
player_on_Ladder
(notice the capital L
in the second one). I assume that’s unintentional…
jgodfrey | 2022-07-15 15:32
Hi,
I was going to copy the code here, but I can never get it to display more than a handful of lines, so instead here’s a screenshot of the script, it shouldn’t look too complicated to understand - I hope
(You can zoom the screenshot when you open the image seperately)
The Part for moving the Player on the Ladder has had a few alterations, but it’s pretty much similar to what I posted above.
The main problem so far is that when the player reaches the ladder he stops dead. Additionally he doesn’t move upwards.
I’ve done a lot of debugging and altering the code, but I seem to always end up back in the same situation. I’ve had the player go up and down the ladder with variations, but other things often break.
I realise the code isn’t as compact as it could be, I could use some functions and pair parts up, I just need it to be readable to make it easier to understand.
JayH | 2022-07-15 16:11
FYI - to paste formatted code into the forum…
- Copy the code to the clipboard
- Paste the code into your forum message
- Select the code block in the forum message
- Press the
{ }
button in the forum editor toolbar - Look at the Preview to verify it’s formatted as expected
jgodfrey | 2022-07-15 17:12