- Home /
Position waypoint along sphere
Hi everyone I need your help. I'm trying to create a waypoint system on a spherical world. That's what I got. The cubes are waypoints ranging from enemy to player direction. Now I have to make sure that the cubes are placed on the spherical surface. I'm trying to use breast and cosine but missing something to make it work. Here is the code with the drawing
Here I add the various waypoints
![public Transform t1, t2, node, planet;
public int division;
private List<Transform> nodes;
Vector3 direction, nodeDirection;
float dist, result;
void Start ()
{
nodes = new List<Transform>();
dist = Vector3.Distance(t1.position, t2.position);
direction = (t2.position - t1.position).normalized;
for (int i = 0; i < division; i++)
{
result = (dist / division) * i;
//
Transform t = Instantiate(node, new Vector3(GetPos(direction.x, result, t1.position.x),
GetPos(direction.y, result, t1.position.y),
GetPos(direction.z, result, t1.position.z)),
Quaternion.identity) as Transform;
nodes.Add(t);
}
float GetPos(float axis, float result, float player)
{
return (axis * result) + player;
}
}][1]
[1]: /storage/temp/101195-helpme.png
Answer by toddisarockstar · Sep 04, 2017 at 05:47 AM
should be simple as getting the distance from the center of your circle to the ouside which it looks like have in the top of your script.
then multiply the NORMALIZED directions for all your objects by the consistant distance. Then just add it to the circles center position for the new objects positions!! here is a generic working example to show the math steps:
GameObject g;
Vector3 center= Vector3.zero;
float dist = 5;
int i = 50;
while (i>0) {i--;
// get some random directions
Vector3 v = new Vector3(
Random.Range(-10f,10f),
Random.Range(-10f,10f),
0);
v=v.normalized;
v=v*dist;
g= GameObject.CreatePrimitive(PrimitiveType.Cube);
g.transform.position=v+center;
}
Your answer
Follow this Question
Related Questions
Finding Position based on a local perspective 1 Answer
Translate Vector3 by Vector3 1 Answer
2D Background Stop Moving on BoundingBox Exit? 2 Answers
Creating a bouncing game without using physics - Vector3 math problem, 2 Answers
Instantiate Gameobjects in the mid position across several targets without overlap 0 Answers