SuzLy
SuzLy

Reputation: 133

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? enter image description here

Upvotes: 2

Views: 139

Answers (3)

Codor
Codor

Reputation: 17605

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].

Upvotes: 0

Nika Kasradze
Nika Kasradze

Reputation: 3019

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()

Upvotes: 2

izeed
izeed

Reputation: 1731

You can accomplish it by using Vector3.MoveTowards http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Upvotes: 0

Related Questions