简体   繁体   中英

finding points along a vector in 3d

I have two points in 3d space, and i want to get a list of points between them which are located with "r" distance from each other. How can I do it most easily using the unity functions? 在此处输入图片说明

Vector3[] GetPointsInbetween(Vector3 a, Vector3 b, float offset){
    int count = (int)((b - a).magnitude / offset);
    Vector3[] result = new Vector3[count];

    Vector3 delta = (b - a).normalized * offset;

    for (int i = 0; i < count; i++) {
        result[i] = a + delta * i;
        Debug.Log(result[i]);
    }

    return result;
}

but .magnitude and .normalized are very expensive operations, try to avoid using this in Update()

您可以通过使用Vector3.MoveTowards http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html来完成它

I'm not familiar with the Unitiy functions, but formally you describe linear interpolation between the two points. The line segment between the points A and B can be described by the parameterized form

A * s + B * (1-s)

where s is from the interval [0,1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM