4

I'm instantiating prefabs in a circle around a central point via the following code...

#pragma strict

var numPoints : int = 7;
var radiusX : int;
var radiusY : int;
var chair : Transform;

function Start () {
    distributeFields();
}

function distributeFields() {
    var centrePos = Vector3(-350,24,-490);
    for (var pointNum = 0; pointNum < numPoints; pointNum++)
    {
        var i = (pointNum * 1.0) / numPoints;
        var angle = i * Mathf.PI * 2;
        var x = Mathf.Sin(angle) * radiusX;
        var z = Mathf.Cos(angle) * radiusY;
        var pos = Vector3(x, 0, z) + centrePos;
        Instantiate(chair, pos, Quaternion.LookRotation(pos));
    } 
}

I want each prefab to face the center, but they are all rotating in the same direction. I assume that each iteration is rotating all "chair" prefabs to the current iteration's rotation. Anything I'm doing clearly wrong here?

tintyethan
  • 1,772
  • 3
  • 20
  • 44

1 Answers1

3

It seems that all of the chairs are now looking away from the origin and that is why they seem to look into same direction. This is the case because Quaternion.LookRotation gets the chair's position vector as input.

If I understand correctly the desired input is vector from the chair to the centrePos. This can be done by changing the instantiate line to this:

    Instantiate(chair, pos, Quaternion.LookRotation(-Vector3(x, 0, z)));
maZZZu
  • 3,585
  • 2
  • 17
  • 21