【问题标题】:How to draw a line of certain length and direction?如何绘制一定长度和方向的线?
【发布时间】:2023-03-31 22:05:01
【问题描述】:

我想创建一条特定长度和方向的线。 我的意思是从A点到B点画一条线。 通常在 Unity C# 中从 A 点到 B 点画一条线很简单。

Debug.DrawLine(point_A, point_B);

图表会是这样的

A----------------B

但我不想要这样的东西,我希望它从 A 点画到 B 点,但有一定的长度。所以我想我们需要这个工作的方向。但我不知道如何编码。

这是我想要的基本图表。

A--------        B

提前致谢。

【问题讨论】:

  • 你在哪个年级/年级(数学很简单,但根据你的水平需要提供不同的解释 - point_A, point_A + (point_B - point_A) * desiredLength /abs(point_B - point_A)

标签: c# vector unity3d line direction


【解决方案1】:

首先,将Debug.DrawLine() 用于调试不是一个好主意。您应该使用 LineRenderer 组件。

第二:你必须创建一个向量,它是从 point_A 到 point_B 的方向,你可以这样做:

Vector3 difference = point_B - point_A;

然后得到线的终点:

Vector3 endPoint = point_A + (difference.normalized * length);

那就做吧

GetComponent<LineRenderer>().SetPosition(0, point_A);
GetComponent<LineRenderer>().SetPosition(1, endPoint);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2014-01-24
    • 2013-08-13
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多