So I’m trying to make a 2D platformer, been watching tutorials and I’m trying to make the players movement code, it is just a simple code. But one lines “Assignment is not allowed inside an expression.”
This is what the line is:
Well that particular error message is probably due to trying to pass floor_normal = Vector2() as an argument, but additionally if you’re using Godot 4 as it says in your post then move_and_slide doesn’t take any arguments and returns a bool (indicating whether or not there was a collision), so that line should just be move_and_slide() .
Well at the beginning of the code I made “var velocity = Vector2()”, and since I just made it move_and_slide(), now var velocity is the problem
this is the entire code:
velocity is already defined in the script’s base class CharacterBody2D – that’s why you don’t have to (and in fact: cannot) re-define it here! You should get an error as well. Just drop the line.
Also, as @soundgnome already mentioned above, move_and_slide returns a bool – so obviously you shouldn’t write that return value into your velocity variable of type Vector2! Just don’t.
extends CharacterBody2D
# Player properties
const GRAVITY = 800
const MAX_SPEED = 200
const ACCELERATION = 800
const FRICTION = 1000
const JUMP_FORCE = -400
var is_on_ground = false
func _physics_process(delta: float) -> void:
# Apply gravity
velocity.y += GRAVITY * delta
# Movement
var input_vector = Vector2()
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
if input_vector.x != 0:
velocity.x += input_vector.x * ACCELERATION * delta
velocity.x = clamp(velocity.x, -MAX_SPEED, MAX_SPEED)
else:
# Apply friction
if is_on_ground:
var friction_direction = -sign(velocity.x)
velocity.x += friction_direction * FRICTION * delta
if abs(velocity.x) < 20:
velocity.x = 0
# Jumping
if is_on_ground and Input.is_action_pressed("ui_up"):
velocity.y = JUMP_FORCE
is_on_ground = false
# Move the player
move_and_slide()
# Check if player is on the ground
is_on_ground = is_on_floor()