Movement issues

Godot Version

Replace this line with your Godot version

Question

I’m working on a 3D boxing game and I’m using this code to get the boxer to move around

if (canMove):
		# Left Analog Stick
		var left_horz = Input.get_action_strength("left_stick_right") - Input.get_action_strength("left_stick_left")
		var left_vert = Input.get_action_strength("left_stick_up") - Input.get_action_strength("left_stick_down")
		
		
		
		var velocity = Vector2()
		left_horz = velocity.x += moveDir * movementSpeed
		left_vert = velocity.y += moveDir * movementSpeed
		velocity = move_and_slide(velocity)


But i got this error

Assignment is not allowed inside an expression

Your error is most likely here:

left_horz = velocity.x += moveDir * movementSpeed
left_vert = velocity.y += moveDir * movementSpeed

You are using += instead of +.

left_horz = velocity.x += moveDir * movementSpeed
is practically the same as:
left_horz = velocity.x = velocity.x + moveDir * movementSpeed

It assigns velocity.x and velocity.y inside of an expression, which you cannot do

1 Like

So, set velocity.x and y, after set your horizontal and vertical variable to their respective velocity.

Yes, that is most likely the correct way to do it.

Try using Input.get_vector for planar movement. This handles mapping four actions to each direction on each axis.

If your script extends a CharacterBody3D then velocity already exists, and you are improperly shadowing it. move_and_slide takes no arguments.

if canMove:
	var input_direction = Input.get_vector("left_stick_left", "left_stick_right", "left_stick_down", "left_stick_up")
	velocity = input_direction * movementSpeed

	move_and_slide()

The error you recieve is because += is an assignment, and so is left_horz = , you cannot use the two in tandem.