# Limit Vector Size in a radius

**URL:** <https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556>\
**Category:** Community & Industry Discussion\
**Tags:** math, unreal-engine, vector, clamp, question\
**Created:** [April 21, 2017, 8:09pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556 "2017-04-21T20:09:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bonatti-San](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bonatti-San](https://forums.unrealengine.com/u/Bonatti-San)\
**Post date:** [April 21, 2017, 8:09pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/1 "2017-04-21T20:09:29Z")

</div>

Hello,

I’m making an object which the player controls it by adding relative location.

What a want to do: When this object arrive some distance (length between Character and the object), they will not be possible to go any further, just to return, and keep in that radius without going away.

How can I do that?

Thanks!

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/e/a/5eaa3314f8af887616935c1e7225a538aa81bc32.png) ](https://i.imgur.com/QD8AOmX.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ecfaa40](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ecfaa40](https://forums.unrealengine.com/u/anonymous_user_5ecfaa40)\
**Post date:** [April 21, 2017, 10:01pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/2 "2017-04-21T22:01:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bonatti-San](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bonatti-San](https://forums.unrealengine.com/u/Bonatti-San)\
**Post date:** [April 21, 2017, 10:39pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/3 "2017-04-21T22:39:09Z")

</div>

Hi RoboScorpion, thanks for the repply.

Yes, the object is attached to the player, and will keep in the radius of the player.

I tried what you said, but when I set the relative location of the step 4, it reset the location to 0 when reach outside the radius.

Take a look if I’m doing something wrong:  
[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/d/b/adb9b97775d4cb3d4401d99726c961ce79b0db6b.png) ](https://i.imgur.com/njeslIQ.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5ecfaa40](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5ecfaa40](https://forums.unrealengine.com/u/anonymous_user_5ecfaa40)\
**Post date:** [April 21, 2017, 10:57pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/4 "2017-04-21T22:57:32Z")

</div>

Can you try using a \> node instead of a \>= node?

---

<div class="post-metadata">

**Author:** ![Bonatti-San](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bonatti-San](https://forums.unrealengine.com/u/Bonatti-San)\
**Post date:** [April 21, 2017, 11:21pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/5 "2017-04-21T23:21:48Z")

</div>

Yep. Same results.

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [April 22, 2017, 5:09pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/6 "2017-04-22T17:09:08Z")

</div>

Math your vector and clamp it after all.

 ![135166-2017-04-23_00-08-03.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/4/d/84d70a23cbab006411045ab24b9d6d325fcc2ed1.png)

---

<div class="post-metadata">

**Author:** ![Bonatti-San](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bonatti-San](https://forums.unrealengine.com/u/Bonatti-San)\
**Post date:** [April 22, 2017, 6:19pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/7 "2017-04-22T18:19:45Z")

</div>

Hello Two-faced, thanks for the repply.

I clamp the vector between A and B, but don’t figure out how I use with the add relative location node

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [April 22, 2017, 6:21pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/8 "2017-04-22T18:21:11Z")

</div>

Add location manually with + node)

---

<div class="post-metadata">

**Author:** ![Bonatti-San](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bonatti-San](https://forums.unrealengine.com/u/Bonatti-San)\
**Post date:** [April 22, 2017, 6:28pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/9 "2017-04-22T18:28:02Z")

</div>

Sorry, I don’t get it ☹

 ![135168-capturar.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/8/b/38bcad733c7008fda43ed3a87667c5610469161f.png)

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [April 22, 2017, 7:25pm UTC](https://forums.unrealengine.com/t/limit-vector-size-in-a-radius/391556/10 "2017-04-22T19:25:29Z")

</div>

You should get location, + it, clamp and set.
