0 votes

I'm using the Platformer3d example and trying to make it so that when the screen is swiped left or right, the player turns.

I've tried:

event.set_as_action("move_right", true))

and

event.action_press ("move_right")

but it's not moving the character.

in Engine by (28 points)
edited by

1 Answer

0 votes

You're using functions that doesn't even exist. Look into this. The way actions work is; you go to the Input Map under Project Settings and assign a button to a certain action, then you can test if such action is pressed or not. So, you would use:

if event.is_action_pressed("action"):

So, assuming you want to turn the player right when you press "E". You then go to the Project Settings, create a new action and assign the "E" key to it, let's call this action "move right". Then you just need to go to the script and check if that action is pressed and turn the camera:

if event.is_action_pressed("move right"):
   #turn player right
by (1,093 points)

I am trying to trigger the action: "move left" from inside the input function when SCREENDRAG's relativex hits a certain point (swipe).

func _input(ie):
if ie.type == InputEvent.SCREEN_TOUCH:
    if ie.pressed && !touch_dragging:
        touch_dragging = true
    if !ie.pressed && touch_dragging:
        touch_dragging = false
        Input.action_release("move_left")
        Input.action_release("move_right")

if ie.type == InputEvent.SCREEN_DRAG:
    if touch_dragging:
        if ie.relative_x < -10:
            Input.action_press("move_left")
        if ie.relative_x > 10:
            Input.action_press("move_right")

It half works on the first swipe, but release doesn't work and I get this error in the console:

C Error: Condition '!custom)action_press.has(p_action) ' is true
C source: main/input_default.cpp:454
C function: action_release

Basically you're trying to make the player turn using action_press() to indirectly change the action state, right? That's not recommended. Of course, you need to check somewhere if the "move_left" action is pressed, and then execute the code to turn. That said, you should do something like this:

func _input(event):
   if event.is_action_pressed("move_left"):
      turn(angle)
   elif event.is_action_pressed("move_right"):
      turn(angle)

   if event.type == InputEvent.SCREEN_DRAG:
      if touch_dragging:
         if event.relative_x < -10:
            turn(angle)
         if event.relative_x > 10:
            turn(angle)

func turn(angle):
   #turn
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.