2016-12-10 3 views
2

私は自分のシーンでオブジェクトをインスタンス化しています。 オブジェクトはインスタンス化されますが、カメラのターゲットにオブジェクトを渡す方法はわかりません。 とあなたカントドラッグインスタンス化されたオブジェクトインスペクタにカメラにインスタンス化されたオブジェクトを渡す方法

public class TankManager : MonoBehaviour 
{ 
void Start() 
{ 
    Instantiate (MenuManager.SelectedCharacter, Vector3.zero, Quaternion.identity); 
}} 

私のカメラコード:

public class FollowCamera : MonoBehaviour{ 
public Transform target; 
public Vector3 offsetPosition; 
public Space offsetPositionSpace = Space.Self; 
public bool lookAt = true; 

private void Update() 
{ 
    Refresh(); 
} 

public void Refresh() 
{ 
    if(target == null) 
    { 
     Debug.LogWarning("Missing target ref !", this); 

     return; 
    } 

    if(offsetPositionSpace == Space.Self) 
    { 
     transform.position = target.TransformPoint(offsetPosition); 
    } 
    else 
    { 
     transform.position = target.position + offsetPosition; 
    } 

    if(lookAt) 
    { 
     transform.LookAt(target); 
    } 
    else 
    { 
     transform.rotation = target.rotation; 
    } 
} 

}

答えて

2

あなたのターゲット変数が公開されているので、TankManagerスクリプト内のそれを参照すること。 あなたはTankManagerスクリプトは、階層内のゲームオブジェクトに添付されていることを仮定すると、スクリプトは次のようになります。

public class TankManager : MonoBehaviour 
{ 

public Camera camera; 
FollowCamera followC; 

    void Start() 
    { 
     GameObject target = Instantiate (MenuManager.SelectedCharacter, Vector3.zero, Quaternion.identity); 
     followC = camera.GetComponent <FollowCamera>(); 
     followC.target = target; 
    } 
} 

は、それからちょうどTankManagerインスペクタエリアにカメラをドラッグ&ドロップします。

+0

それは働いてくれてありがとう兄弟ですが、gameobjectを読んでいる人のための少しの強化は、変換する必要がありますもキャストが必要です。 –

関連する問題