如何在 unity3D 中沿鼠标位置方向绘制一条长度为 x 的线



我的问题似乎很简单,但我自己也想不通。

我想从我的 transform.position 沿鼠标光标所在的方向绘制一条固定长度的线。

我想通了:

var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
lazer.setPosition(0, transform.position);
// here is where the failing starts. i need to calculate the end position.
lazer.setPosition(1, ?)

谢谢A。

我认为您正在寻找的是Vector2Vector3类上的变量normalized。 像这样的东西每次都会给你一个长度相同(实际上是大小)的新向量:

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 offsetPos = mousePos - transform.position;
Vector3 newVec = offsetPos.normalized * scale; // this is the important line
newVec += transform.position;

最新更新