2017-02-09 33 views
-1

攝像機跟隨對象我做了一個播放器預製與標籤球員是在場景催生當遊戲starts.How我可以讓相機使用球員標籤跟隨玩家。如何使標籤

目前使用下面的腳本

public Transform target;   // The position that that camera will be following. 
    public float smoothing = 5f;  // The speed with which the camera will be following. 

    Vector3 offset;      // The initial offset from the target. 


    void Start() 
    { 
     // Calculate the initial offset. 
     offset = transform.position - target.position; 
    } 


    void FixedUpdate() 
    { 

     // Create a postion the camera is aiming for based on the offset from the target. 
     Vector3 targetCamPos = target.position + offset; 

     // Smoothly interpolate between the camera's current position and it's target position. 
     transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime); 
    } 

回答

3

使用this

由於

public Transform target;   // The position that that camera will be following. 
public float smoothing = 5f;  // The speed with which the camera will be following. 


Vector3 offset;      // The initial offset from the target. 


void Start() 
{ 
    try 
    { 
     target = GameObject.FindGameObjectWithTag("Player").transform; // this is goint to find a certain tagged object from hirarchey and assing it to target. 
    } 
    catch (NullReferenceException ex) 
    { 
     Debug.Log("target gameObjects is not present in hierarchy "); 
    } 

    // Calculate the initial offset. 
    offset = transform.position - target.position; 
} 


void FixedUpdate() 
{ 

    // Create a postion the camera is aiming for based on the offset from the target. 
    Vector3 targetCamPos = target.position + offset; 

    // Smoothly interpolate between the camera's current position and it's target position. 
    transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime); 
} 

,或者你可以做一個事件,並用標籤發現gameObejct當它催生了在一定的時間

As

public Transform target;   // The position that that camera will be following. 
public float smoothing = 5f;  // The speed with which the camera will be following. 


Vector3 offset;      // The initial offset from the target. 


void Start() 
{ 
    // Calculate the initial offset. 

    offset = transform.position - target.position; 
} 

// Call this method where you spawing your target and set the tag and call this mehtod supply tag parameter 
public void FindTaggedGameObject(string tag) 
{ 
    try 
    { 
     target = GameObject.FindGameObjectWithTag("Player").transform; // this is goint to find a certain tagged object from hirarchey and assing it to target. 
    } 
    catch (NullReferenceException ex) 
    { 
     Debug.Log("target gameObjects is not present in hierarchy "); 
    } 
} 


void FixedUpdate() 
{ 

    // Create a postion the camera is aiming for based on the offset from the target. 
    Vector3 targetCamPos = target.position + offset; 

    // Smoothly interpolate between the camera's current position and it's target position. 
    transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime); 
} 
+0

給了一個錯誤**的NullReferenceException:對象沒有設置爲一個對象 CompleteProject.CameraFollow.FixedUpdate(的實例)(在資產/ _CompletedAssets /腳本/相機/ CameraFollow.cs:27)** –

+0

其一個給錯誤你試過了哪一個?你有把標籤添加到目標對象嗎?你的物體什麼時候產卵?如果它開始使它在清醒時產卵 –

+0

第一個給出錯誤 –