Unity:停止相机跟随播放器?

时间:2016-10-25 19:59:39

标签: unity3d camera

我正在关注Survival Shooter团结教程,在教程中,使用向下的代码来使摄像机跟随播放器。代码正在工作,但我怎么能改变它以便它停止跟随玩家在给定的X和是的。

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 个答案:

答案 0 :(得分: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);
    }
}

followPlayer的值更改为false,它将停止跟随

答案 1 :(得分: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;

    ...