相机行为

时间:2018-10-31 06:52:19

标签: c# unity3d camera

此摄影机脚本旨在旋转播放器并观察播放器,并在不移动时缓慢地捕捉到播放器。 (播放器在开始移动之前先经过Vector3,然后再进行移动)。我的问题是我想缓慢地“喂入” deltaPosition,所以这不是突然的,而是缓慢而平稳的过渡,如果到达,也要停止添加。

    private void LateUpdate()
{
    if (player.isMoving)
    {
        desiredPosition = player.beforeMoving + offset;
    }
    else
    {
        Vector3 deltaPosition = player.transform.position - player.beforeMoving; 
        desiredPosition += deltaPosition * Time.deltaTime;
    }
    Quaternion camTurnAngle =
    Quaternion.AngleAxis(input * rotationSpeed, Vector3.up);
    desiredPosition = camTurnAngle * desiredPosition;
    transform.position = Vector3.Slerp(transform.position, desiredPosition, smoothFactor);
    transform.LookAt(player.transform);
}

编辑::我想我会分享最终代码。

    private void LateUpdate()
{
    Quaternion rotation = Quaternion.Euler(GetDegree(), input.x * rotationSpeed, 0f);
    if (player.isMoving)
    {
        desiredPosition = player.beforeMoving + offset;
        CalculatePanTime();
    }
    else if (!player.isMoving)
    {
        desiredPosition = player.transform.position + offset;
    }
    transform.position = Vector3.Slerp(transform.position, rotation * desiredPosition, GetSpeed());
    transform.LookAt(player.transform);
}

private void CalculatePanTime()
{
    stoppedTime = Time.time;
    playerDelta = Vector3.Distance(player.transform.position, player.beforeMoving);
    timeToPan = (playerDelta / snappingSpeed) * Time.deltaTime;
}

private float GetSpeed()
{
    if (Time.time < stoppedTime + timeToPan)
    {
        controlsDisabled = true; return snappingSpeed;
    }
    else
    {
        controlsDisabled = false; return smoothSpeed;
    }
}

1 个答案:

答案 0 :(得分:3)

您正在告诉我们您想要代码做什么,这很好。您还将发布为实现目标而实现的代码,这也很好。您还可以告诉我们,由于该代码导致什么不起作用吗?

据我了解,它是“ Vector3 deltaPosition = player.transform.position-player.beforeMoving;         wantedPosition + = deltaPosition * Time.deltaTime; “这并不符合您的预期

也许尝试这样的事情:

private void LateUpdate()
{
    // snap the rotation center slowly to the player's position if not moving, or player's position before he started moving
    desiredPosition = Vector3.Lerp(desiredPosition, player.beforeMoving, 0.1f);

    // rotate around rotation center
    Quaternion camTurnAngle = Quaternion.AngleAxis(rotationSpeed * Time.time, Vector3.up);      
    desiredPosition += camTurnAngle * offset;

    // set the position to rotate around rotation center, and look towards player
    transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothFactor);
    transform.LookAt(player.transform);
}

您的代码的问题是您超调了。如果要实现一些决定相机拍摄速度或拍摄时间的方法,请尝试引入一个浮动播放器。timeStopMoving= Time.time,可用于在他不移动时正确计算位置校正

if(player.isMoving)
{
    desiredPosition = player.beforeMoving;
}
else
{
    const float timeNeededToSnap = 2f;
    // or float timeNeededToSnap = (player.transform.position - player.beforeMoving).magnitude; // (which you could compute only once in player script, when he stops moving, and then reuse the value instead of performing a ".magnitude" every frame)
    if(Time.time < player.timeStopMoving + timeNeededToSnap)
    {
        desiredPosition = Vector3.Lerp(desiredPosition, player.transform.position, (Time.time - player.timeStopMoving) / timeNeededToSnap);
    }
    else
    {
        // an other problem here is: if the player starts moving AGAIN before your desiredPosition got the value, do you want desired position to glich to the new player.beforeMoving?...
        desiredPosition = player.transform.position;
    }
}

编辑:

要使他的束缚不那么线性,可以使用:

if(Time.time < player.timeStopMoving + timeNeededToSnap)
{
    var t = Mathf.Cos(Maths.Pi * 0.5f * (Time.time - player.timeStopMoving) / timeNeededToSnap); // as timeDelta/totalTime goes from 0->1, multiply it by Pi/2 and the Cos will also go from 0->1 but with a smoothing speed
    desiredPosition = Vector3.Lerp(desiredPosition, player.transform.position, t);
}