如何在具有偏移的两个点之间创建对象?

时间:2016-09-06 03:01:03

标签: c# unity3d

我想知道如何做到这一点 在Pic1 SEG3中。例如,我知道如何用这个Vector3 position = controlPoints[i].position + j *(controlPoints[i + 1].position - controlPoints[i].position) / segCount;在SEG1 / SEG2中做什么(有关代码访问的更多信息here这是一个老问题,我问过两个点之间的划分),

但是我想知道如何在SEG3中做到这一点(我们将在第二次出现)。正如你在SEG2中看到的那样,当我移动它时,subPoints将始终保持在中心,但我想知道如何在SEG3中移动它,以便子点保持特定距离(int distanceCP)远离controlPoints,任何人都会知道怎么做??

PIC1

以下是执行SEG1 / SEG2的代码。

public class Points : MonoBehaviour
  {
      public Transform[] points;
      public GameObject GameObj;
      public float GameObjectAmount = 2;
      void Start()
      {
          duplicateObject(GameObj, (int) GameObjectAmount);
      }
      public void duplicateObject(GameObject original, int howmany)
      {
          howmany++;
          for (int i = 0; i < points.Length-1; i++)
          {
              for (int j = 1; j < howmany; j++)
              {
                  Vector3 position = points[i].position + j * (points[i + 1].position - points[i].position) / howmany;
                  Instantiate(original, position, Quaternion.identity);
              }
          }
      }
  }

2 个答案:

答案 0 :(得分:1)

Vector3.distance为您提供从第一个控制点到第二个控制点的距离。 使用新距离(distaceCP)在第一个位置划分或制作矢量。在另一个控制点和反向矢量方向上相同。 将您的对象放在每个创建的向量的末尾,然后就可以了。

或者只需使用MoveToward将对象从A移动到B.

if(webview ! = null) { webview.destory(); webview == null;}

答案 1 :(得分:0)

这可以使用Unity的Ray API完成。您可以使用它来修改controlPoints

controlPoint1 = somePos;
controlPoint2 = someOtherPos;

然后使用Ray's GetPoint函数获取controlPoint1的偏移量。

var offsetDistance = 5;
Ray myRay= new Ray();
offsetRayFrom.origin = controlPoint1 ;
offsetRayFrom.direction = controlPoint2 - controlPoint1 ;
Vector3 newFromDisance = offsetRayFrom.GetPoint(offsetDistance);
controlPoint1 = newFromDisance; //Change 'From' Distance

现在,对controlPoint2位置做同样的事情,但是翻转原点和方向值,然后你现在可以在for循环中使用这两个新值(Vector3)。

我无法理解您的代码,所以我将我的个人位置修改为块位置功能以包含offset参数。评论说。您可以阅读评论,以了解幕后发生的事情。

它需要2个位置(controlPoint1controlPoint2),然后是Vector3数组来存储结果,然后是块数(howmany),bool来告诉如果要使用偏移或不使用,那么最后是距离偏移。 我没有让函数返回一个数组。我做了它来填充传递给result参数的数组。这对内存管理很有用。

void posToChunkDistances(Vector3 from, Vector3 to, Vector3[] result, int chunkAmount, bool useOffset = false, float offsetDistance = -1f)
{

    //Change from and to values if we want to use offset
    if (useOffset)
    {
        Debug.Log("Use Offset");

        //Un-Comment to Allow negative offset?
        //offsetDistance = Mathf.Abs(offsetDistance);

        //Find new 'From' Distance
        Ray offsetRayFrom = new Ray();
        offsetRayFrom.origin = from;
        offsetRayFrom.direction = to - from;
        Vector3 newFromDisance = offsetRayFrom.GetPoint(offsetDistance);
        from = newFromDisance; //Change 'From' Distance


        //Find new 'To' Distance
        Ray offsetRayTo = new Ray();
        offsetRayTo.origin = to;
        offsetRayTo.direction = from - to;
        Vector3 newToDisance = offsetRayTo.GetPoint(offsetDistance);
        to = newToDisance; //Change 'To' Distance
    }

    //divider must be between 0 and 1
    float divider = 1f / chunkAmount;
    float linear = 0f;

    if (chunkAmount == 0)
    {
        Debug.LogError("chunkAmount Distance must be > 0 instead of " + chunkAmount);
        return;
    }

    if (chunkAmount == 1)
    {
        result[0] = Vector3.Lerp(from, to, 0.5f); //Return half/middle point
        return;
    }

    for (int i = 0; i < chunkAmount; i++)
    {
        if (i == 0)
        {
            linear = divider / 2;
        }
        else
        {
            linear += divider; //Add the divider to it to get the next distance
        }
        // Debug.Log("Loop " + i + ", is " + linear);
        result[i] = Vector3.Lerp(from, to, linear);
    }
}

<强>用法

public GameObject testCubePrefab;
public Transform dst1;
public Transform dst2;

void Start()
{
    int myDistances = 2;
    Vector3[] distancesResult = new Vector3[myDistances];
    posToChunkDistances(dst1.position, dst2.position, distancesResult, myDistances, true, 5f);

    for (int i = 0; i < distancesResult.Length; i++)
    {
        Instantiate(testCubePrefab, distancesResult[i], Quaternion.identity);
    }
}