0 votes

I have written the exact code an online tutorial suggested but the player character only moves right when I press the assigned key but not left. here's the code:

extends KinematicBody2D

var motion = Vector2(0,0)
const speed = 400

func physicsprocess(delta):
if Input.isactionpressed("left") and not Input.isactionpressed("right"):
motion.x = -speed
if Input.isactionpressed("right") and not Input.isactionpressed("left"):
motion.x = speed
else:
motion.x = 0
moveandslide(motion)

Godot version 3.3.1
in Engine by (12 points)

1 Answer

+1 vote
if Input.isactionpressed("left") and not Input.isactionpressed("right"):
    motion.x = -speed
elif Input.isactionpressed("right") and not Input.isactionpressed("left"):
    motion.x = speed
else:
    motion.x = 0

Make the second if an elif.

What was happening when you pressed left motion.x was set to -speed. Then it evaluated Input.isactionpressed("right") and not Input.isactionpressed("left") which turned out to be false and so the else condition fired setting motion.x to 0.

by (3,545 points)
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.