Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Proper method to retrieve camera position

Discussion in 'Project Tiny' started by m-naimshah, Jul 1, 2019.

  1. m-naimshah

    m-naimshah

    Joined:
    Jun 21, 2019
    Posts:
    3
    I attempted to move the camera using mouse input using the following script but the camera shakes rapidly once moved. I assume that the method to retrieve camera position is incorrect. I have tried using math.Lerp as a workaround but the screen shaking still happens.

    Code (CSharp):
    1.  
    2.         float3 inputPosition;
    3.         float3 inputPositionPrev;
    4.         protected override void OnUpdate()
    5.         {
    6.             InputSystem inputSystem = World.GetExistingSystem<InputSystem>();
    7.             inputPosition = inputSystem.GetWorldInputPosition();
    8.  
    9.             if (inputSystem.GetMouseButtonDown(0))
    10.             {
    11.                 inputPositionPrev = inputPosition;
    12.             }
    13.             if (inputSystem.GetMouseButton(0))
    14.             {
    15.                 Entities.WithAll<Camera2D>().ForEach((Entity entity, ref Translation translation) =>
    16.                 {
    17.                     inputPosition = inputSystem.GetWorldInputPosition();
    18.                     float3 newPos = translation.Value - (inputPosition - inputPositionPrev);
    19.  
    20.                     translation.Value = newPos; // BUG: will shake screen rapidly ):
    21.  
    22.                     // Store last input pos
    23.                     inputPositionPrev = inputPosition;
    24.                 });
    25.             }
    26.         }