Trying to orient a circle of objects relative to a ray?

I have this code to generate a prefab and then ring it with a set number of other prefabs in a nice circle:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MakeRing : MonoBehaviour {
    
    public Transform ASNode;
    public static Vector3 originPoint = new Vector3(0,0,0);

    void Start () {

        Vector3 centerPoint = new Vector3(20.0f, 20.0f, 20.0f);
        
        PlotCircle(5, centerPoint);
    }
 
    void PlotCircle(int count, Vector3 centerPoint) {
    	
    	float step = Mathf.PI * 2 / count;
    	float radius = count / 3;
    	float offset = 2.0f;
    	
    	Transform centerNodePointer = (Transform)Instantiate(ASNode, centerPoint, Quaternion.identity);
    	
    	Vector3 currentPoint = new Vector3();
    	
    	Ray baseRay = new Ray(originPoint, centerPoint);
    	
    	for (int i = 0; i < count; i++)
    	{
    		float angle = step * i;
    		
    		currentPoint.x = centerPoint.x + Mathf.Cos(angle) * radius;
    		currentPoint.y = centerPoint.y + offset;
    		currentPoint.z = centerPoint.z + Mathf.Sin(angle) * radius;
    		
    		Ray subRay = new Ray(centerPoint, currentPoint);
    		    		
    		Transform subNodePointer = (Transform)Instantiate(ASNode, currentPoint, Quaternion.identity);
    	}
    }

This makes a really nice ring of prefabs oriented about the vertical (Y) axis. What I’d like to do is orient the whole ring so the imaginary “vertical” axis through the ring is aligned with a ray going from the origin to the center object. I created Rays for the origin-to-center, and for the center-to-ring-object’s-current-position, but my brain is going numb as I try to puzzle out the proper transform to apply to the ring objects.

Can some kind soul please point me in the right direction (if you’ll pardon the term)?

Many thanks in advance!

Looking at this, I am a bit perplexed at why you are trying to draw a ring facing world zero, but I am sure you are going to change that.

Hint: You are creating an object at the core, but not orientating that object. make it LookAt() the origin point, then all of your sub object simply parent themselves to it and set themselves in local space.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MakeRing : MonoBehaviour {
    
    public Transform ASNode;
    public static Vector3 originPoint = new Vector3(0,0,0);

    void Start () {

        Vector3 centerPoint = new Vector3(20.0f, 20.0f, 20.0f);
        
        PlotCircle(5, centerPoint);
    }
 
    void PlotCircle(int count, Vector3 centerPoint) {
    	
    	float step = Mathf.PI * 2 / count;
    	float radius = count / 3;
    	float offset = 2.0f;
    	
    	Transform centerNodePointer = (Transform)Instantiate(ASNode, centerPoint, Quaternion.identity);
		centerNodePointer.LookAt(centerNodePointer.position + centerPoint);
    	
    	Vector3 currentPoint = new Vector3();
    	
    	Ray baseRay = new Ray(originPoint, centerPoint);
    	
    	for (int i = 0; i < count; i++)
    	{
    		float angle = step * i;
    		
    		currentPoint.x = Mathf.Cos(angle) * radius;
    		currentPoint.y = Mathf.Sin(angle) * radius;
		currentPoint.z = offset;
    		    		
    		Transform subNodePointer = (Transform)Instantiate(ASNode, Vector3.zero, centerNodePointer.rotation);
			subNodePointer.parent=centerNodePointer;
			subNodePointer.localPosition=currentPoint;
    	}
    }
}

I removed some stuff you didnt need, and added the orientation and localPosition you needed.

Wow. Okay, I need to dig a lot more into those functions. Thank you so much, you’ve helped me a ton!

Here’s another n00b question. In this bit:

centerNodePointer.LookAt(centerNodePointer.position + centerPoint);

I would have thought the CenterNodePointer.position and centerPoint would be the same value, since that’s where the Transform was instantiated at? What is the purpose behind adding them together?

You are correct, sorry.

How are you orientating the object? In order to get a ring around it, you would need to face the object in a direction. :wink:

As it sits, you simply state that it is at Vector3(20,20,20) and it’s orientation is a Quaternion.identity. The code I gave you sets it according to a rotation.

I wasn’t orienting it, at first. I just had it going around the x/z axis to test that I had the placement stuff working. After that is when I wanted to worry about orienting it. Your code actually works perfectly, now wherever I place the center node the ring appears oriented just as I wanted it.

The (20,20,20) was just a test placement, a number I made up.

Thanks again!