Search Unity

Help with camera script.

Discussion in 'Scripting' started by hafizmrozlan, Jun 24, 2016.

  1. hafizmrozlan

    hafizmrozlan

    Joined:
    Jun 15, 2012
    Posts:
    117
    Hey guys,

    So I have a camera that'll follow my player movement. But instead of following the player directly, I want the camera to snap ahead on the next fixed position.

    This is what I don't want https://giphy.com/gifs/3o72FgymNAtqPFZdTO

    This is what I want to achieve, but I only manage to get it right on the first jump only https://giphy.com/gifs/l46CjLNPc5MKz3KyQ

    The following is the code I used for the second GIF, initially I used Lerp function to clamp the camera position with regards to player position.

    Code (CSharp):
    1. void MoveCamera () {
    2.         Vector3 targetPosition = new Vector3 (transform.position.x, transform.position.y + distanceToCover, transform.position.z);
    3.         float percentToMove = (player.transform.position.y - playerStartingPosition) / (playerTargetPosition - playerStartingPosition);
    4.         percentToMove = Mathf.Clamp01 (percentToMove);
    5.  
    6.         transform.position = Vector3.Lerp(startingPosition, new Vector3(0f, targetPosition, -10f), percentToMove);
    7.     }
    But then I discovered a lazier way which is...

    Code (CSharp):
    1. void Update () {
    2.             float cameraPositionY = player.transform.position.y + verticalOffset;
    3.             cameraPositionY = Mathf.Clamp (cameraPositionY, player.transform.position.y, targetPosition);
    4.             transform.position = new Vector3 (0f, cameraPositionY, -10f);
    5.         }
    However I'm still lost on how to update the next targetPosition for the camera.

    What I have tried, checking with if statement, if the player has arrived his destination, camera's targetPosition will be increment to the next step. However, due to player movement in arching way, the first time the player pass the platform before hitting the apex and returned back to the platform already trigger the targetPosition increment code. This cause the camera targetPosition to be updated before the player resting on the platform again.

    The same way I have tried, if the camera has reached its targetPosition, give different targetPosition value. But it all happen in single frame when new value assigned, the player still moving up and the camera will chase it again and its behaviour becoming like the first GIF. I know somehow I can turn the code to call the function in different frame but I still don't know how yet. Currently all the code is called in Update function, will turn them later to LateUpdate.

    Some work around I have considered is to trigger the camera movement the same way I triggered the player jump. So every time the player jumps, the camera will move upward in its own speed but independently from the player position, but will be scaled to make it look like it is following the player. But I feel this work around is dirty and there must be a correct way to do it.

    Help, please?
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    How about making targetPosition for camera a variable that only changes when you press jump key?

    Alternatively I don't think you should be changing the target on every frame. Moving camera can be just the Vector3.Lerp(). It sounds like you want it to happen only under certain conditions.
    If Camera position.Y is close enough to camera target.Y && player position Y is far enough from camera target.Y.
    The "close enough" is done with Mathf.Abs(a-b) if you need it.