Having issues with Quaternion.LookRotation(...)

Hey community.

My game is a 2D perspective along the global Z axis so Y is up and X is left and right. Right now I am trying to instantiate a set of quads in a circle. I essentialy want it so the quads rotate along their Z axis to point their Y axis at the center of the circle.
Here are a couple of images of my issue.

Red line is X axis, Green line is Y axis.

This first image is my current state.

And this image is what I want to achieve.

And finally here is my code I am using to create this.

for (int pointNum = 0; pointNum < numPoints; pointNum++) {
			
         float i = (pointNum * 1.0f) / numPoints;
         float angle = i * Mathf.PI * 2.0f;
         float x = Mathf.Sin(angle) * radius;
	 float y = Mathf.Cos(angle) * radius;
			
	 Vector3 position = transform.position + new Vector3(x, y, 0);
	 Vector3 direction = (transform.position - position).normalized;
	 Quaternion rotation = Quaternion.LookRotation(direction);
	 rotation.x = 0.0f;
         rotation.y = 0.0f;
			
	 if (objectPrefab != null) 
	 Instantiate(objectPrefab , position, rotation);
			
}

Is anyone able to give me some pointers on how to achieve this?

Regards

  • Matt

I think you’re working too hard. You’re instantiating these prefabs, so you know they always start out with some rotation (probably 0,0,0). So you just need to rotate each one around Z by a certain number of degrees. In fact, you’ve already calculated angle as part of calculating the position – the rotation you want is probably either the same angle, or the negative of that. Something like this:

  Quaternion rotation = Quaternion.Euler(0, 0, angle);
  Instantiate(objectPrefab, position, rotation);

Fiddle with that a bit and I think you’ll have it.

Using look rotation, use the direction parameter as the upwards direction, and Vector3.forward as the look direction

Hey guys, Yea got it working using Hpjohn’s advice. I forgot LookRotation(…) had the up direction var.

Cheers for the replys.