英文:
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);
或者从一开始就声明speed
和delta
为float
。
请注意,此强制转换不是无损的(即,并非每个double
都可以正确转换为float
)。
另请参阅:
英文:
Documentation does not list Vector2
multiplication operator accepting double
. Try using float
s (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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论