- Home /
The question is answered, right answer was accepted
Rotate Triangles "Correctly" Around The Circle
Hello,
I have a circle and this circle has orbits around it. The orbits' shape is triangle.

My orbit creation code:
public void CreateOrbitsAroundCircle(int num, Vector3 point, float radius)
{
for (int i = 0; i < num; i++)
{
/* Distance around the circle */
var radians = 2 * Mathf.PI / num * i;
/* Get the vector direction */
var vertical = Mathf.Sin(radians);
var horizontal = Mathf.Cos(radians);
var spawnDir = new Vector3(horizontal, vertical, 0);
/* Get the spawn position */
var spawnPos = point + spawnDir * radius; // Radius is just the distance away from the point
/* Now spawn */
var newOrbit = Instantiate(orbit, spawnPos, Quaternion.identity, this.transform);
//newOrbit.name = playerName;
/* Rotate the enemy to face towards player */
//enemy.transform.LookAt(point);
/* Adjust height */
//enemy.transform.Translate(new Vector3(0, enemy.transform.localScale.y / 2, 0));
}
}
I can add orbits by this code up to 10 orbits.
My question is this: I want to rotate triangles like my circle has spikes! Like this: 
How can I do that?
Answer by ugurgulser · Sep 18, 2020 at 06:02 PM
I just added this code to orbits. It worked well!
target = transform.parent.gameObject;
Vector3 dir = target.transform.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle + 90, Vector3.forward);
Answer by unity_ek98vnTRplGj8Q · Sep 18, 2020 at 03:30 PM
First make sure your triangle prefab is pointing up. This will make it easy to calculate the rotation to apply to the triangle if we already know the rotation of the prefab before we spawn it. Then you just need to calculate the angle to rotate your triangle by depending on where on the circle you are going to place it. You can do that by calculating the angle between the up direction and the direction of your spawn position.
var spawnPos = point + spawnDir * radius; // Radius is just the distance away from the point
float rotAngle = Vector3.Angle(Vector3.Up, spawnDir);
if(spawnDir.x < 0) rotAngle *= -1;
Quaternion orbitRotation = Quaternion.Euler(0, 0, rotAngle);
/* Now spawn */
var newOrbit = Instantiate (orbit, spawnPos, orbitRotation, this.transform);
Thanks for your reply.
This code works well with one,two,three and six orbits. When it comes four orbits or more. It gets weird. Can you help me?

Follow this Question
Related Questions
Advance Unity 2d apply circle Rotation-z with mouse 1 Answer
How do I make a rotatable procedural circle in 3d space? 1 Answer
Rotating guiTexture around a point in circle 0 Answers
Camera rotation around player while following. 6 Answers
LookAt once 0 Answers