Limit Vector Size in a radius

Hi,

Just need clarification on something. Are you trying to keep the object within some distance of the character? Or are you trying to keep the character within some distance of the object?

If you want to keep something within a certain radius of something else, you can try doing this. Say we want to keep “B”, within some distance “r”, of “A”.

  1. Get the vector, let’s call it “U”, from the A to the location where you are trying to move B.
  2. Get the size(length) of U and check to see if it’s greater than r.
  3. If it is greater than r, don’t move B in that direction.
  4. If B somehow ends up outside of the radius, just normalize U, then multiply it by r. Set this vector as B’s relative location from A.

This is also assumes that B is also attached to A. That way B moves with A.