How do i create objects in between two points with an offset???

Discussion in 'Scripting' started by alexander11, Sep 6, 2016.

1. alexander11

Joined:
Aug 11, 2014
Posts:
94
Hello i am wondering on how to do this

in Pic1 SEG3, for example i know how to do whats in SEG1/SEG2 with this `
Code (CSharp):
1. Vector3 position = controlPoints[i].position + j *(controlPoints[i + 1].position - controlPoints[i].position) / segCount;
`(*For more about the code visit here its an old question i asked about dividing between two points*), but i want to know how to do this in SEG3(which we'll come around in second), as you can see in SEG2 when i move it the subPoints will always stay in the center, but i want to know when i move it like in SEG3 that sub points stay specific distance(int distanceCP) away from the controlPoints, would anyone know how to do this??

- -Pic1

_____________________________________________________________

Here is the code that does SEG1/SEG2.
Code (CSharp):
1.  public class Points : MonoBehaviour
2.   {
3.       public Transform[] points;
4.       public GameObject GameObj;
5.       public float GameObjectAmount = 2;
6.       void Start()
7.       {
8.           duplicateObject(GameObj, (int) GameObjectAmount);
9.       }
10.       public void duplicateObject(GameObject original, int howmany)
11.       {
12.           howmany++;
13.           for (int i = 0; i < points.Length-1; i++)
14.           {
15.               for (int j = 1; j < howmany; j++)
16.               {
17.                   Vector3 position = points[i].position + j * (points[i + 1].position - points[i].position) / howmany;
18.                   Instantiate(original, position, Quaternion.identity);
19.               }
20.           }
21.       }
22.       void OnDrawGizmos()
23.       {
24.       for (int i = 0; i < points.Length - 1; i++)
25.       {
26.           Gizmos.DrawLine(points[i].position, points[i + 1].position);
27.       }
28. }
29.   }
30.

2. takatok

Joined:
Aug 18, 2016
Posts:
1,496
Code (CSharp):
1. public void duplicateObject(GameObject original, int howmany, float distance)
2.     {
3.         howmany++;
4.         for (int i = 0; i < points.Length - 1; i++)
5.         {
6.             // make sure we can fit howmany objects in our line if they are "distance" apart
7.             float xDiff = points[i + 1].position.x - points[i].position.x;
8.             float yDiff = points[i + 1].position.y - points[i].position.y;
9.             float zDiff = points[i + 1].position.z - points[i].position.z;
10.
11.             float segDistance = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
12.
13.             // if the line is too short I just shorten distance
14.             // you can return an error if the distance can't be shortened
15.             if (segDistance < (float)howmany * distance)
16.                 distance = segDistance / (float)howmany;
17.
18.             // find out what percentage of this line distance covers
19.             float distanceNormal = distance / segDistance;
20.
21.             // Now get convert distanceNormal into a Vector3
22.             Vector3 oneDistance = new Vector3();
23.             oneDistance.x = xDiff / distanceNormal;
24.             oneDistance.y = yDiff / distanceNormal;
25.             oneDistance.z = zDiff / distanceNormal;
26.
27.             // Every other object should be next to start Cube
28.             for (int j = 1; j < howmany; j+=2)
29.             {
30.                 Vector3 position = points[i].position + ((j + 1) / 2) * oneDistance;
31.                 Instantiate(original, position, Quaternion.identity);
32.             }
33.
34.             // Now do the other half next to the end Cube
35.             for (int j=2;j< howmany;j+=2)
36.             {
37.                 Vector3 position = points[i+1].position - (j  / 2) * oneDistance;
38.                 Instantiate(original, position, Quaternion.identity);
39.             }
40.         }
41.     }

3. alexander11

Joined:
Aug 11, 2014
Posts:
94
@takatok it's not working i'm getting this in Pic1, but i am trying to do this in Pic2.

-Pic1

______________________________________________________________________________________
-Pic2

______________________________________________________________________________________
And here is the code that i implemented into my script(i may have implemented it wrong i dont know).
Code (CSharp):
1. using UnityEngine;
2. using System.Collections;
3.
4. public class Point : MonoBehaviour {
5.
6.     public Transform[] points;
7.     public GameObject GameObj;
8.     public float GameObjectAmount = 1;
9.     public float distance = 1;
10.
11.     void Start()
12.     {
13.         duplicateObject(GameObj, (int)GameObjectAmount, distance);
14.     }
15.     public void duplicateObject(GameObject original, int howmany, float distance)
16.     {
17.         howmany++;
18.         for (int i = 0; i < points.Length - 1; i++)
19.         {
20.             // make sure we can fit howmany objects in our line if they are "distance" apart
21.             float xDiff = points[i + 1].position.x - points[i].position.x;
22.             float yDiff = points[i + 1].position.y - points[i].position.y;
23.             float zDiff = points[i + 1].position.z - points[i].position.z;
24.
25.             float segDistance = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
26.
27.             // if the line is too short I just shorten distance
28.             // you can return an error if the distance can't be shortened
29.             if (segDistance < (float)howmany * distance)
30.                 distance = segDistance / (float)howmany;
31.
32.             // find out what percentage of this line distance covers
33.             float distanceNormal = distance / segDistance;
34.
35.             // Now get convert distanceNormal into a Vector3
36.             Vector3 oneDistance = new Vector3();
37.             oneDistance.x = xDiff / distanceNormal;
38.             oneDistance.y = yDiff / distanceNormal;
39.             oneDistance.z = zDiff / distanceNormal;
40.
41.             // Every other object should be next to start Cube
42.             for (int j = 1; j < howmany; j += 2)
43.             {
44.                 Vector3 position = points[i].position + ((j + 1) / 2) * oneDistance;
45.                 Instantiate(original, position, Quaternion.identity);
46.             }
47.
48.             // Now do the other half next to the end Cube
49.             for (int j = 2; j < howmany; j += 2)
50.             {
51.                 Vector3 position = points[i + 1].position - (j / 2) * oneDistance;
52.                 Instantiate(original, position, Quaternion.identity);
53.             }
54.         }
55.     }
56.     void OnDrawGizmos()
57.     {
58.         for (int i = 0; i < points.Length - 1; i++)
59.         {
60.             Gizmos.DrawLine(points[i].position, points[i + 1].position);
61.         }
62.     }
63. }
64.

4. takatok

Joined:
Aug 18, 2016
Posts:
1,496
Sorry really silly mistake
Change:
Code (CSharp):
1.  oneDistance.x = xDiff / distanceNormal;
2.             oneDistance.y = yDiff / distanceNormal;
3.             oneDistance.z = zDiff / distanceNormal;
Into:
Code (CSharp):
1.  oneDistance.x = xDiff *distanceNormal;
2.             oneDistance.y = yDiff * distanceNormal;
3.             oneDistance.z = zDiff * distanceNormal;

Joined:
Aug 11, 2014
Posts:
94
Thanks

6. takatok

Joined:
Aug 18, 2016
Posts:
1,496
Here's a minor change that makes it so if there is an odd number of balls it puts the last ball right in the middle. So it doesn't look lopsided. If you have 5 spheres.. currently it puts 3 next to one and 2 next to the other.. this change would put 2 next to each and 1 in the middle of the line:
Code (CSharp):
1.     public void duplicateObject(GameObject original, int howmany, float distance)
2.     {
3.         howmany++;
4.         int maxSpheres = ((howmany % 2)==0) ? howmany - 1 : howmany;
5.         for (int i = 0; i <points.Length-1; i++)
6.         {
7.             // make sure we can fit howmany objects in our line if they are "distance" apart
8.             float xDiff = points[i + 1].position.x - points[i].position.x;
9.             float yDiff = points[i + 1].position.y - points[i].position.y;
10.             float zDiff = points[i + 1].position.z - points[i].position.z;
11.
12.             Debug.Log("xDiff = " + xDiff + ", YDiff = " + yDiff + ", ZDiff = " + zDiff);
13.
14.             float segDistance = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
15.             Debug.Log("SegDistance: " + segDistance);
16.
17.             // if the line is too short I just shorten distance
18.             // you can return an error if the distance can't be shortened
19.             if (segDistance < (float)howmany * distance)
20.                 distance = segDistance / (float)howmany;
21.
22.             // find out what percentage of this line distance covers
23.             float distanceNormal = distance / segDistance;
24.             Debug.Log("distanceNormal: " + distanceNormal);
25.
26.             // Now get convert distanceNormal into a Vector3
27.             Vector3 oneDistance = new Vector3();
28.             oneDistance.x = xDiff * distanceNormal;
29.             oneDistance.y = yDiff * distanceNormal;
30.             oneDistance.z = zDiff * distanceNormal;
31.
32.             // Every other object should be next to start Cube
33.             for (int j = 1; j < maxSpheres; j += 2)
34.             {
35.                 Vector3 position = points[i].position + ((j + 1) / 2) * oneDistance;
36.                 Instantiate(original, position, Quaternion.identity);
37.             }
38.
39.             // Now do the other half next to the end Cube
40.             for (int j = 2; j < maxSpheres; j += 2)
41.             {
42.                 Vector3 position = points[i + 1].position - (j / 2) * oneDistance;
43.                 Instantiate(original, position, Quaternion.identity);
44.             }
45.             // Draw the last odd one in the exact middle
46.             if (howmany > maxSpheres)
47.             {
48.                 Vector3 position = new Vector3();
49.                 position.x = xDiff / 2.0f;
50.                 position.y = yDiff / 2.0f;
51.                 position.z = zDiff / 2.0f;
52.
53.                 Instantiate(original, points[i].position + position, Quaternion.identity);
54.             }
55.         }
56.     }

7. alexander11

Joined:
Aug 11, 2014
Posts:
94
Thanks again.

Joined:
Aug 11, 2014
Posts:
94
unityunity