Unexpected Token

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By imgonna

Okay so this is my script so far:
`extends KinematicBody2D

var velocity = Vector2.ZERO

func _physics_process(delta):
if Input.is_action_pressed(“ui_right”):
velocity.x = 4
elif Input.is_action_pressed(“ui_left”):
velocity.x = -4
velocity.x = 0

for some reason, when I play the game it won’t work and ends up as a black screen.
Please help me. Oh and also, ignore the italic ones. Those are just the underscores.

Please post the full debugger message.

timothybrentwood | 2021-11-03 01:43

:bust_in_silhouette: Reply From: PhantomPixel

First of all, the correct method to move a charecter should use -= or += depending on the case,

if Input.is_action_pressed("right"):
        velocity.x += 1
if Input.is_action_pressed("left"):
        velocity.x -= 1
if Input.is_action_pressed("down"):
        velocity.y += 1
if Input.is_action_pressed("up"):
        velocity.y -= 1

Note; I used the input map for simplicity, in you case the input for right would be ui_right
Maybe that is the case?
If not, respond again to this thread or:
Godot Docs

:bust_in_silhouette: Reply From: DaddyMonster

The code should be indented like this:

extends KinematicBody2D

var velocity = Vector2.ZERO

func _physics_process(_delta):
    if Input.is_action_pressed("ui_right"):
        velocity.x += 4
    elif Input.is_action_pressed("ui_left"):
        velocity.x -= 4
        velocity.x = 0


In gdscript everything aside from member variables (var velocity = Vector2.ZERO) must go in a method (function) which is whitespace defined (with a tab). Note, don’t mix tabs and spaces.

Have a read through this: https://docs.godotengine.org/en/3.4/getting_started/scripting/gdscript/gdscript_basics.html