2017-02-09 70 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

第一个给出错误 –