Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0019 Cannot multiply Vector2 and double

Tags:

c#

godot

I am using C# and Godot.

I have been able to multiple Vector2 and float but the movement of the character does not work when delta is a float.

direction is a Vector2 and both speed and delta are double.

Here is my code:

Velocity = direction.Normalized() * speed * delta;

The line of code results in CS0019 error stating that operator '*' cannot be applied to operands of type 'Vector2' and 'double'.

I am unable to change speed and delta into a float because if delta is a float, _PhysicsProcess cannot be overridden and does not move the character:

public override void _PhysicsProcess(double delta)
like image 293
iAmMe Avatar asked Apr 16 '26 04:04

iAmMe


1 Answers

Documentation does not list Vector2 multiplication operator accepting double. Try using floats (via casting):

Velocity = direction.Normalized() * (float)(speed * delta);

Or just declare speed and delta as floats from the start.

Note that his cast is not lossless (i.e. not every double can be correctly converted to float).

See also:

  • Floating-point numeric types (C# reference)
  • Operator overloading
like image 155
Guru Stron Avatar answered Apr 19 '26 03:04

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!