简体   繁体   中英

Camera Behaviour

This camera script is intended to rotate and look at the player while it is moving and snapping to the player slowly while it isn't. (Player passes over a Vector3 beforeMoving before it is starting movement). My issue is I want to "feed" the deltaPosition slowly so it isn't a sudden snap but rather a slow and smooth transition, also stop adding if arrived.

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

Edit: I thought I would share the final code.

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

You are telling us what you want the code to do, that is good. You are also posting the code you implemented to achieve your goal, that is also good. Can you also tell us what is not working as you want it to work as a result of that code?

From what I understand it is the "Vector3 deltaPosition = player.transform.position - player.beforeMoving; desiredPosition += deltaPosition * Time.deltaTime; " that is not behaving as you expect it to

maybe try something like this:

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

the problem with your code is that you are overshooting. If you want to implement something that decides how fast the camera snaps, or how much time it should take to snap, try introducing a float player.timeStopMoving = Time.time that you can use to correctly compute the position correction while he is not moving.

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

To make he lerp less linear, you can use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM