title: Finding magnitude of vector in given direction
body: <p>I am writing a tool in Unity3D which allows dragging points to resize shapes:</p>
  
  <p><img src="https://i.stack.imgur.com/JKvR7.png" alt="enter image description here"></p>
  
  <p>The drag points can only be moved along the surface normal (i.e. the example cube could not become deformed, only elongated).</p>
  
  <p>Naturally the mouse can be dragged in any direction, so I am trying to calculate the distance dragged along the surface normal.</p>
  
  <p>Currently I am doing the following:</p>
  
  <pre><code>public static float NormalMoveHandle(Vector3 position, Vector3 normal)
  {
      Vector3 newPosition = FreeMoveHandle(position);
      Vector3 dragVector = newPosition - position;
  
      return Vector3.Dot(dragVector, normal) * dragVector.magnitude;
  }
  </code></pre>
  
  <p>However I'm getting unpredictable results:</p>
  
  <p><a href="http://i.gyazo.com/35f3fc4b4e0471f3c5d70097ddc6f79f.mp4" rel="nofollow noreferrer">http://i.gyazo.com/35f3fc4b4e0471f3c5d70097ddc6f79f.mp4</a></p>
  
  <p>When dragging more than a short distance the value will suddenly become NaN:</p>
  
  <p><img src="https://i.stack.imgur.com/BMK2W.png" alt="enter image description here"></p>
  
  <p>If I adjust my calculation I get more stable behaviour, but the handle becomes sluggish. I believe this demonstrates that the handles are functional; the problem lies with my maths.</p>
  
  <pre><code>return Vector3.Dot(dragVector, normal);
  </code></pre>
  
  <p>Is there an error in my magnitude calculation?</p>