2017-09-06 40 views
1

現在,他們正面臨着外界:我如何讓角色站成圈形,但面向內部?

Facing out

我想到目前爲止的唯一的事情就是改變這一行:

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; 
    } 
} 
+1

爲什麼不牛逼下襬transform.lookat(centralpoint.transform),然後簡單地旋轉它們180度? – oxrock

+0

@oxrock如何做到這一點?現在我只添加了line squadMemeber.LookAt(center); var var line之前。 –

回答

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; 
相關問題