Hello guys,

I wish I had listen more closely in school, I try to instantiate an object with a 90° angle on a circle, I can’t find the way to do this!

Thanks if you can help me!

TWB

Hello guys,

I wish I had listen more closely in school, I try to instantiate an object with a 90° angle on a circle, I can’t find the way to do this!

Thanks if you can help me!

TWB

I manage to Instantiate the prefabs at proper location, it’s the way to rotate them properly that I can’t figure how to deal with :(.

TWB

What do you mean by rotate them properly?

to make a circle around a point is something like this

*sudo*

```
Vector3 center = Vector3.zero;
float radius = 10.0f;
GameObject prefab;
int numObjects = 4;
bool lookIn; //look to center or out
float angle = (360 / 4) * Mathf.Degree2Rad;
for(int i = 0; i < 4; i++){
Vector3 pos = new Vector3( Mathf.Cos( angle * i), 0.0f, Mathf.Sin(angle* i));
Quaternion rot = Quaternion.LookRotation( (lookIn)? -pos : pos);
Instantiate(prefab, pos, rot);
}
```

**I wasn’t in class ever too, so my math is questionable at best, did this from memory in the browser so I might have done something wrong.

edit: didn’t take account of actual position in the rotation, but it works if the center is Vector3.zero

To take it into account follow what @lordofduct said

Thank you for your response,

Here is what I have done. What I can’t manage is to rotate properly the object, like a tree compared to the the core of a planet for example ^^.

TWB

```
for (var pointNum = 0; pointNum < numPoints; pointNum++)
{
double i = (pointNum * 1.0) / numPoints;
float angle = (float)(i * Mathf.PI * 2);
float x = Mathf.Sin(angle) * radiusX;
float y = Mathf.Cos(angle) * radiusY;
Vector3 pos = new Vector3(x, y, 0) + transform.position;
Instantiate(Obstacles[Random.Range(0, Obstacles.Length)], pos, Quaternion.identity, gameObject.transform);
}
```

The normal on a sphere or circle, if you have the point on the sphere/circle, is just that point - the center.

```
normal = (pos - center).normalized
```

Once you have a normal, if in 3-space you can just use a Quaternion.LookRotation using the normal as the up to get a 3-space rotation.

If in 2-space, the angle is just the Atan2 of that Vector2.

What I did in the rotation with the “lookIn” bool doesn’t do that?

(Assuming the tree extends in the Z axis.)

I tried that and it doen’t work.

I reallyt have to work my geomtery skills!..

```
for (var pointNum = 0; pointNum < numPoints; pointNum++)
{
double i = (pointNum * 1.0) / numPoints;
float angle = (float)(i * Mathf.PI * 2);
float x = Mathf.Sin(angle) * radiusX;
float y = Mathf.Cos(angle) * radiusY;
Vector3 pos = new Vector3(x, y) + transform.position;
Vector3 normal = (pos - transform.position).normalized;
Instantiate(Obstacles[Random.Range(0, Obstacles.Length)], pos, Quaternion.LookRotation(pos, normal), gameObject.transform);
}
```

Thanks!

I did’nt maange to make this code work, this must be me!

It’s because you did this:

```
Quaternion.LookRotation(pos, normal)
```

This is LookRotation:

The parameters are forward and up.

Not position and up.

To make it work with pos and vector3.zero as the center like I did you can say`pos.normalized`

Thanks a lot guys,

I’m close to what I wan’t, the only probleme is that my GameObjects are rotated on their y local axis the wrong way, how can i can I rotate them 90°?.

```
double i = (pointNum * 1.0) / numPoints;
float angle = (float)(i * Mathf.PI * 2);
float x = Mathf.Sin(angle) * radiusX;
float y = Mathf.Cos(angle) * radiusY;
Vector3 pos = new Vector3(x, y) + transform.position;
Vector3 normal = (pos - transform.position).normalized;
Vector3 tangent = Vector3.Cross(normal, new Vector3(pos.x, -pos.y, pos.z));
GameObject obstacle = Instantiate(Obstacles[Random.Range(0, Obstacles.Length)], pos, Quaternion.LookRotation(tangent, normal), gameObject.transform);
```