Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. 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.         }