CS0019 无法将 Vector2 与 double 相乘

huangapple go评论69阅读模式
英文:

CS0019 Cannot multiply Vector2 and double

问题

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)

英文:

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)

答案1

得分: 4

文档未列出Vector2乘法运算符接受double。尝试使用float(通过强制转换):

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

或者从一开始就声明speeddeltafloat

请注意,此强制转换不是无损的(即,并非每个double都可以正确转换为float)。

另请参阅:

英文:

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:

huangapple
  • 本文由 发表于 2023年5月26日 01:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定