Search Unity

camera zoom but translating to target like in google maps (perspective camera)

Discussion in 'Scripting' started by pretender, Dec 13, 2018.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi guys, I was trying to achieve effect when zooming on something beneath mouse, camera locks that position, it always stays beneath that mouse. Camera is not ortho but perspective. here is what i have so far but it doesn't work that good. target isn't locked

    Code (CSharp):
    1.  
    2.     // This is in LateUpdate
    3.     RaycastHit hit;
    4.     var ray = _camera.ScreenPointToRay (Input.mousePosition);
    5.     if (Physics.Raycast (ray, out hit))
    6.     {
    7.         var offset = new Vector3((target.position.x - hit.point.x), target.position.y, (target.position.z - hit.point.z));
    8.         target.Translate(offset * Time.deltaTime * 3f);
    9.  
    10.     }
    11.  
    Thanks!
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    why not simply using the ray direction like this:
    o far but it doesn't work that good. target isn't locked

    Code (CSharp):
    1.  
    2.     // This is in LateUpdate
    3.     const float ZOOM_SPEED = 3f;
    4.     RaycastHit hit;
    5.     var ray = _camera.ScreenPointToRay (Input.mousePosition);
    6.     if (Physics.Raycast (ray, out hit))
    7.     {
    8.         target.Translate(ZOOM_SPEED * ray.direction * Time.deltaTime);
    9.  
    10.     }
    11.  
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Lookup "quill18creates", project "mostly civilized".
    He does that with the camera on one if the first episodes(i think 2 or 3)