2016-10-25 38 views
1

我正在關注Survival Shooter統一教程,並且在教程中向下給出的代碼用於使相機跟隨玩家。代碼正在工作,但是我該如何改變它,以便它在給定的X和Y處停止跟隨玩家點。Unity:停止攝像機跟隨播放器?

代碼

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); 
} 

回答

2

簡單地停止更新自己的立場:

private bool followPlayer = true; 
void FixedUpdate() 
{ 
    if(followPlayer){ 
     // 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); 
    } 
} 

變化followPlayerfalse值,它將停止以下

2

要判斷玩家是否是在給定點,你需要檢查玩家和點之間的距離,例如:

public Transform target;    // player position 
public Transform stopingPoint;   // stopping point position 
public double tolerance;    // the "radius" of stopping point 

private bool followPlayer = true; 

    ... 

void FixedUpdate() 
{ 
    if(!followPlayer) 
     return; 
    followPlayer = Vector3.Distance(target.position, stopingPoint.position) <= tolerance; 

    ...