Search Unity

Teather camera movement

Discussion in 'Scripting' started by siegeon, Sep 5, 2011.

  1. siegeon

    siegeon

    Joined:
    Nov 23, 2010
    Posts:
    81
    [Resolved]

    I have have trouble figuring this out, but I would like to have the camera slerp to the cursor up to a maximum distance from the main player. Right now the camera will slerp to the cursor regardless of the distance from the player. I was hoping someone else would be willing to take a look, and give me a suggestion of how to achieve this.

    This is the code that affects camera movement.
    Code (csharp):
    1.  
    2. // HANDLE CAMERA POSITION
    3.         Vector3 cameraAdjustmentVector = GetCameraAdjustmentValue(ref posRel);
    4.  
    5.         // Set the target position of the camera to point at the focus point
    6.         Vector3 cameraTargetPosition = GetCameraTarget(ref cameraAdjustmentVector);
    7.  
    8.         SetCameraPosition(cameraTargetPosition);
    9.        
    10.         // Save camera offset so we can use it in the next frame
    11.         cameraOffset = mainCameraTransform.position - player.position;
    12.     }
    13.         private Vector3 GetCameraTarget(ref Vector3 cameraAdjustmentVector)
    14.         {
    15.             Vector3 cameraTargetPosition = player.position + cameraOffset + cameraAdjustmentVector * cameraPreview;// +initOffsetToPlayer;
    16.             return cameraTargetPosition;
    17.         }
    18.         private void SetCameraPosition(Vector3 cameraTargetPosition)
    19.         {
    20.  
    21.             mainCameraTransform.position = Vector3.SmoothDamp(mainCameraTransform.position, cameraTargetPosition, ref cameraVelocity, cameraSmoothing);
    22.  
    23.             if (Vector3.Distance(mainCameraTransform.position, player.position) > 28f)
    24.             {
    25.                 cameraTargetPosition = mainCameraTransformPreviousPosition;
    26.                 return;
    27.             }
    28.  
    29.             mainCameraTransformPreviousPosition = mainCameraTransform.position;
    30.         }
    31.  
     
    Last edited: Sep 6, 2011