我有一个统一的项目,我正在做一个拖动指示器(下面)

有人能告诉我如何限制这个指示器的长度吗?
我用下面的代码创建它
if (Input.GetMouseButtonDown(0))
{
if (lr == null)
{
lr = gameObject.AddComponent<LineRenderer>();
}
lr.enabled = true;
lr.positionCount = 2;
startPos = camera.ScreenToWorldPoint(Input.mousePosition) + camOffset;
lr.SetPosition(0, startPos);
lr.useWorldSpace = true;
lr.widthCurve = ac;
lr.numCapVertices = 10;
lr.sortingLayerName = "Top";
lr.startColor = Color.white;
lr.endColor = Color.white;
}
if (Input.GetMouseButton(0))
{
endPos = camera.ScreenToWorldPoint(Input.mousePosition) + camOffset;
lr.SetPosition(1, endPos);
}
if (Input.GetMouseButtonUp(0))
{
lr.enabled = false;
attached = false;
}我试过像这样限制长度
Vector3 length = startPos - endPos;
if(length.magnitude <= 7) lr.SetPosition(1, endPos);但是当我超过7的长度后,它就没有endPos更新了。
知道如何在更新endPos的同时达到这个限制吗?
发布于 2020-10-27 06:59:15
Vector3 direction = endPos - startPos;
float length = direction.magnitude;
if(length > 7)
endPos = startPos + direction / length * 7;
lr.SetPosition(1, endPos);注意:我使用的是direction / length而不是direction.normalized,以避免昂贵的重复计算direction.magnitude
https://stackoverflow.com/questions/64549431
复制相似问题