错误 CS0030:无法将类型 ‘UnityEngine.Vector2’ 转换为 ‘UnityEngine.Vector2Int’

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

error CS0030: Cannot convert type 'UnityEngine.Vector2' to 'UnityEngine.Vector2Int'

问题

我在我的洞穴生成脚本中遇到了这个问题,这是这行代码:

Vector2Int nextPoint = passage[passage.Count - 1] + (Vector2Int)(dir * connectionLength);

我该如何解决?

我以前从未遇到过这个错误,所以我不知道该如何解决,这可能是一个很容易解决的错误,但我刚刚开始使用Unity。我希望有人告诉我如何解决这个问题。

英文:

I have this problem in my cave generation script, this is the line

Vector2Int nextPoint = passage[passage.Count - 1] + (Vector2Int)(dir * connectionLength);

How I do solve it?

I never had this error so I dont know how to solve it, it will probably an error very easy to solve but I'm just beginning with unity. I'm expecting someone that says me how to solve this problem

答案1

得分: 1

你可以使用Vector2Int.FloorToInt(Vector2 t)来进行转换,我不认为有一个明确的从Vector2Vector2Int的转换定义。如果你更喜欢四舍五入而不是向下取整的值,你也可以使用Vector2Int.RoundToInt(Vector2 t)

你的代码将如下所示:

Vector2Int nextPoint = passage[passage.Count - 1] + Vector2Int.FloorToInt(dir * connectionLength);
英文:

You can use Vector2Int.FloorToInt(Vector2 t) to do the conversion, I don't believe there is an explicit conversion defined from Vector2 to Vector2Int. In case you prefer rounding instead of flooring the value, you can also use Vector2Int.RoundToInt(Vector2 t)


Your code would look like this:

Vector2Int nextPoint = passage[passage.Count - 1] + Vector2Int.FloorToInt(dir * connectionLength);

答案2

得分: 0

你之所以会得到错误是因为 Vector2Vector2Int 是两种不同的类型。 Vector2 可以存储浮点数值,而 Vector2Int 则不能。


因此,你需要将 dir 的 Vector2(看起来是这样)四舍五入为最接近的整数值。 为此,你可以使用 Vector2ToInt

总的来说,你的代码应该像这样:

Vector2 nextPoint = passage[passage.Count - 1] + (dir * connectionLength);
Vector2Int nextPointInt = new Vector2Int((int) nextPoint.x, (int) nextPoint.y);

假设值的类型是:

private Vector2 dir;
private int connectionLength;
private List<Vector2Int> passage;
 ----- 或者 -----
private List<Vector2> passage;
英文:

You are getting the error because Vector2 and Vector2Int are two different types. Vector2 can store float values while Vector2Int cannot.


Therefore you need to Round the dir Vector2 (as it seems) to the nearest integer value. For this, You can use Vector2ToInt.

Overall your code should look like this:

Vector2 nextPoint = passage[passage.Count - 1] + (dir * connectionLength);
Vector2Int nextPointInt = new Vector2Int((int) nextPoint.x, (int) nextPoint.y);

Assuming that the value types are:

private Vector2 dir;
private int connectionLength;
private List&lt;Vector2Int&gt; passage;
 ----- OR -----
private List&lt;Vector2&gt; passage;

答案3

得分: 0

使用这段代码:

# 将 Vector2 转换为 Vector2Int。
vector2IntValue = Vector2Int.FloorToInt(vector2Value);

你的最终代码:

Vector2Int lengthVal = Vector2Int.FloorToInt(dir * connectionLength);
Vector2Int nextPoint = passage[passage.Count - 1] + lengthVal;
英文:

Use this code :

 // Convert the Vector2 to Vector2Int.
 vector2IntValue = Vector2Int.FloorToInt(vector2Value);

Your Final Code :

 Vector2Int lengthVal = Vector2Int.FloorToInt(dir * connectionLength);
 Vector2Int nextPoint = passage[passage.Count - 1] + lengthVal;

答案4

得分: 0

我刚刚解决了,谢谢,但我有另一个问题,我的脚本不会创建地图,它创建了一个瓦片块。

英文:

I have just solve it thank you, but I have another problem my script doesn´t create a map it creates a block of tiles.

huangapple
  • 本文由 发表于 2023年7月20日 16:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727956.html
匿名

发表评论

匿名网友

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

确定