2012-09-03 4 views
0

나는 타워 방어 게임과 같은 Unity 3D에서 첫 게임을 만들고 있습니다. 필자는 타워의 Fbx 모델을 가져 와서 조립식에 부착했습니다. 이제 타워의 노즐이 회전하여 적이 지나갈 때 적을 따라 가기를 원합니다. 내가 가져온 fbx 모델에서 고정 된 타워 바닥에 두 개의 폴리 메쉬가 있고, 회전 할 타워 맨 위에 하나가 있습니다. 이제이 두 개의 메쉬로 두 개의 다른 gameObject를 만들려고했으나 같은 위치에 놓으면 중복됩니다. 그래서 저는 노즐이베이스 위에 정확하게 놓 이도록 수동 정렬을해야합니다. 전체 타워가 하나의 gameObject 인 다른 방법이 있는지 궁금 해서요. 상단 부분을 회전시킬 수 있습니다.unity3D에서 타워 노즐 메쉬 회전

답변

1

나는 내 문제를 해결할 수있었습니다. 가장 좋은 방법인지는 확실치 않지만 작동합니다.

타워를 업그레이드하고 노즐 부분 만 변환하려면 본질적으로이 작업을 수행했습니다.

public class tryFbx : MonoBehaviour { 
    public GameObject[] ModelPrefab; 
    GameObject modelInstance; 
    Renderer rn = new Renderer(); 

    // Attaching the model to prefab at runtime by creating a array of prefabs 
    public void AttachModelToPrefab(GameObject modelPrefab) { 
     modelInstance = GameObject.Instantiate(modelPrefab) as GameObject; 
     modelInstance.transform.position = transform.position; 
     modelInstance.transform.rotation = transform.rotation; 

     // Attach the model instance to the prefab shell 
     modelInstance.transform.parent = gameObject.transform; 
    } 

    void Start() { 

    } 

    // Update is called once per frame 
    void Update() { 
      if (GameManager.upgrade){ 
      AttachModelToPrefab(ModelPrefab[GameManager.towerUpgradeLevel]); 
      foreach (Renderer r in modelInstance.GetComponentsInChildren<Renderer>()){ 
        // "polySurface98" is the name of the mesh I want to rotate. The tower and its upgrade have the same name. 
        if (r.name == "polySurface98") 
        rn = r; 
      } 
     // apply any transformation to the partial fbx 
     rn.transform.Translate(1,1,1); 
     } 
    } 
} 
0

GameObjects의 계층 구조를 만들 수 있습니다. 당신은 하나의 parent GameObject를 가지고 타워와 2 명의 아이들을 표현할 것입니다. 아이들의 좌표는 부모의 좌표와 관련이 있으므로, 당신이 아이들에게하는 "지저분한"수동 교정이 포함될 것임을 주목하십시오.

더 복잡한 경우 애니메이션이있는 하나의 메시 만 만들어 하나의 GameObject에 적용 할 수 있습니다.