Unknown error..please help

Godot Version

4.4.1

Question

Can anyone help me find the error?

Velocity is of the type Vector2, and you are trying to assign a float to it, which is incorrect.

1 Like

But i used the same formula (velocity = direction *delta* speed) for player and another enemy. And it worked fine

It’s not a formula, it’s an expression. The value and type of the expression will depend on the value and type of its operands. Those operands can have different values and types in different parts of code, even if they have the same names.

If direction is Vector2 then the result will be Vector2 as well and it can be properly assigned to velocity which is Vector2 as well.

However, if direction is an integer, which is case in your above code, the result will be a single integer as well and an integer cannot be assigned to Vector2.

you can use

var direction = Vector2(0,0)# instead of var direction = 0

maybe

1 Like

That seems to be the problem…Thanks! But wait… isn’t ‘delta’ always a float? wont multiplying velocity with delta cause a problem?

Thanks!

1 Like

No, because multiplication of vectors with scalars (single values) is mathematically defined, whereas assigning a scalar to a vector is not.

If multiplication of vectors and scalars didn’t work then the code you marked as the solution wouldn’t work either.

1 Like

I don’t think it’s defined as “the distributive property” in this case, but you can think of it as the distributive property from math.

A • (x,y) = (A • x , A • y)

1 Like

thank you!

Thank you!

There is a nice constant you can use in these cases instead

var direction = Vector2.ZERO
2 Likes