I cant run the move and slide function because of error "Invalid write in function........"

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

extends KinematicBody2D

var velocity = Vector2()
export var jumpheight = 100
export var walkspeed = 150

func _get_input():
if Input.is_action_pressed(“left_move”):
velocity.x = -walkspeed
elif Input.is_action_pressed(“right_move”):
velocity.x = walkspeed
velocity.x = 0

func _physics_process(delta):
var motion = velocity.x*delta

I keep getting the error but the guy from the tutorial always runs it.Ther error says:
"Invalid write in function ‘move_and_collide’ in base ‘Kinematicbody2D’ Cannot convert argument 1from float to Vector2’

If you can solve this or tell me where the mistake is, It would be greatly appreciated!

You said move_and_slide in title btw

Annoying_Brother | 2020-12-11 18:52

Also is the move_and_collide() in the physics process func?

Annoying_Brother | 2020-12-11 18:53

:bust_in_silhouette: Reply From: jgodfrey

Your problem is here:

var motion = velocity.x*delta

The first argument to move_and_collide is documented to be a Vector2. In your code, you’re trying to pass afloat value instead (just as the error says), as your motion variable extracts the x component of the velocity vector.

Perhaps you meant:

var motion = velocity * delta

The result of that will be a Vector2, which will satisfy the move_and_collide call.