Godot Version
4.4.1.stable
Question
Invalid operands ‘Vector2’ and ‘float’ in operator ‘+’
I am getting the error when I press the dash button
if Input.is_action_pressed(“dash”) and can_dash:
can_dash = false
dash_direction = direction.normalized()
velocity = dash_direction * dash_speed
velocity += 1.0 - (friction * delta) ← error on this line
$Dashcooldown.start()
The error is showing because you’re trying to add a float to velocity, which is a Vector2.
So instead of writing:
velocity += 1.0 - (friction * delta) ← error on this line
Try this:
velocity += dash_direction * (1.0 - (friction * delta)) # Fix: scale by direction to stay as Vector2
1 Like
Try just switching the + to a * ?
velocity *= 1.0 - (friction * delta)
it doesn’t crash anymore, but it doesn’t do anything, i have set up var dash_speed = speed * 2, but it doesn’t change when I press dash button
Did you used move_and_slide
function to move the player, after setting the velocity?
Not sure if I understand, I have move_and_slide after dash code
if Input.is_action_pressed("dash") and can_dash:
can_dash = false
dash_direction = direction.normalized()
velocity = dash_direction * dash_speed
velocity += dash_direction * (1.0 - (friction * delta))
$Dashcooldown.start()
move_and_slide()
and this is my movement code
func get_input():
var vertical = Input.get_axis("up", "down")
var horizontal = Input.get_axis("left", "right")
return Vector2(horizontal, vertical)
if direction.length() > 0:
velocity = velocity.lerp(direction.normalized() * speed, acceleration)
else:
velocity = velocity.lerp(Vector2.ZERO, friction)
1 Like
You had:
I’d suggest:
if Input.is_action_pressed("dash") && can_dash:
can_dash = false
dash_direction = direction.normalized()
var cur_vel = dash_speed + 1.0 - (friction * delta) # sum velocity here
velocity = dash_direction * cur_vel
$Dashcooldown.start()
That is, calculate the whole velocity term before scaling by it.
2 Likes
Hey, please can you paste your codes as preformatted text here?
Like this
``` I used these symbols for the code area, similar copy and paste your codes within this format ```
1 Like