Hi,
So I’ve been trying to get this to work for some time now – basically what I’m trying to do is get a predetermined amount of points along a circular path and draw gizmos on them.
Now this is the code I’m using to simply create the circle path on a XZ plane.
I tried messing around with transform.forward and transform.right vectors to use the gameobject’s rotation as a guide for the Sin and Cos functions but it resulted in an odd oval shape with uneven spacing.
Any ideas how I could align the drawn cubes to the rotation of the gameobject?
But that only rotates the gameobject? The points aren’t transforms, they’re Vector3’s.
I feel like this gif could explain my issue better:
What the code does now is that it places the cubes on the circle evenly, but the cubes don’t follow the rotation of the gameobject, which is the problem I’m currently facing.
So, I need to modify my code, but I have no idea how.
Okay, I think I got it. I swapped some of the transform.rotation and transform.position references with their local equivalents, and now the cubes are rotating nicely.
You’ll want the cubes to be parented by the object, and set the local position instead of the world position. Right now you say “I want this object to be 5,4 from the center of the world”, that i world position. What you want to say is “i want this object to be 5,4 from the center of my parent”, that is local space. Local is relative to his parent, until there is none. Then its relative to the world again.
The cubes are currently just Gizmos, not real objects, so they don’t follow the gameobject’s rotation. I guess I should’ve stated that in my post, sorry. I’ll edit my OP.
I got it working without having to do anything with Gizmos, I simply made some changes to the Vector3’s that the Gizmos are using as their positions. It’s working well now, but I’ll keep your suggestion in mind, thanks!
Sure. I can’t post my full code, but this part has all the things that affected the behaviour of the Gizmos. Really the only way the code differs from what it was is using local rotations and positions instead of global.
AngleAxis is a static function which generates a Quaternion… you can use that quaternion as you like.
that’s fairly irrelevant to the issue, the vectors are going to be applied to the objects you want at those points, so why have a vector3 list/array and not just have a transform array?
using UnityEngine;
using System.Collections.Generic;
public class Test : MonoBehaviour
{
public Transform markerPrefab;
[Range(1, 100)]
public int pointCount;
public float radius;
public List<Transform> markers;
public void Start()
{
markers = new List<Transform>();
InitMarkers();
}
public void Update()
{
if (pointCount != markers.Count)
{
InitMarkers();
}
////// the core bit ///////
Quaternion quaternion = Quaternion.AngleAxis(360f / (float)(pointCount - 1), transform.up);
Vector3 vec3 = transform.forward * radius;
for (int index = 0; index < pointCount; ++index)
{
markers[index].position = transform.position + vec3;
// update for the next one
vec3 = quaternion * vec3;
}
////// end of the core bit ///////
}
private void InitMarkers()
{
if (pointCount > markers.Count)
{
for (int i = markers.Count; i < pointCount; i++)
{
// doesn't matter, we're updating the positions later
markers.Add(Instantiate(markerPrefab, Vector3.zero, Quaternion.identity) as Transform);
}
}
if (pointCount < markers.Count)
{
while (pointCount < markers.Count)
{
Destroy(markers[0].gameObject);
markers.RemoveAt(0); // dont miss this line out!!
}
}
}
}
I modified my OP a while ago – my goal was indeed to distribute points along the circle, and I managed to get it work. I know I could’ve been clearer in my first post, but at the time I felt like the info I left out wouldn’t have meant a thing.
Thanks for the info on Quaternions, They’re something I’ve rarely had to use so I don’t know much about them.
I might come back to this thread in case I do some changes to the script or if it breaks, but for now I’ve got it sorted.