现在他们正面向外面:
我到目前为止唯一尝试的是改变这条线:
var rot = Quaternion.LookRotation(pos - center);
要
var rot = Quaternion.LookRotation(pos + center);
但它没有用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadFormation : MonoBehaviour
{
enum Formation
{
Square, Circle
}
public Transform squadMemeber;
public int columns = 4;
public int space = 10;
public int numObjects = 20;
public float yOffset = 1;
// Use this for initialization
void Start()
{
ChangeFormation();
}
// Update is called once per frame
void Update()
{
}
private void ChangeFormation()
{
Formation formation = Formation.Circle;
switch (formation)
{
/*case Formation.Square:
for (int i = 0; i < 23; i++)
{
Transform go = Instantiate(squadMemeber);
Vector3 pos = FormationSquare(i);
go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
go.Rotate(new Vector3(0, -90, 0));
}
break;*/
case Formation.Circle:
Vector3 center = transform.position;
for (int i = 0; i < numObjects; i++)
{
Vector3 pos = RandomCircle(center, 5.0f);
var rot = Quaternion.LookRotation(pos - center);
pos.y = Terrain.activeTerrain.SampleHeight(pos);
pos.y = pos.y + yOffset;
Instantiate(squadMemeber, pos, rot);
}
break;
}
}
Vector2 FormationSquare(int index) // call this func for all your objects
{
float posX = (index % columns) * space;
float posY = (index / columns) * space;
return new Vector2(posX, posY);
}
Vector3 RandomCircle(Vector3 center, float radius)
{
float ang = Random.value * 360;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.y = center.y;
return pos;
}
}
答案 0 :(得分:2)
你太近了。您必须在LookRotation
函数中切换变量的顺序,并在实例化后将变量赋值给对象。
替换
var rot = Quaternion.LookRotation(pos - center);
与
var rot = Quaternion.LookRotation(center - pos);
然后替换:
Instantiate(squadMemeber, pos, rot);
与
Transform insObj = Instantiate(squadMemeber, pos, rot);
insObj.rotation = rot;