My movement code is working for a few seconds and then my game stops

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

extends KinematicBody2D

var speed = 200
var velocity = Vector2()

func _ready():
pass

func _physics_process(delta):
velocity = Vector2()
if Input.is_action_pressed(“up”):
velocity -= speed
if Input.is_action_pressed(“down”):
velocity.y += speed
if Input.is_action_pressed(“left”):
velocity.x += speed
if Input.is_action_pressed(“right”):
velocity.x -= speed

move_and_slide(velocity)
look_at(get_local_mouse_position())

Please don’t post the same question multiple times. If you have additional information, just add it to the original post by 1) editing the post or 2) adding additional Comments.

I’d recommend that you Hide this copy of the question and continue in the original one. That way, any/all input from the community will be in the same place.

jgodfrey | 2023-02-03 23:08

is this the exact copy of your code? or did you rewrite it? because there’s some minor typos in your code

Edit: just saw that underscores are removed if not put in code blocks…

phteven | 2023-02-04 03:53

:bust_in_silhouette: Reply From: phteven

I’d assume the issue is because of this line:

if Input.is_action_pressed("up"):
    velocity -= speed

you can’t subtract a vector by a float or int
change it to this to fix the issue:

if Input.is_action_pressed("up"):
    velocity.y -= speed

you should have an error explaining what the issue is in the debugger.