title: Find point at distance along a vector
body: <p>Given a known direction and range I'm trying to calculate the 3D position along the vector at that range from the starting point. </p>
  
  <p>To do this I'm basically following the Unity manual and the solution offered in various other questions about this topic: </p>
  
  <blockquote>
    <p>Take your direction, normalise it and multiply by the required
    distance.</p>
  </blockquote>
  
  <p>This isn't working for me but I can't figure out what's wrong. Here's my code that should draw a line from the starting point towards the mouse cursor position, ending at the given distance from the starting point:</p>
  
  <pre class="lang-c# prettyprint-override"><code>IEnumerator DrawLineToMouse(float range)
  {   
      RayCastHit hit;
      Vector3 endPos;
      float rangeSquared = Mathf.Sqrt(range);
  
      while (Input.GetButton("Fire1"))
      {
          if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 2000))
          {
              endPos = (hit.point - startPos).normalized * rangeSquared; // startPos is the starting point of the line to be drawn  
              Debug.DrawLine(startPos, hit.point, Color.green, Time.deltaTime);
              Debug.DrawLine(startPos, endPos, Color.red, Time.deltaTime);   
          } 
  
          yield return null;
      }
  }
  </code></pre>
  
  <p>For some reason the direction seems off. In this picture the green Debug.Draw line is direct from the starting point to the mouse position, the red line is to the calculated vector:</p>
  
  <p><a href="https://i.stack.imgur.com/kj9zM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kj9zM.png" alt="enter image description here"></a></p>
  
  <p>I've tried both perspective and orthographic cameras, and I've tried varying the starting point, the problem is the same. I don't know what to try regarding the code because everything I've read suggests it should work.</p>
  
  <p>What could be the problem?</p>