title: Move to target with offset
body: <p>I am trying to move my camera to a clicked Gameobjects position with a specified offset. So the script should calculate the resulting position by the clicked gameobjects position, the actual camera position and a offset.</p>
  
  <p>I have tried it with this code:</p>
  
  <pre><code>Vector3 distanceVector = transform.position - target.transform.position;
  Vector3 distanceVectorNormalized = distanceVector.normalized;
  targetPosition = (distanceVectorNormalized * preferredDistance);
  </code></pre>
  
  <p>But I am getting some really weird values. Here is the code I made for this:</p>
  
  <pre><code>public float moveSpeed = 0.1f;
  
  private bool moving = false;
  private GameObject target;
  
  // The distance between the camera and the targets position
  private float preferredDistance = 3;
  
  // The position the camera will move to
  private Vector3 targetPosition;
  
  // Start is called before the first frame update
  void Start()
  {
  }
  
  // Update is called once per frame
  void Update()
  {
      if (Input.GetMouseButtonDown(0))
      {
          var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
          if (Physics.Raycast(ray, out var hit, 100) == false) return;
  
          Debug.Log(hit.transform.gameObject.name);
          target = hit.transform.gameObject;
  
          ... Here should be the calculations
  
          moving = true;
      }
  
      if (moving)
      {
          transform.position = Vector3.Lerp(transform.position, targetPosition, moveSpeed);
          transform.LookAt(target.transform.position);
  
          var offsetX = Math.Abs(transform.position.x - targetPosition.x);
          var offsetZ = Math.Abs(transform.position.z - targetPosition.z);
  
          if (offsetX &lt; .01
              &amp;&amp; offsetZ &lt; .01) moving = false;
      }
  }
  </code></pre>